]> git.mxchange.org Git - friendica.git/blob - include/items.php
Merge remote branch 'upstream/master'
[friendica.git] / include / items.php
1 <?php
2
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
9
10 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
11
12
13         $sitefeed    = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
14         $public_feed = (($dfrn_id) ? false : true);
15         $starred     = false;   // not yet implemented, possible security issues
16         $converse    = false;
17
18         if($public_feed && $a->argc > 2) {
19                 for($x = 2; $x < $a->argc; $x++) {
20                         if($a->argv[$x] == 'converse')
21                                 $converse = true;
22                         if($a->argv[$x] == 'starred')
23                                 $starred = true;
24                         if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
25                                 $category = $a->argv[$x+1];
26                 }
27         }
28
29         
30
31         // default permissions - anonymous user
32
33         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid`  = '' AND `deny_gid`  = '' ";
34
35         $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
36                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
37                 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
38                 dbesc($owner_nick)
39         );
40
41         if(! count($r))
42                 killme();
43
44         $owner = $r[0];
45         $owner_id = $owner['user_uid'];
46         $owner_nick = $owner['nickname'];
47
48         $birthday = feed_birthday($owner_id,$owner['timezone']);
49
50         if(! $public_feed) {
51
52                 $sql_extra = '';
53                 switch($direction) {
54                         case (-1):
55                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
56                                 $my_id = $dfrn_id;
57                                 break;
58                         case 0:
59                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
60                                 $my_id = '1:' . $dfrn_id;
61                                 break;
62                         case 1:
63                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
64                                 $my_id = '0:' . $dfrn_id;
65                                 break;
66                         default:
67                                 return false;
68                                 break; // NOTREACHED
69                 }
70
71                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
72                         intval($owner_id)
73                 );
74
75                 if(! count($r))
76                         killme();
77
78                 $contact = $r[0];
79                 $groups = init_groups_visitor($contact['id']);
80
81                 if(count($groups)) {
82                         for($x = 0; $x < count($groups); $x ++) 
83                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
84                         $gs = implode('|', $groups);
85                 }
86                 else
87                         $gs = '<<>>' ; // Impossible to match 
88
89                 $sql_extra = sprintf(" 
90                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
91                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
92                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
93                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
94                 ",
95                         intval($contact['id']),
96                         intval($contact['id']),
97                         dbesc($gs),
98                         dbesc($gs)
99                 );
100         }
101
102         if($public_feed)
103                 $sort = 'DESC';
104         else
105                 $sort = 'ASC';
106
107         if(! strlen($last_update))
108                 $last_update = 'now -30 days';
109
110         if(isset($category)) {
111                 $sql_extra .= file_tag_file_query('item',$category,'category');
112         }
113
114         if($public_feed) {
115                 if(! $converse)
116                         $sql_extra .= " AND `contact`.`self` = 1 ";
117         }
118
119         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
120
121         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
122                 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`, 
123                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
124                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
125                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
126                 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
127                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
128                 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
129                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0 
130                 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
131                 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
132                 $sql_extra
133                 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
134                 intval($owner_id),
135                 dbesc($check_date),
136                 dbesc($check_date),
137                 dbesc($sort)
138         );
139
140         // Will check further below if this actually returned results.
141         // We will provide an empty feed if that is the case.
142
143         $items = $r;
144
145         $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
146
147         $atom = '';
148
149         $hubxml = feed_hublinks();
150
151         $salmon = feed_salmonlinks($owner_nick);
152
153         $atom .= replace_macros($feed_template, array(
154                 '$version'      => xmlify(FRIENDICA_VERSION),
155                 '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
156                 '$feed_title'   => xmlify($owner['name']),
157                 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
158                 '$hub'          => $hubxml,
159                 '$salmon'       => $salmon,
160                 '$name'         => xmlify($owner['name']),
161                 '$profile_page' => xmlify($owner['url']),
162                 '$photo'        => xmlify($owner['photo']),
163                 '$thumb'        => xmlify($owner['thumb']),
164                 '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
165                 '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
166                 '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) , 
167                 '$birthday'     => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
168                 '$community'    => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
169         ));
170
171         call_hooks('atom_feed', $atom);
172
173         if(! count($items)) {
174
175                 call_hooks('atom_feed_end', $atom);
176
177                 $atom .= '</feed>' . "\r\n";
178                 return $atom;
179         }
180
181         foreach($items as $item) {
182
183                 // prevent private email from leaking.
184                 if($item['network'] === NETWORK_MAIL)
185                         continue;
186
187                 // public feeds get html, our own nodes use bbcode
188
189                 if($public_feed) {
190                         $type = 'html';
191                         // catch any email that's in a public conversation and make sure it doesn't leak
192                         if($item['private'])
193                                 continue;
194                 }
195                 else {
196                         $type = 'text';
197                 }
198
199                 $atom .= atom_entry($item,$type,null,$owner,true);
200         }
201
202         call_hooks('atom_feed_end', $atom);
203
204         $atom .= '</feed>' . "\r\n";
205
206         return $atom;
207 }
208
209
210 function construct_verb($item) {
211         if($item['verb'])
212                 return $item['verb'];
213         return ACTIVITY_POST;
214 }
215
216 function construct_activity_object($item) {
217
218         if($item['object']) {
219                 $o = '<as:object>' . "\r\n";
220                 $r = parse_xml_string($item['object'],false);
221
222
223                 if(! $r)
224                         return '';
225                 if($r->type)
226                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
227                 if($r->id)
228                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
229                 if($r->title)
230                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
231                 if($r->link) {
232                         if(substr($r->link,0,1) === '<') {
233                                 // patch up some facebook "like" activity objects that got stored incorrectly
234                                 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
235                                 // we can probably remove this hack here and in the following function in a few months time.
236                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
237                                         $r->link = str_replace('&','&amp;', $r->link);
238                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
239                                 $o .= $r->link;
240                         }                                       
241                         else
242                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
243                 }
244                 if($r->content)
245                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
246                 $o .= '</as:object>' . "\r\n";
247                 return $o;
248         }
249
250         return '';
251
252
253 function construct_activity_target($item) {
254
255         if($item['target']) {
256                 $o = '<as:target>' . "\r\n";
257                 $r = parse_xml_string($item['target'],false);
258                 if(! $r)
259                         return '';
260                 if($r->type)
261                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
262                 if($r->id)
263                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
264                 if($r->title)
265                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
266                 if($r->link) {
267                         if(substr($r->link,0,1) === '<') {
268                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
269                                         $r->link = str_replace('&','&amp;', $r->link);
270                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
271                                 $o .= $r->link;
272                         }                                       
273                         else
274                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
275                 }
276                 if($r->content)
277                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
278                 $o .= '</as:target>' . "\r\n";
279                 return $o;
280         }
281
282         return '';
283 }
284
285 /* limit_body_size()
286  *
287  *              The purpose of this function is to apply system message length limits to
288  *              imported messages without including any embedded photos in the length
289  */
290 if(! function_exists('limit_body_size')) {
291 function limit_body_size($body) {
292
293         logger('limit_body_size: start', LOGGER_DEBUG);
294
295         $maxlen = get_max_import_size();
296
297         // If the length of the body, including the embedded images, is smaller
298         // than the maximum, then don't waste time looking for the images
299         if($maxlen && (strlen($body) > $maxlen)) {
300
301                 logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
302
303                 $orig_body = $body;
304                 $new_body = '';
305                 $textlen = 0;
306                 $max_found = false;
307
308                 $img_start = strpos($orig_body, '[img');
309                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
310                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
311                 while(($img_st_close !== false) && ($img_end !== false)) {
312
313                         $img_st_close++; // make it point to AFTER the closing bracket
314                         $img_end += $img_start;
315                         $img_end += strlen('[/img]');
316
317                         if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
318                                 // This is an embedded image
319
320                                 if( ($textlen + $img_start) > $maxlen ) {
321                                         if($textlen < $maxlen) {
322                                                 logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
323                                                 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
324                                                 $textlen = $maxlen;
325                                         }
326                                 }
327                                 else {
328                                         $new_body = $new_body . substr($orig_body, 0, $img_start);
329                                         $textlen += $img_start;
330                                 }
331
332                                 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
333                         }
334                         else {
335
336                                 if( ($textlen + $img_end) > $maxlen ) {
337                                         if($textlen < $maxlen) {
338                                                 logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
339                                                 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
340                                                 $textlen = $maxlen;
341                                         }
342                                 }
343                                 else {
344                                         $new_body = $new_body . substr($orig_body, 0, $img_end);
345                                         $textlen += $img_end;
346                                 }
347                         }
348                         $orig_body = substr($orig_body, $img_end);
349
350                         if($orig_body === false) // in case the body ends on a closing image tag
351                                 $orig_body = '';
352
353                         $img_start = strpos($orig_body, '[img');
354                         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
355                         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
356                 }
357
358                 if( ($textlen + strlen($orig_body)) > $maxlen) {
359                         if($textlen < $maxlen) {
360                                 logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
361                                 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
362                                 $textlen = $maxlen;
363                         }
364                 }
365                 else {
366                         logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
367                         $new_body = $new_body . $orig_body;
368                         $textlen += strlen($orig_body);
369                 }
370
371                 return $new_body;
372         }
373         else
374                 return $body;
375 }}
376
377 function title_is_body($title, $body) {
378
379         $title = strip_tags($title);
380         $title = trim($title);
381         $title = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $title);
382
383         $body = strip_tags($body);
384         $body = trim($body);
385         $body = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $body);
386
387         if (strlen($title) < strlen($body))
388                 $body = substr($body, 0, strlen($title));
389
390         if (($title != $body) and (substr($title, -3) == "...")) {
391                 $pos = strrpos($title, "...");
392                 if ($pos > 0) {
393                         $title = substr($title, 0, $pos);
394                         $body = substr($body, 0, $pos);
395                 }
396         }
397
398         return($title == $body);
399 }
400
401
402
403 function get_atom_elements($feed,$item) {
404
405         require_once('library/HTMLPurifier.auto.php');
406         require_once('include/html2bbcode.php');
407
408         $best_photo = array();
409
410         $res = array();
411
412         $author = $item->get_author();
413         if($author) { 
414                 $res['author-name'] = unxmlify($author->get_name());
415                 $res['author-link'] = unxmlify($author->get_link());
416         }
417         else {
418                 $res['author-name'] = unxmlify($feed->get_title());
419                 $res['author-link'] = unxmlify($feed->get_permalink());
420         }
421         $res['uri'] = unxmlify($item->get_id());
422         $res['title'] = unxmlify($item->get_title());
423         $res['body'] = unxmlify($item->get_content());
424         $res['plink'] = unxmlify($item->get_link(0));
425
426         // removing the content of the title if its identically to the body
427         // This helps with auto generated titles e.g. from tumblr
428         if (title_is_body($res["title"], $res["body"]))
429                 $res['title'] = "";
430
431         if($res['plink'])
432                 $base_url = implode('/', array_slice(explode('/',$res['plink']),0,3));
433         else
434                 $base_url = '';
435
436         // look for a photo. We should check media size and find the best one,
437         // but for now let's just find any author photo
438
439         $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
440
441         if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
442                 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
443                 foreach($base as $link) {
444                         if(!x($res, 'author-avatar') || !$res['author-avatar']) {
445                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
446                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
447                         }
448                 }
449         }
450
451         $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
452
453         if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
454                 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
455                 if($base && count($base)) {
456                         foreach($base as $link) {
457                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
458                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
459                                 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
460                                         if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
461                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
462                                 }
463                         }
464                 }
465         }
466
467         // No photo/profile-link on the item - look at the feed level
468
469         if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
470                 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
471                 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
472                         $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
473                         foreach($base as $link) {
474                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
475                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
476                                 if(! $res['author-avatar']) {
477                                         if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
478                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
479                                 }
480                         }
481                 }
482
483                 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
484
485                 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
486                         $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
487
488                         if($base && count($base)) {
489                                 foreach($base as $link) {
490                                         if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
491                                                 $res['author-link'] = unxmlify($link['attribs']['']['href']);
492                                         if(! (x($res,'author-avatar'))) {
493                                                 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
494                                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
495                                         }
496                                 }
497                         }
498                 }
499         }
500
501         $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
502         if($apps && $apps[0]['attribs']['']['source']) {
503                 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
504                 if($res['app'] === 'web')
505                         $res['app'] = 'OStatus';
506         }
507
508         // base64 encoded json structure representing Diaspora signature
509
510         $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
511         if($dsig) {
512                 $res['dsprsig'] = unxmlify($dsig[0]['data']);
513         }
514
515         $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
516         if($dguid)
517                 $res['guid'] = unxmlify($dguid[0]['data']);
518
519         $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
520         if($bm)
521                 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
522
523
524         /**
525          * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
526          */
527
528         $have_real_body = false;
529
530         $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
531         if($rawenv) {
532                 $have_real_body = true;
533                 $res['body'] = $rawenv[0]['data'];
534                 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
535                 // make sure nobody is trying to sneak some html tags by us
536                 $res['body'] = notags(base64url_decode($res['body']));
537         }
538
539         
540         $res['body'] = limit_body_size($res['body']);
541
542         // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
543         // the content type. Our own network only emits text normally, though it might have been converted to 
544         // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
545         // have to assume it is all html and needs to be purified.
546
547         // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
548         // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
549         // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
550         // html.
551
552         if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
553
554                 $res['body'] = reltoabs($res['body'],$base_url);
555
556                 $res['body'] = html2bb_video($res['body']);
557
558                 $res['body'] = oembed_html2bbcode($res['body']);
559
560                 $config = HTMLPurifier_Config::createDefault();
561                 $config->set('Cache.DefinitionImpl', null);
562
563                 // we shouldn't need a whitelist, because the bbcode converter
564                 // will strip out any unsupported tags.
565
566                 $purifier = new HTMLPurifier($config);
567                 $res['body'] = $purifier->purify($res['body']);
568
569                 $res['body'] = @html2bbcode($res['body']);
570
571
572         }
573         elseif(! $have_real_body) {
574
575                 // it's not one of our messages and it has no tags
576                 // so it's probably just text. We'll escape it just to be safe.
577
578                 $res['body'] = escape_tags($res['body']);
579         }
580
581         // this tag is obsolete but we keep it for really old sites
582
583         $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
584         if($allow && $allow[0]['data'] == 1)
585                 $res['last-child'] = 1;
586         else
587                 $res['last-child'] = 0;
588
589         $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
590         if($private && intval($private[0]['data']) > 0)
591                 $res['private'] = intval($private[0]['data']);
592         else
593                 $res['private'] = 0;
594
595         $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
596         if($extid && $extid[0]['data'])
597                 $res['extid'] = $extid[0]['data'];
598
599         $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
600         if($rawlocation)
601                 $res['location'] = unxmlify($rawlocation[0]['data']);
602
603
604         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
605         if($rawcreated)
606                 $res['created'] = unxmlify($rawcreated[0]['data']);
607
608
609         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
610         if($rawedited)
611                 $res['edited'] = unxmlify($rawedited[0]['data']);
612
613         if((x($res,'edited')) && (! (x($res,'created'))))
614                 $res['created'] = $res['edited']; 
615
616         if(! $res['created'])
617                 $res['created'] = $item->get_date('c');
618
619         if(! $res['edited'])
620                 $res['edited'] = $item->get_date('c');
621
622
623         // Disallow time travelling posts
624
625         $d1 = strtotime($res['created']);
626         $d2 = strtotime($res['edited']);
627         $d3 = strtotime('now');
628
629         if($d1 > $d3)
630                 $res['created'] = datetime_convert();
631         if($d2 > $d3)
632                 $res['edited'] = datetime_convert();
633
634         $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
635         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
636                 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
637         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
638                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
639         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
640                 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
641         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
642                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
643
644         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
645                 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
646
647                 foreach($base as $link) {
648                         if(!x($res, 'owner-avatar') || !$res['owner-avatar']) {
649                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
650                                         $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
651                         }
652                 }
653         }
654
655         $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
656         if($rawgeo)
657                 $res['coord'] = unxmlify($rawgeo[0]['data']);
658
659
660         $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
661
662         // select between supported verbs
663
664         if($rawverb) {
665                 $res['verb'] = unxmlify($rawverb[0]['data']);
666         }
667
668         // translate OStatus unfollow to activity streams if it happened to get selected
669                 
670         if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
671                 $res['verb'] = ACTIVITY_UNFOLLOW;
672
673         $cats = $item->get_categories();
674         if($cats) {
675                 $tag_arr = array();
676                 foreach($cats as $cat) {
677                         $term = $cat->get_term();
678                         if(! $term)
679                                 $term = $cat->get_label();
680                         $scheme = $cat->get_scheme();
681                         if($scheme && $term && stristr($scheme,'X-DFRN:'))
682                                 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
683                         elseif($term)
684                                 $tag_arr[] = notags(trim($term));
685                 }
686                 $res['tag'] =  implode(',', $tag_arr);
687         }
688
689         $attach = $item->get_enclosures();
690         if($attach) {
691                 $att_arr = array();
692                 foreach($attach as $att) {
693                         $len   = intval($att->get_length());
694                         $link  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
695                         $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
696                         $type  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
697                         if(strpos($type,';'))
698                                 $type = substr($type,0,strpos($type,';'));
699                         if((! $link) || (strpos($link,'http') !== 0))
700                                 continue;
701
702                         if(! $title)
703                                 $title = ' ';
704                         if(! $type)
705                                 $type = 'application/octet-stream';
706
707                         $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]'; 
708                 }
709                 $res['attach'] = implode(',', $att_arr);
710         }
711
712         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
713
714         if($rawobj) {
715                 $res['object'] = '<object>' . "\n";
716                 $child = $rawobj[0]['child'];
717                 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
718                         $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
719                         $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
720                 }       
721                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
722                         $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
723                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
724                         $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
725                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
726                         $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
727                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
728                         $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
729                         if(! $body)
730                                 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
731                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
732                         $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
733                         if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
734
735                                 $body = html2bb_video($body);
736
737                                 $config = HTMLPurifier_Config::createDefault();
738                                 $config->set('Cache.DefinitionImpl', null);
739
740                                 $purifier = new HTMLPurifier($config);
741                                 $body = $purifier->purify($body);
742                                 $body = html2bbcode($body);
743                         }
744
745                         $res['object'] .= '<content>' . $body . '</content>' . "\n";
746                 }
747
748                 $res['object'] .= '</object>' . "\n";
749         }
750
751         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
752
753         if($rawobj) {
754                 $res['target'] = '<target>' . "\n";
755                 $child = $rawobj[0]['child'];
756                 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
757                         $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
758                 }       
759                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
760                         $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
761                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
762                         $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
763                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
764                         $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
765                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
766                         $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
767                         if(! $body)
768                                 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
769                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
770                         $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
771                         if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
772
773                                 $body = html2bb_video($body);
774
775                                 $config = HTMLPurifier_Config::createDefault();
776                                 $config->set('Cache.DefinitionImpl', null);
777
778                                 $purifier = new HTMLPurifier($config);
779                                 $body = $purifier->purify($body);
780                                 $body = html2bbcode($body);
781                         }
782
783                         $res['target'] .= '<content>' . $body . '</content>' . "\n";
784                 }
785
786                 $res['target'] .= '</target>' . "\n";
787         }
788
789         $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
790
791         call_hooks('parse_atom', $arr);
792
793         //if (($res["title"] != "") or (strpos($res["body"], "RT @") > 0)) {
794         if (strpos($res["body"], "RT @") !== false) {
795                 $debugfile = tempnam("/home/ike/log", "item-res2-");
796                 file_put_contents($debugfile, serialize($arr));
797         }
798
799         return $res;
800 }
801
802 function encode_rel_links($links) {
803         $o = '';
804         if(! ((is_array($links)) && (count($links))))
805                 return $o;
806         foreach($links as $link) {
807                 $o .= '<link ';
808                 if($link['attribs']['']['rel'])
809                         $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
810                 if($link['attribs']['']['type'])
811                         $o .= 'type="' . $link['attribs']['']['type'] . '" ';
812                 if($link['attribs']['']['href'])
813                         $o .= 'href="' . $link['attribs']['']['href'] . '" ';
814                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
815                         $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
816                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
817                         $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
818                 $o .= ' />' . "\n" ;
819         }
820         return xmlify($o);
821 }
822
823
824
825 function item_store($arr,$force_parent = false) {
826
827         // If a Diaspora signature structure was passed in, pull it out of the 
828         // item array and set it aside for later storage.
829
830         $dsprsig = null;
831         if(x($arr,'dsprsig')) {
832                 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
833                 unset($arr['dsprsig']);
834         }
835
836         if(x($arr, 'gravity'))
837                 $arr['gravity'] = intval($arr['gravity']);
838         elseif($arr['parent-uri'] === $arr['uri'])
839                 $arr['gravity'] = 0;
840         elseif(activity_match($arr['verb'],ACTIVITY_POST))
841                 $arr['gravity'] = 6;
842         else      
843                 $arr['gravity'] = 6;   // extensible catchall
844
845         if(! x($arr,'type'))
846                 $arr['type']      = 'remote';
847
848         // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
849
850         if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) 
851                 $arr['body'] = strip_tags($arr['body']);
852
853         require_once('Text/LanguageDetect.php');
854         $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
855         $l = new Text_LanguageDetect;
856         $lng = $l->detectConfidence($naked_body);
857         $arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
858
859
860         $arr['wall']          = ((x($arr,'wall'))          ? intval($arr['wall'])                : 0);
861         $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : random_string());
862         $arr['extid']         = ((x($arr,'extid'))         ? notags(trim($arr['extid']))         : '');
863         $arr['author-name']   = ((x($arr,'author-name'))   ? notags(trim($arr['author-name']))   : '');
864         $arr['author-link']   = ((x($arr,'author-link'))   ? notags(trim($arr['author-link']))   : '');
865         $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
866         $arr['owner-name']    = ((x($arr,'owner-name'))    ? notags(trim($arr['owner-name']))    : '');
867         $arr['owner-link']    = ((x($arr,'owner-link'))    ? notags(trim($arr['owner-link']))    : '');
868         $arr['owner-avatar']  = ((x($arr,'owner-avatar'))  ? notags(trim($arr['owner-avatar']))  : '');
869         $arr['created']       = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
870         $arr['edited']        = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
871         $arr['commented']     = datetime_convert();
872         $arr['received']      = datetime_convert();
873         $arr['changed']       = datetime_convert();
874         $arr['title']         = ((x($arr,'title'))         ? notags(trim($arr['title']))         : '');
875         $arr['location']      = ((x($arr,'location'))      ? notags(trim($arr['location']))      : '');
876         $arr['coord']         = ((x($arr,'coord'))         ? notags(trim($arr['coord']))         : '');
877         $arr['last-child']    = ((x($arr,'last-child'))    ? intval($arr['last-child'])          : 0 );
878         $arr['visible']       = ((x($arr,'visible') !== false) ? intval($arr['visible'])         : 1 );
879         $arr['deleted']       = 0;
880         $arr['parent-uri']    = ((x($arr,'parent-uri'))    ? notags(trim($arr['parent-uri']))    : '');
881         $arr['verb']          = ((x($arr,'verb'))          ? notags(trim($arr['verb']))          : '');
882         $arr['object-type']   = ((x($arr,'object-type'))   ? notags(trim($arr['object-type']))   : '');
883         $arr['object']        = ((x($arr,'object'))        ? trim($arr['object'])                : '');
884         $arr['target-type']   = ((x($arr,'target-type'))   ? notags(trim($arr['target-type']))   : '');
885         $arr['target']        = ((x($arr,'target'))        ? trim($arr['target'])                : '');
886         $arr['plink']         = ((x($arr,'plink'))         ? notags(trim($arr['plink']))         : '');
887         $arr['allow_cid']     = ((x($arr,'allow_cid'))     ? trim($arr['allow_cid'])             : '');
888         $arr['allow_gid']     = ((x($arr,'allow_gid'))     ? trim($arr['allow_gid'])             : '');
889         $arr['deny_cid']      = ((x($arr,'deny_cid'))      ? trim($arr['deny_cid'])              : '');
890         $arr['deny_gid']      = ((x($arr,'deny_gid'))      ? trim($arr['deny_gid'])              : '');
891         $arr['private']       = ((x($arr,'private'))       ? intval($arr['private'])             : 0 );
892         $arr['bookmark']      = ((x($arr,'bookmark'))      ? intval($arr['bookmark'])            : 0 );
893         $arr['body']          = ((x($arr,'body'))          ? trim($arr['body'])                  : '');
894         $arr['tag']           = ((x($arr,'tag'))           ? notags(trim($arr['tag']))           : '');
895         $arr['attach']        = ((x($arr,'attach'))        ? notags(trim($arr['attach']))        : '');
896         $arr['app']           = ((x($arr,'app'))           ? notags(trim($arr['app']))           : '');
897         $arr['origin']        = ((x($arr,'origin'))        ? intval($arr['origin'])              : 0 );
898         $arr['guid']          = ((x($arr,'guid'))          ? notags(trim($arr['guid']))          : get_guid());
899
900         if($arr['parent-uri'] === $arr['uri']) {
901                 $parent_id = 0;
902                 $parent_deleted = 0;
903                 $allow_cid = $arr['allow_cid'];
904                 $allow_gid = $arr['allow_gid'];
905                 $deny_cid  = $arr['deny_cid'];
906                 $deny_gid  = $arr['deny_gid'];
907         }
908         else { 
909
910                 // find the parent and snarf the item id and ACL's
911                 // and anything else we need to inherit
912
913                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
914                         dbesc($arr['parent-uri']),
915                         intval($arr['uid'])
916                 );
917
918                 if(count($r)) {
919
920                         // is the new message multi-level threaded?
921                         // even though we don't support it now, preserve the info
922                         // and re-attach to the conversation parent.
923
924                         if($r[0]['uri'] != $r[0]['parent-uri']) {
925                                 $arr['thr-parent'] = $arr['parent-uri'];
926                                 $arr['parent-uri'] = $r[0]['parent-uri'];
927                                 $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d 
928                                         ORDER BY `id` ASC LIMIT 1",
929                                         dbesc($r[0]['parent-uri']),
930                                         dbesc($r[0]['parent-uri']),
931                                         intval($arr['uid'])
932                                 );
933                                 if($z && count($z))
934                                         $r = $z;
935                         }
936
937                         $parent_id      = $r[0]['id'];
938                         $parent_deleted = $r[0]['deleted'];
939                         $allow_cid      = $r[0]['allow_cid'];
940                         $allow_gid      = $r[0]['allow_gid'];
941                         $deny_cid       = $r[0]['deny_cid'];
942                         $deny_gid       = $r[0]['deny_gid'];
943                         $arr['wall']    = $r[0]['wall'];
944
945                         // if the parent is private, force privacy for the entire conversation
946                         // This differs from the above settings as it subtly allows comments from 
947                         // email correspondents to be private even if the overall thread is not. 
948
949                         if($r[0]['private'])
950                                 $arr['private'] = $r[0]['private'];
951
952                         // Edge case. We host a public forum that was originally posted to privately.
953                         // The original author commented, but as this is a comment, the permissions
954                         // weren't fixed up so it will still show the comment as private unless we fix it here. 
955
956                         if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
957                                 $arr['private'] = 0;
958                 }
959                 else {
960
961                         // Allow one to see reply tweets from status.net even when
962                         // we don't have or can't see the original post.
963
964                         if($force_parent) {
965                                 logger('item_store: $force_parent=true, reply converted to top-level post.');
966                                 $parent_id = 0;
967                                 $arr['thr-parent'] = $arr['parent-uri'];
968                                 $arr['parent-uri'] = $arr['uri'];
969                                 $arr['gravity'] = 0;
970                         }
971                         else {
972                                 logger('item_store: item parent was not found - ignoring item');
973                                 return 0;
974                         }
975                         
976                         $parent_deleted = 0;
977                 }
978         }
979
980         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
981                 dbesc($arr['uri']),
982                 intval($arr['uid'])
983         );
984         if($r && count($r)) {
985                 logger('item-store: duplicate item ignored. ' . print_r($arr,true));
986                 return 0;
987         }
988
989         call_hooks('post_remote',$arr);
990
991         if(x($arr,'cancel')) {
992                 logger('item_store: post cancelled by plugin.');
993                 return 0;
994         }
995
996         dbesc_array($arr);
997
998         logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
999
1000         $r = dbq("INSERT INTO `item` (`" 
1001                         . implode("`, `", array_keys($arr)) 
1002                         . "`) VALUES ('" 
1003                         . implode("', '", array_values($arr)) 
1004                         . "')" );
1005
1006         // find the item we just created
1007
1008         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC ",
1009                 $arr['uri'],           // already dbesc'd
1010                 intval($arr['uid'])
1011         );
1012
1013         if(count($r)) {
1014                 $current_post = $r[0]['id'];
1015                 logger('item_store: created item ' . $current_post);
1016         }
1017         else {
1018                 logger('item_store: could not locate created item');
1019                 return 0;
1020         }
1021         if(count($r) > 1) {
1022                 logger('item_store: duplicated post occurred. Removing duplicates.');
1023                 q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ",
1024                         $arr['uri'],
1025                         intval($arr['uid']),
1026                         intval($current_post)
1027                 );
1028         }
1029
1030         if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))      
1031                 $parent_id = $current_post;
1032
1033         if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
1034                 $private = 1;
1035         else
1036                 $private = $arr['private']; 
1037
1038         // Set parent id - and also make sure to inherit the parent's ACL's.
1039
1040         $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
1041                 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
1042                 intval($parent_id),
1043                 dbesc($allow_cid),
1044                 dbesc($allow_gid),
1045                 dbesc($deny_cid),
1046                 dbesc($deny_gid),
1047                 intval($private),
1048                 intval($parent_deleted),
1049                 intval($current_post)
1050         );
1051
1052         $arr['id'] = $current_post;
1053         $arr['parent'] = $parent_id;
1054         $arr['allow_cid'] = $allow_cid;
1055         $arr['allow_gid'] = $allow_gid;
1056         $arr['deny_cid'] = $deny_cid;
1057         $arr['deny_gid'] = $deny_gid;
1058         $arr['private'] = $private;
1059         $arr['deleted'] = $parent_deleted;
1060         call_hooks('post_remote_end',$arr);
1061
1062         // update the commented timestamp on the parent
1063
1064         q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1065                 dbesc(datetime_convert()),
1066                 dbesc(datetime_convert()),
1067                 intval($parent_id)
1068         );
1069
1070         if($dsprsig) {
1071                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1072                         intval($current_post),
1073                         dbesc($dsprsig->signed_text),
1074                         dbesc($dsprsig->signature),
1075                         dbesc($dsprsig->signer)
1076                 );
1077         }
1078
1079
1080         /**
1081          * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
1082          */
1083
1084         if($arr['last-child']) {
1085                 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
1086                         dbesc($arr['uri']),
1087                         intval($arr['uid']),
1088                         intval($current_post)
1089                 );
1090         }
1091
1092         tag_deliver($arr['uid'],$current_post);
1093
1094         return $current_post;
1095 }
1096
1097 function get_item_contact($item,$contacts) {
1098         if(! count($contacts) || (! is_array($item)))
1099                 return false;
1100         foreach($contacts as $contact) {
1101                 if($contact['id'] == $item['contact-id']) {
1102                         return $contact;
1103                         break; // NOTREACHED
1104                 }
1105         }
1106         return false;
1107 }
1108
1109
1110 function tag_deliver($uid,$item_id) {
1111
1112         // look for mention tags and setup a second delivery chain for forum/community posts if appropriate
1113
1114         $a = get_app();
1115
1116         $mention = false;
1117
1118         $u = q("select * from user where uid = %d limit 1",
1119                 intval($uid)
1120         );
1121         if(! count($u))
1122                 return;
1123
1124         $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1125         $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1126
1127
1128         $i = q("select * from item where id = %d and uid = %d limit 1",
1129                 intval($item_id),
1130                 intval($uid)
1131         );
1132         if(! count($i))
1133                 return;
1134
1135         $item = $i[0];
1136
1137         $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1138
1139         // Diaspora uses their own hardwired link URL in @-tags
1140         // instead of the one we supply with webfinger
1141
1142         $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1143
1144         $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1145         if($cnt) {
1146                 foreach($matches as $mtch) {
1147                         if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1148                                 $mention = true;
1149                                 logger('tag_deliver: mention found: ' . $mtch[2]);
1150                         }
1151                 }
1152         }
1153
1154         if(! $mention)
1155                 return;
1156
1157         // send a notification
1158
1159         require_once('include/enotify.php');
1160         notification(array(
1161                 'type'         => NOTIFY_TAGSELF,
1162                 'notify_flags' => $u[0]['notify-flags'],
1163                 'language'     => $u[0]['language'],
1164                 'to_name'      => $u[0]['username'],
1165                 'to_email'     => $u[0]['email'],
1166                 'uid'          => $u[0]['uid'],
1167                 'item'         => $item,
1168                 'link'         => $a->get_baseurl() . '/display/' . $u[0]['nickname'] . '/' . $item['id'],
1169                 'source_name'  => $item['author-name'],
1170                 'source_link'  => $item['author-link'],
1171                 'source_photo' => $item['author-avatar'],
1172                 'verb'         => ACTIVITY_TAG,
1173                 'otype'        => 'item'
1174         ));
1175
1176         if((! $community_page) && (! $prvgroup))
1177                 return;
1178
1179
1180         // tgroup delivery - setup a second delivery chain
1181         // prevent delivery looping - only proceed
1182         // if the message originated elsewhere and is a top-level post
1183
1184         if(($item['wall']) || ($item['origin']) || ($item['id'] != $item['parent']))
1185                 return;
1186
1187         // now change this copy of the post to a forum head message and deliver to all the tgroup members
1188
1189
1190         $c = q("select name, url, thumb from contact where self = 1 and uid = %d limit 1",
1191                 intval($u[0]['uid'])
1192         );
1193         if(! count($c))
1194                 return;
1195
1196         // also reset all the privacy bits to the forum default permissions
1197
1198         $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
1199
1200         $forum_mode = (($prvgroup) ? 2 : 1);
1201
1202         q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s', 
1203                 `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'  where id = %d limit 1",
1204                 intval($forum_mode),
1205                 dbesc($c[0]['name']),
1206                 dbesc($c[0]['url']),
1207                 dbesc($c[0]['thumb']),
1208                 intval($private),
1209                 dbesc($u[0]['allow_cid']),
1210                 dbesc($u[0]['allow_gid']),
1211                 dbesc($u[0]['deny_cid']),
1212                 dbesc($u[0]['deny_gid']),
1213                 intval($item_id)
1214         );
1215
1216         proc_run('php','include/notifier.php','tgroup',$item_id);                       
1217
1218 }
1219
1220
1221
1222
1223
1224
1225 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
1226
1227         $a = get_app();
1228
1229         $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
1230
1231         if($contact['duplex'] && $contact['dfrn-id'])
1232                 $idtosend = '0:' . $orig_id;
1233         if($contact['duplex'] && $contact['issued-id'])
1234                 $idtosend = '1:' . $orig_id;            
1235
1236         $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
1237
1238         $rino_enable = get_config('system','rino_encrypt');
1239
1240         if(! $rino_enable)
1241                 $rino = 0;
1242
1243         $ssl_val = intval(get_config('system','ssl_policy'));
1244         $ssl_policy = '';
1245
1246         switch($ssl_val){
1247                 case SSL_POLICY_FULL:
1248                         $ssl_policy = 'full';
1249                         break;
1250                 case SSL_POLICY_SELFSIGN:
1251                         $ssl_policy = 'self';
1252                         break;                  
1253                 case SSL_POLICY_NONE:
1254                 default:
1255                         $ssl_policy = 'none';
1256                         break;
1257         }
1258
1259         $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
1260
1261         logger('dfrn_deliver: ' . $url);
1262
1263         $xml = fetch_url($url);
1264
1265         $curl_stat = $a->get_curl_code();
1266         if(! $curl_stat)
1267                 return(-1); // timed out
1268
1269         logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
1270
1271         if(! $xml)
1272                 return 3;
1273
1274         if(strpos($xml,'<?xml') === false) {
1275                 logger('dfrn_deliver: no valid XML returned');
1276                 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
1277                 return 3;
1278         }
1279
1280         $res = parse_xml_string($xml);
1281
1282         if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
1283                 return (($res->status) ? $res->status : 3);
1284
1285         $postvars     = array();
1286         $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
1287         $challenge    = hex2bin((string) $res->challenge);
1288         $perm         = (($res->perm) ? $res->perm : null);
1289         $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
1290         $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
1291         $page         = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
1292
1293         if($owner['page-flags'] == PAGE_PRVGROUP)
1294                 $page = 2;
1295
1296         $final_dfrn_id = '';
1297
1298         if($perm) {
1299                 if((($perm == 'rw') && (! intval($contact['writable']))) 
1300                 || (($perm == 'r') && (intval($contact['writable'])))) {
1301                         q("update contact set writable = %d where id = %d limit 1",
1302                                 intval(($perm == 'rw') ? 1 : 0),
1303                                 intval($contact['id'])
1304                         );
1305                         $contact['writable'] = (string) 1 - intval($contact['writable']);                       
1306                 }
1307         }
1308
1309         if(($contact['duplex'] && strlen($contact['pubkey'])) 
1310                 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1311                 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1312                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
1313                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
1314         }
1315         else {
1316                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
1317                 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
1318         }
1319
1320         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
1321
1322         if(strpos($final_dfrn_id,':') == 1)
1323                 $final_dfrn_id = substr($final_dfrn_id,2);
1324
1325         if($final_dfrn_id != $orig_id) {
1326                 logger('dfrn_deliver: wrong dfrn_id.');
1327                 // did not decode properly - cannot trust this site 
1328                 return 3;
1329         }
1330
1331         $postvars['dfrn_id']      = $idtosend;
1332         $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
1333         if($dissolve)
1334                 $postvars['dissolve'] = '1';
1335
1336
1337         if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1338                 $postvars['data'] = $atom;
1339                 $postvars['perm'] = 'rw';
1340         }
1341         else {
1342                 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1343                 $postvars['perm'] = 'r';
1344         }
1345
1346         $postvars['ssl_policy'] = $ssl_policy;
1347
1348         if($page)
1349                 $postvars['page'] = $page;
1350         
1351         if($rino && $rino_allowed && (! $dissolve)) {
1352                 $key = substr(random_string(),0,16);
1353                 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1354                 $postvars['data'] = $data;
1355                 logger('rino: sent key = ' . $key, LOGGER_DEBUG);       
1356
1357
1358                 if($dfrn_version >= 2.1) {      
1359                         if(($contact['duplex'] && strlen($contact['pubkey'])) 
1360                                 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1361                                 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1362
1363                                 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1364                         }
1365                         else {
1366                                 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1367                         }
1368                 }
1369                 else {
1370                         if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1371                                 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1372                         }
1373                         else {
1374                                 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1375                         }
1376                 }
1377
1378                 logger('md5 rawkey ' . md5($postvars['key']));
1379
1380                 $postvars['key'] = bin2hex($postvars['key']);
1381         }
1382
1383         logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1384
1385         $xml = post_url($contact['notify'],$postvars);
1386
1387         logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1388
1389         $curl_stat = $a->get_curl_code();
1390         if((! $curl_stat) || (! strlen($xml)))
1391                 return(-1); // timed out
1392
1393         if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
1394                 return(-1);
1395
1396         if(strpos($xml,'<?xml') === false) {
1397                 logger('dfrn_deliver: phase 2: no valid XML returned');
1398                 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1399                 return 3;
1400         }
1401
1402         if($contact['term-date'] != '0000-00-00 00:00:00') {
1403                 logger("dfrn_deliver: $url back from the dead - removing mark for death");
1404                 require_once('include/Contact.php');
1405                 unmark_for_death($contact);
1406         }
1407
1408         $res = parse_xml_string($xml);
1409
1410         return $res->status; 
1411 }
1412
1413
1414 /**
1415  *
1416  * consume_feed - process atom feed and update anything/everything we might need to update
1417  *
1418  * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1419  *
1420  * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1421  *             It is this person's stuff that is going to be updated.
1422  * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1423  *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST 
1424  *             have a contact record.
1425  * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or 
1426  *        might not) try and subscribe to it.
1427  * $datedir sorts in reverse order
1428  * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been 
1429  *      imported prior to its children being seen in the stream unless we are certain
1430  *      of how the feed is arranged/ordered.
1431  * With $pass = 1, we only pull parent items out of the stream.
1432  * With $pass = 2, we only pull children (comments/likes).
1433  *
1434  * So running this twice, first with pass 1 and then with pass 2 will do the right
1435  * thing regardless of feed ordering. This won't be adequate in a fully-threaded
1436  * model where comments can have sub-threads. That would require some massive sorting
1437  * to get all the feed items into a mostly linear ordering, and might still require
1438  * recursion.  
1439  */
1440
1441 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
1442
1443         require_once('library/simplepie/simplepie.inc');
1444
1445         if(! strlen($xml)) {
1446                 logger('consume_feed: empty input');
1447                 return;
1448         }
1449                 
1450         $feed = new SimplePie();
1451         $feed->set_raw_data($xml);
1452         if($datedir)
1453                 $feed->enable_order_by_date(true);
1454         else
1455                 $feed->enable_order_by_date(false);
1456         $feed->init();
1457
1458         if($feed->error())
1459                 logger('consume_feed: Error parsing XML: ' . $feed->error());
1460
1461         $permalink = $feed->get_permalink();
1462
1463         // Check at the feed level for updated contact name and/or photo
1464
1465         $name_updated  = '';
1466         $new_name = '';
1467         $photo_timestamp = '';
1468         $photo_url = '';
1469         $birthday = '';
1470
1471         $hubs = $feed->get_links('hub');
1472         logger('consume_feed: hubs: ' . print_r($hubs,true), LOGGER_DATA);
1473
1474         if(count($hubs))
1475                 $hub = implode(',', $hubs);
1476
1477         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1478         if(! $rawtags)
1479                 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1480         if($rawtags) {
1481                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1482                 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1483                         $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1484                         $new_name = $elems['name'][0]['data'];
1485                 } 
1486                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1487                         $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1488                         $photo_url = $elems['link'][0]['attribs']['']['href'];
1489                 }
1490
1491                 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1492                         $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1493                 }
1494         }
1495
1496         if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1497                 logger('consume_feed: Updating photo for ' . $contact['name']);
1498                 require_once("Photo.php");
1499                 $photo_failure = false;
1500                 $have_photo = false;
1501
1502                 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1503                         intval($contact['id']),
1504                         intval($contact['uid'])
1505                 );
1506                 if(count($r)) {
1507                         $resource_id = $r[0]['resource-id'];
1508                         $have_photo = true;
1509                 }
1510                 else {
1511                         $resource_id = photo_new_resource();
1512                 }
1513                         
1514                 $img_str = fetch_url($photo_url,true);
1515                 // guess mimetype from headers or filename
1516                 $type = guess_image_type($photo_url,true);
1517                 
1518                 
1519                 $img = new Photo($img_str, $type);
1520                 if($img->is_valid()) {
1521                         if($have_photo) {
1522                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1523                                         dbesc($resource_id),
1524                                         intval($contact['id']),
1525                                         intval($contact['uid'])
1526                                 );
1527                         }
1528                                 
1529                         $img->scaleImageSquare(175);
1530                                 
1531                         $hash = $resource_id;
1532                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1533                                 
1534                         $img->scaleImage(80);
1535                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1536
1537                         $img->scaleImage(48);
1538                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1539
1540                         $a = get_app();
1541
1542                         q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'  
1543                                 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1544                                 dbesc(datetime_convert()),
1545                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
1546                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
1547                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
1548                                 intval($contact['uid']),
1549                                 intval($contact['id'])
1550                         );
1551                 }
1552         }
1553
1554         if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1555                 $r = q("select * from contact where uid = %d and id = %d limit 1",
1556                         intval($contact['uid']),
1557                         intval($contact['id'])
1558                 );
1559
1560                 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1561                         dbesc(notags(trim($new_name))),
1562                         dbesc(datetime_convert()),
1563                         intval($contact['uid']),
1564                         intval($contact['id'])
1565                 );
1566
1567                 // do our best to update the name on content items
1568
1569                 if(count($r)) {
1570                         q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
1571                                 dbesc(notags(trim($new_name))),
1572                                 dbesc($r[0]['name']),
1573                                 dbesc($r[0]['url']),
1574                                 intval($contact['uid'])
1575                         );
1576                 }
1577         }
1578
1579         if(strlen($birthday)) {
1580                 if(substr($birthday,0,4) != $contact['bdyear']) {
1581                         logger('consume_feed: updating birthday: ' . $birthday);
1582
1583                         /**
1584                          *
1585                          * Add new birthday event for this person
1586                          *
1587                          * $bdtext is just a readable placeholder in case the event is shared
1588                          * with others. We will replace it during presentation to our $importer
1589                          * to contain a sparkle link and perhaps a photo. 
1590                          *
1591                          */
1592                          
1593                         $bdtext = sprintf( t('%s\'s birthday'), $contact['name']);
1594                         $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ) ;
1595
1596
1597                         $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
1598                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1599                                 intval($contact['uid']),
1600                                 intval($contact['id']),
1601                                 dbesc(datetime_convert()),
1602                                 dbesc(datetime_convert()),
1603                                 dbesc(datetime_convert('UTC','UTC', $birthday)),
1604                                 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1605                                 dbesc($bdtext),
1606                                 dbesc($bdtext2),
1607                                 dbesc('birthday')
1608                         );
1609                         
1610
1611                         // update bdyear
1612
1613                         q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1614                                 dbesc(substr($birthday,0,4)),
1615                                 intval($contact['uid']),
1616                                 intval($contact['id'])
1617                         );
1618
1619                         // This function is called twice without reloading the contact
1620                         // Make sure we only create one event. This is why &$contact 
1621                         // is a reference var in this function
1622
1623                         $contact['bdyear'] = substr($birthday,0,4);
1624                 }
1625
1626         }
1627
1628         $community_page = 0;
1629         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
1630         if($rawtags) {
1631                 $community_page = intval($rawtags[0]['data']);
1632         }
1633         if(is_array($contact) && intval($contact['forum']) != $community_page) {
1634                 q("update contact set forum = %d where id = %d limit 1",
1635                         intval($community_page),
1636                         intval($contact['id'])
1637                 );
1638                 $contact['forum'] = (string) $community_page;
1639         }
1640
1641
1642         // process any deleted entries
1643
1644         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1645         if(is_array($del_entries) && count($del_entries) && $pass != 2) {
1646                 foreach($del_entries as $dentry) {
1647                         $deleted = false;
1648                         if(isset($dentry['attribs']['']['ref'])) {
1649                                 $uri = $dentry['attribs']['']['ref'];
1650                                 $deleted = true;
1651                                 if(isset($dentry['attribs']['']['when'])) {
1652                                         $when = $dentry['attribs']['']['when'];
1653                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1654                                 }
1655                                 else
1656                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1657                         }
1658                         if($deleted && is_array($contact)) {
1659                                 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id` 
1660                                         WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
1661                                         dbesc($uri),
1662                                         intval($importer['uid']),
1663                                         intval($contact['id'])
1664                                 );
1665                                 if(count($r)) {
1666                                         $item = $r[0];
1667
1668                                         if(! $item['deleted'])
1669                                                 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1670
1671                                         if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
1672                                                 $xo = parse_xml_string($item['object'],false);
1673                                                 $xt = parse_xml_string($item['target'],false);
1674                                                 if($xt->type === ACTIVITY_OBJ_NOTE) {
1675                                                         $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
1676                                                                 dbesc($xt->id),
1677                                                                 intval($importer['importer_uid'])
1678                                                         );
1679                                                         if(count($i)) {
1680
1681                                                                 // For tags, the owner cannot remove the tag on the author's copy of the post.
1682
1683                                                                 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
1684                                                                 $author_remove = (($item['origin'] && $item['self']) ? true : false);
1685                                                                 $author_copy = (($item['origin']) ? true : false);
1686
1687                                                                 if($owner_remove && $author_copy)
1688                                                                         continue;
1689                                                                 if($author_remove || $owner_remove) {
1690                                                                         $tags = explode(',',$i[0]['tag']);
1691                                                                         $newtags = array();
1692                                                                         if(count($tags)) {
1693                                                                                 foreach($tags as $tag)
1694                                                                                         if(trim($tag) !== trim($xo->body))
1695                                                                                                 $newtags[] = trim($tag);
1696                                                                         }
1697                                                                         q("update item set tag = '%s' where id = %d limit 1",
1698                                                                                 dbesc(implode(',',$newtags)),
1699                                                                                 intval($i[0]['id'])
1700                                                                         );
1701                                                                 }
1702                                                         }
1703                                                 }
1704                                         }
1705
1706                                         if($item['uri'] == $item['parent-uri']) {
1707                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1708                                                         `body` = '', `title` = ''
1709                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
1710                                                         dbesc($when),
1711                                                         dbesc(datetime_convert()),
1712                                                         dbesc($item['uri']),
1713                                                         intval($importer['uid'])
1714                                                 );
1715                                         }
1716                                         else {
1717                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1718                                                         `body` = '', `title` = '' 
1719                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1720                                                         dbesc($when),
1721                                                         dbesc(datetime_convert()),
1722                                                         dbesc($uri),
1723                                                         intval($importer['uid'])
1724                                                 );
1725                                                 if($item['last-child']) {
1726                                                         // ensure that last-child is set in case the comment that had it just got wiped.
1727                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1728                                                                 dbesc(datetime_convert()),
1729                                                                 dbesc($item['parent-uri']),
1730                                                                 intval($item['uid'])
1731                                                         );
1732                                                         // who is the last child now? 
1733                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d 
1734                                                                 ORDER BY `created` DESC LIMIT 1",
1735                                                                         dbesc($item['parent-uri']),
1736                                                                         intval($importer['uid'])
1737                                                         );
1738                                                         if(count($r)) {
1739                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1740                                                                         intval($r[0]['id'])
1741                                                                 );
1742                                                         }
1743                                                 }       
1744                                         }
1745                                 }       
1746                         }
1747                 }
1748         }
1749
1750         // Now process the feed
1751
1752         if($feed->get_item_quantity()) {
1753
1754                 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1755
1756         // in inverse date order
1757                 if ($datedir)
1758                         $items = array_reverse($feed->get_items());
1759                 else
1760                         $items = $feed->get_items();
1761
1762
1763                 foreach($items as $item) {
1764
1765                         $is_reply = false;
1766                         $item_id = $item->get_id();
1767                         $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1768                         if(isset($rawthread[0]['attribs']['']['ref'])) {
1769                                 $is_reply = true;
1770                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1771                         }
1772
1773                         if(($is_reply) && is_array($contact)) {
1774
1775                                 if($pass == 1)
1776                                         continue;
1777
1778                                 // Have we seen it? If not, import it.
1779
1780                                 $item_id  = $item->get_id();
1781                                 $datarray = get_atom_elements($feed,$item);
1782
1783                                 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
1784                                         $datarray['author-name'] = $contact['name'];
1785                                 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
1786                                         $datarray['author-link'] = $contact['url'];
1787                                 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
1788                                         $datarray['author-avatar'] = $contact['thumb'];
1789
1790                                 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
1791                                         logger('consume_feed: no author information! ' . print_r($datarray,true));
1792                                         continue;
1793                                 }
1794
1795                                 $force_parent = false;
1796                                 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
1797                                         if($contact['network'] === NETWORK_OSTATUS)
1798                                                 $force_parent = true;
1799                                         if(strlen($datarray['title']))
1800                                                 unset($datarray['title']);
1801                                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1802                                                 dbesc(datetime_convert()),
1803                                                 dbesc($parent_uri),
1804                                                 intval($importer['uid'])
1805                                         );
1806                                         $datarray['last-child'] = 1;
1807                                 }
1808
1809
1810                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1811                                         dbesc($item_id),
1812                                         intval($importer['uid'])
1813                                 );
1814
1815                                 // Update content if 'updated' changes
1816
1817                                 if(count($r)) {
1818                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
1819
1820                                                 // do not accept (ignore) an earlier edit than one we currently have.
1821                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
1822                                                         continue;
1823
1824                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1825                                                         dbesc($datarray['title']),
1826                                                         dbesc($datarray['body']),
1827                                                         dbesc($datarray['tag']),
1828                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1829                                                         dbesc($item_id),
1830                                                         intval($importer['uid'])
1831                                                 );
1832                                         }
1833
1834                                         // update last-child if it changes
1835
1836                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1837                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1838                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1839                                                         dbesc(datetime_convert()),
1840                                                         dbesc($parent_uri),
1841                                                         intval($importer['uid'])
1842                                                 );
1843                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1844                                                         intval($allow[0]['data']),
1845                                                         dbesc(datetime_convert()),
1846                                                         dbesc($item_id),
1847                                                         intval($importer['uid'])
1848                                                 );
1849                                         }
1850                                         continue;
1851                                 }
1852
1853
1854                                 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
1855                                         // one way feed - no remote comment ability
1856                                         $datarray['last-child'] = 0;
1857                                 }
1858                                 $datarray['parent-uri'] = $parent_uri;
1859                                 $datarray['uid'] = $importer['uid'];
1860                                 $datarray['contact-id'] = $contact['id'];
1861                                 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1862                                         $datarray['type'] = 'activity';
1863                                         $datarray['gravity'] = GRAVITY_LIKE;
1864                                         // only one like or dislike per person
1865                                         $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
1866                                                 intval($datarray['uid']),
1867                                                 intval($datarray['contact-id']),
1868                                                 dbesc($datarray['verb']),
1869                                                 dbesc($parent_uri),
1870                                                 dbesc($parent_uri)
1871                                         );
1872                                         if($r && count($r))
1873                                                 continue; 
1874                                 }
1875
1876                                 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
1877                                         $xo = parse_xml_string($datarray['object'],false);
1878                                         $xt = parse_xml_string($datarray['target'],false);
1879
1880                                         if($xt->type == ACTIVITY_OBJ_NOTE) {
1881                                                 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
1882                                                         dbesc($xt->id),
1883                                                         intval($importer['importer_uid'])
1884                                                 );
1885                                                 if(! count($r))
1886                                                         continue;
1887
1888                                                 // extract tag, if not duplicate, add to parent item
1889                                                 if($xo->id && $xo->content) {
1890                                                         $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
1891                                                         if(! (stristr($r[0]['tag'],$newtag))) {
1892                                                                 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
1893                                                                         dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
1894                                                                         intval($r[0]['id'])
1895                                                                 );
1896                                                         }
1897                                                 }
1898                                         }
1899                                 }
1900
1901                                 $r = item_store($datarray,$force_parent);
1902                                 continue;
1903                         }
1904
1905                         else {
1906
1907                                 // Head post of a conversation. Have we seen it? If not, import it.
1908
1909                                 $item_id  = $item->get_id();
1910
1911                                 $datarray = get_atom_elements($feed,$item);
1912
1913                                 if(is_array($contact)) {
1914                                         if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
1915                                                 $datarray['author-name'] = $contact['name'];
1916                                         if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
1917                                                 $datarray['author-link'] = $contact['url'];
1918                                         if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
1919                                                 $datarray['author-avatar'] = $contact['thumb'];
1920                                 }
1921
1922                                 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
1923                                         logger('consume_feed: no author information! ' . print_r($datarray,true));
1924                                         continue;
1925                                 }
1926
1927                                 // special handling for events
1928
1929                                 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
1930                                         $ev = bbtoevent($datarray['body']);
1931                                         if(x($ev,'desc') && x($ev,'start')) {
1932                                                 $ev['uid'] = $importer['uid'];
1933                                                 $ev['uri'] = $item_id;
1934                                                 $ev['edited'] = $datarray['edited'];
1935                                                 $ev['private'] = $datarray['private'];
1936
1937                                                 if(is_array($contact))
1938                                                         $ev['cid'] = $contact['id'];
1939                                                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1940                                                         dbesc($item_id),
1941                                                         intval($importer['uid'])
1942                                                 );
1943                                                 if(count($r))
1944                                                         $ev['id'] = $r[0]['id'];
1945                                                 $xyz = event_store($ev);
1946                                                 continue;
1947                                         }
1948                                 }
1949
1950                                 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
1951                                         if(strlen($datarray['title']))
1952                                                 unset($datarray['title']);
1953                                         $datarray['last-child'] = 1;
1954                                 }
1955
1956
1957                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1958                                         dbesc($item_id),
1959                                         intval($importer['uid'])
1960                                 );
1961
1962                                 // Update content if 'updated' changes
1963
1964                                 if(count($r)) {
1965                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
1966
1967                                                 // do not accept (ignore) an earlier edit than one we currently have.
1968                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
1969                                                         continue;
1970
1971                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1972                                                         dbesc($datarray['title']),
1973                                                         dbesc($datarray['body']),
1974                                                         dbesc($datarray['tag']),
1975                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1976                                                         dbesc($item_id),
1977                                                         intval($importer['uid'])
1978                                                 );
1979                                         }
1980
1981                                         // update last-child if it changes
1982
1983                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1984                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1985                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1986                                                         intval($allow[0]['data']),
1987                                                         dbesc(datetime_convert()),
1988                                                         dbesc($item_id),
1989                                                         intval($importer['uid'])
1990                                                 );
1991                                         }
1992                                         continue;
1993                                 }
1994
1995                                 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1996                                         logger('consume-feed: New follower');
1997                                         new_follower($importer,$contact,$datarray,$item);
1998                                         return;
1999                                 }
2000                                 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW))  {
2001                                         lose_follower($importer,$contact,$datarray,$item);
2002                                         return;
2003                                 }
2004
2005                                 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
2006                                         logger('consume-feed: New friend request');
2007                                         new_follower($importer,$contact,$datarray,$item,true);
2008                                         return;
2009                                 }
2010                                 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND))  {
2011                                         lose_sharer($importer,$contact,$datarray,$item);
2012                                         return;
2013                                 }
2014
2015
2016                                 if(! is_array($contact))
2017                                         return;
2018
2019
2020                                 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2021                                                 // one way feed - no remote comment ability
2022                                                 $datarray['last-child'] = 0;
2023                                 }
2024                                 if($contact['network'] === NETWORK_FEED)
2025                                         $datarray['private'] = 2;
2026
2027                                 // This is my contact on another system, but it's really me.
2028                                 // Turn this into a wall post.
2029
2030                                 if($contact['remote_self']) {
2031                                         $datarray['wall'] = 1;
2032                                         if($contact['network'] === NETWORK_FEED) {
2033                                                 $datarray['private'] = 0;
2034                                         }
2035                                 }
2036
2037                                 $datarray['parent-uri'] = $item_id;
2038                                 $datarray['uid'] = $importer['uid'];
2039                                 $datarray['contact-id'] = $contact['id'];
2040
2041                                 if(! link_compare($datarray['owner-link'],$contact['url'])) {
2042                                         // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, 
2043                                         // but otherwise there's a possible data mixup on the sender's system.
2044                                         // the tgroup delivery code called from item_store will correct it if it's a forum,
2045                                         // but we're going to unconditionally correct it here so that the post will always be owned by our contact. 
2046                                         logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
2047                                         $datarray['owner-name']   = $contact['name'];
2048                                         $datarray['owner-link']   = $contact['url'];
2049                                         $datarray['owner-avatar'] = $contact['thumb'];
2050                                 }
2051
2052                                 $r = item_store($datarray);
2053                                 continue;
2054
2055                         }
2056                 }
2057         }
2058 }
2059
2060 function local_delivery($importer,$data) {
2061
2062         $a = get_app();
2063
2064         if($importer['readonly']) {
2065                 // We aren't receiving stuff from this person. But we will quietly ignore them
2066                 // rather than a blatant "go away" message.
2067                 logger('local_delivery: ignoring');
2068                 return 0;
2069                 //NOTREACHED
2070         }
2071
2072         // Consume notification feed. This may differ from consuming a public feed in several ways
2073         // - might contain email or friend suggestions
2074         // - might contain remote followup to our message
2075         //              - in which case we need to accept it and then notify other conversants
2076         // - we may need to send various email notifications
2077
2078         $feed = new SimplePie();
2079         $feed->set_raw_data($data);
2080         $feed->enable_order_by_date(false);
2081         $feed->init();
2082
2083 /*
2084         // Currently unsupported - needs a lot of work
2085         $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
2086         if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
2087                 $base = $reloc[0]['child'][NAMESPACE_DFRN];
2088                 $newloc = array();
2089                 $newloc['uid'] = $importer['importer_uid'];
2090                 $newloc['cid'] = $importer['id'];
2091                 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
2092                 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
2093                 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
2094                 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
2095                 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
2096                 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
2097                 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
2098                 $newloc['site-pubkey'] = notags(unxmlify($base['site-pubkey'][0]['data']));
2099                 $newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
2100                 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));
2101                 
2102                 // TODO
2103                 // merge with current record, current contents have priority
2104                 // update record, set url-updated
2105                 // update profile photos
2106                 // schedule a scan?
2107
2108         }
2109 */
2110
2111         // handle friend suggestion notification
2112
2113         $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
2114         if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
2115                 $base = $sugg[0]['child'][NAMESPACE_DFRN];
2116                 $fsugg = array();
2117                 $fsugg['uid'] = $importer['importer_uid'];
2118                 $fsugg['cid'] = $importer['id'];
2119                 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
2120                 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
2121                 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
2122                 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
2123                 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
2124
2125                 // Does our member already have a friend matching this description?
2126
2127                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
2128                         dbesc($fsugg['name']),
2129                         dbesc(normalise_link($fsugg['url'])),
2130                         intval($fsugg['uid'])
2131                 );
2132                 if(count($r))
2133                         return 0;
2134
2135                 // Do we already have an fcontact record for this person?
2136
2137                 $fid = 0;
2138                 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2139                         dbesc($fsugg['url']),
2140                         dbesc($fsugg['name']),
2141                         dbesc($fsugg['request'])
2142                 );
2143                 if(count($r)) {
2144                         $fid = $r[0]['id'];
2145
2146                         // OK, we do. Do we already have an introduction for this person ?
2147                         $r = q("select id from intro where uid = %d and fid = %d limit 1",
2148                                 intval($fsugg['uid']),
2149                                 intval($fid)
2150                         );
2151                         if(count($r))
2152                                 return 0;
2153                 }
2154                 if(! $fid)
2155                         $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
2156                         dbesc($fsugg['name']),
2157                         dbesc($fsugg['url']),
2158                         dbesc($fsugg['photo']),
2159                         dbesc($fsugg['request'])
2160                 );
2161                 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2162                         dbesc($fsugg['url']),
2163                         dbesc($fsugg['name']),
2164                         dbesc($fsugg['request'])
2165                 );
2166                 if(count($r)) {
2167                         $fid = $r[0]['id'];
2168                 }
2169                 // database record did not get created. Quietly give up.
2170                 else
2171                         return 0;
2172
2173
2174                 $hash = random_string();
2175  
2176                 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
2177                         VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
2178                         intval($fsugg['uid']),
2179                         intval($fid),
2180                         intval($fsugg['cid']),
2181                         dbesc($fsugg['body']),
2182                         dbesc($hash),
2183                         dbesc(datetime_convert()),
2184                         intval(0)
2185                 );
2186
2187                 notification(array(
2188                         'type'         => NOTIFY_SUGGEST,
2189                         'notify_flags' => $importer['notify-flags'],
2190                         'language'     => $importer['language'],
2191                         'to_name'      => $importer['username'],
2192                         'to_email'     => $importer['email'],
2193                         'uid'          => $importer['importer_uid'],
2194                         'item'         => $fsugg,
2195                         'link'         => $a->get_baseurl() . '/notifications/intros',
2196                         'source_name'  => $importer['name'],
2197                         'source_link'  => $importer['url'],
2198                         'source_photo' => $importer['photo'],
2199                         'verb'         => ACTIVITY_REQ_FRIEND,
2200                         'otype'        => 'intro'
2201                 ));
2202
2203                 return 0;
2204         }
2205
2206         $ismail = false;
2207
2208         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
2209         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
2210
2211                 logger('local_delivery: private message received');
2212
2213                 $ismail = true;
2214                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
2215
2216                 $msg = array();
2217                 $msg['uid'] = $importer['importer_uid'];
2218                 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
2219                 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
2220                 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
2221                 $msg['contact-id'] = $importer['id'];
2222                 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
2223                 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
2224                 $msg['seen'] = 0;
2225                 $msg['replied'] = 0;
2226                 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
2227                 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
2228                 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
2229                 
2230                 dbesc_array($msg);
2231
2232                 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
2233                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
2234
2235                 // send notifications.
2236
2237                 require_once('include/enotify.php');
2238
2239                 $notif_params = array(
2240                         'type' => NOTIFY_MAIL,
2241                         'notify_flags' => $importer['notify-flags'],
2242                         'language' => $importer['language'],
2243                         'to_name' => $importer['username'],
2244                         'to_email' => $importer['email'],
2245                         'uid' => $importer['importer_uid'],
2246                         'item' => $msg,
2247                         'source_name' => $msg['from-name'],
2248                         'source_link' => $importer['url'],
2249                         'source_photo' => $importer['thumb'],
2250                         'verb' => ACTIVITY_POST,
2251                         'otype' => 'mail'
2252                 );
2253                         
2254                 notification($notif_params);
2255                 return 0;
2256
2257                 // NOTREACHED
2258         }       
2259
2260         $community_page = 0;
2261         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
2262         if($rawtags) {
2263                 $community_page = intval($rawtags[0]['data']);
2264         }
2265         if(intval($importer['forum']) != $community_page) {
2266                 q("update contact set forum = %d where id = %d limit 1",
2267                         intval($community_page),
2268                         intval($importer['id'])
2269                 );
2270                 $importer['forum'] = (string) $community_page;
2271         }
2272         
2273         logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
2274
2275         // process any deleted entries
2276
2277         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
2278         if(is_array($del_entries) && count($del_entries)) {
2279                 foreach($del_entries as $dentry) {
2280                         $deleted = false;
2281                         if(isset($dentry['attribs']['']['ref'])) {
2282                                 $uri = $dentry['attribs']['']['ref'];
2283                                 $deleted = true;
2284                                 if(isset($dentry['attribs']['']['when'])) {
2285                                         $when = $dentry['attribs']['']['when'];
2286                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
2287                                 }
2288                                 else
2289                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
2290                         }
2291                         if($deleted) {
2292
2293                                 // check for relayed deletes to our conversation
2294
2295                                 $is_reply = false;              
2296                                 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
2297                                         dbesc($uri),
2298                                         intval($importer['importer_uid'])
2299                                 );
2300                                 if(count($r)) {
2301                                         $parent_uri = $r[0]['parent-uri'];
2302                                         if($r[0]['id'] != $r[0]['parent'])
2303                                                 $is_reply = true;
2304                                 }                               
2305
2306                                 if($is_reply) {
2307                                         $community = false;
2308
2309                                         if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2310                                                 $sql_extra = '';
2311                                                 $community = true;
2312                                                 logger('local_delivery: possible community delete');
2313                                         }
2314                                         else
2315                                                 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2316  
2317                                         // was the top-level post for this reply written by somebody on this site? 
2318                                         // Specifically, the recipient? 
2319
2320                                         $is_a_remote_delete = false;
2321
2322                                         $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, 
2323                                                 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
2324                                                 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
2325                                                 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2326                                                 AND `item`.`uid` = %d 
2327                                                 $sql_extra
2328                                                 LIMIT 1",
2329                                                 dbesc($parent_uri),
2330                                                 dbesc($parent_uri),
2331                                                 dbesc($parent_uri),
2332                                                 intval($importer['importer_uid'])
2333                                         );
2334                                         if($r && count($r))
2335                                                 $is_a_remote_delete = true;                     
2336
2337                                         // Does this have the characteristics of a community or private group comment?
2338                                         // If it's a reply to a wall post on a community/prvgroup page it's a 
2339                                         // valid community comment. Also forum_mode makes it valid for sure. 
2340                                         // If neither, it's not.
2341
2342                                         if($is_a_remote_delete && $community) {
2343                                                 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2344                                                         $is_a_remote_delete = false;
2345                                                         logger('local_delivery: not a community delete');
2346                                                 }
2347                                         }
2348
2349                                         if($is_a_remote_delete) {
2350                                                 logger('local_delivery: received remote delete');
2351                                         }
2352                                 }
2353
2354                                 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
2355                                         WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
2356                                         dbesc($uri),
2357                                         intval($importer['importer_uid']),
2358                                         intval($importer['id'])
2359                                 );
2360
2361                                 if(count($r)) {
2362                                         $item = $r[0];
2363
2364                                         if($item['deleted'])
2365                                                 continue;
2366
2367                                         logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
2368
2369                                         if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2370                                                 $xo = parse_xml_string($item['object'],false);
2371                                                 $xt = parse_xml_string($item['target'],false);
2372
2373                                                 if($xt->type === ACTIVITY_OBJ_NOTE) {
2374                                                         $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
2375                                                                 dbesc($xt->id),
2376                                                                 intval($importer['importer_uid'])
2377                                                         );
2378                                                         if(count($i)) {
2379
2380                                                                 // For tags, the owner cannot remove the tag on the author's copy of the post.
2381                                                                 
2382                                                                 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
2383                                                                 $author_remove = (($item['origin'] && $item['self']) ? true : false);
2384                                                                 $author_copy = (($item['origin']) ? true : false); 
2385
2386                                                                 if($owner_remove && $author_copy)
2387                                                                         continue;
2388                                                                 if($author_remove || $owner_remove) {                                                           
2389                                                                         $tags = explode(',',$i[0]['tag']);
2390                                                                         $newtags = array();
2391                                                                         if(count($tags)) {
2392                                                                                 foreach($tags as $tag)
2393                                                                                         if(trim($tag) !== trim($xo->body))
2394                                                                                                 $newtags[] = trim($tag);
2395                                                                         }
2396                                                                         q("update item set tag = '%s' where id = %d limit 1",
2397                                                                                 dbesc(implode(',',$newtags)),
2398                                                                                 intval($i[0]['id'])
2399                                                                         );
2400                                                                 }
2401                                                         }
2402                                                 }
2403                                         }
2404
2405                                         if($item['uri'] == $item['parent-uri']) {
2406                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2407                                                         `body` = '', `title` = ''
2408                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
2409                                                         dbesc($when),
2410                                                         dbesc(datetime_convert()),
2411                                                         dbesc($item['uri']),
2412                                                         intval($importer['importer_uid'])
2413                                                 );
2414                                         }
2415                                         else {
2416                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2417                                                         `body` = '', `title` = ''
2418                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2419                                                         dbesc($when),
2420                                                         dbesc(datetime_convert()),
2421                                                         dbesc($uri),
2422                                                         intval($importer['importer_uid'])
2423                                                 );
2424                                                 if($item['last-child']) {
2425                                                         // ensure that last-child is set in case the comment that had it just got wiped.
2426                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2427                                                                 dbesc(datetime_convert()),
2428                                                                 dbesc($item['parent-uri']),
2429                                                                 intval($item['uid'])
2430                                                         );
2431                                                         // who is the last child now? 
2432                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
2433                                                                 ORDER BY `created` DESC LIMIT 1",
2434                                                                         dbesc($item['parent-uri']),
2435                                                                         intval($importer['importer_uid'])
2436                                                         );
2437                                                         if(count($r)) {
2438                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
2439                                                                         intval($r[0]['id'])
2440                                                                 );
2441                                                         }       
2442                                                 }
2443                                                 // if this is a relayed delete, propagate it to other recipients
2444
2445                                                 if($is_a_remote_delete)
2446                                                         proc_run('php',"include/notifier.php","drop",$item['id']);
2447                                         }
2448                                 }
2449                         }
2450                 }
2451         }
2452
2453
2454         foreach($feed->get_items() as $item) {
2455
2456                 $is_reply = false;              
2457                 $item_id = $item->get_id();
2458                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
2459                 if(isset($rawthread[0]['attribs']['']['ref'])) {
2460                         $is_reply = true;
2461                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
2462                 }
2463
2464                 if($is_reply) {
2465                         $community = false;
2466
2467                         if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2468                                 $sql_extra = '';
2469                                 $community = true;
2470                                 logger('local_delivery: possible community reply');
2471                         }
2472                         else
2473                                 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2474  
2475                         // was the top-level post for this reply written by somebody on this site? 
2476                         // Specifically, the recipient? 
2477
2478                         $is_a_remote_comment = false;
2479
2480                         // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2481                         $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, 
2482                                 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
2483                                 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
2484                                 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2485                                 AND `item`.`uid` = %d 
2486                                 $sql_extra
2487                                 LIMIT 1",
2488                                 dbesc($parent_uri),
2489                                 dbesc($parent_uri),
2490                                 dbesc($parent_uri),
2491                                 intval($importer['importer_uid'])
2492                         );
2493                         if($r && count($r))
2494                                 $is_a_remote_comment = true;                    
2495
2496                         // Does this have the characteristics of a community or private group comment?
2497                         // If it's a reply to a wall post on a community/prvgroup page it's a 
2498                         // valid community comment. Also forum_mode makes it valid for sure. 
2499                         // If neither, it's not.
2500
2501                         if($is_a_remote_comment && $community) {
2502                                 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2503                                         $is_a_remote_comment = false;
2504                                         logger('local_delivery: not a community reply');
2505                                 }
2506                         }
2507
2508                         if($is_a_remote_comment) {
2509                                 logger('local_delivery: received remote comment');
2510                                 $is_like = false;
2511                                 // remote reply to our post. Import and then notify everybody else.
2512
2513                                 $datarray = get_atom_elements($feed,$item);
2514
2515                                 $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body`  FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2516                                         dbesc($item_id),
2517                                         intval($importer['importer_uid'])
2518                                 );
2519
2520                                 // Update content if 'updated' changes
2521
2522                                 if(count($r)) {
2523                                         $iid = $r[0]['id'];
2524                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2525                                         
2526                                                 // do not accept (ignore) an earlier edit than one we currently have.
2527                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2528                                                         continue;
2529   
2530                                                 logger('received updated comment' , LOGGER_DEBUG);
2531                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2532                                                         dbesc($datarray['title']),
2533                                                         dbesc($datarray['body']),
2534                                                         dbesc($datarray['tag']),
2535                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2536                                                         dbesc($item_id),
2537                                                         intval($importer['importer_uid'])
2538                                                 );
2539
2540                                                 proc_run('php',"include/notifier.php","comment-import",$iid);
2541
2542                                         }
2543
2544                                         continue;
2545                                 }
2546
2547
2548                                 // TODO: make this next part work against both delivery threads of a community post
2549
2550 //                              if((! link_compare($datarray['author-link'],$importer['url'])) && (! $community)) {
2551 //                                      logger('local_delivery: received relay claiming to be from ' . $importer['url'] . ' however comment author url is ' . $datarray['author-link'] ); 
2552                                         // they won't know what to do so don't report an error. Just quietly die.
2553 //                                      return 0;
2554 //                              }                                       
2555
2556                                 // our user with $importer['importer_uid'] is the owner
2557
2558                                 $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
2559                                         intval($importer['importer_uid'])
2560                                 );
2561
2562
2563                                 $datarray['type'] = 'remote-comment';
2564                                 $datarray['wall'] = 1;
2565                                 $datarray['parent-uri'] = $parent_uri;
2566                                 $datarray['uid'] = $importer['importer_uid'];
2567                                 $datarray['owner-name'] = $own[0]['name'];
2568                                 $datarray['owner-link'] = $own[0]['url'];
2569                                 $datarray['owner-avatar'] = $own[0]['thumb'];
2570                                 $datarray['contact-id'] = $importer['id'];
2571
2572                                 if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
2573                                         $is_like = true;
2574                                         $datarray['type'] = 'activity';
2575                                         $datarray['gravity'] = GRAVITY_LIKE;
2576                                         $datarray['last-child'] = 0;
2577                                         // only one like or dislike per person
2578                                         $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s' or `parent-uri` = '%s') and deleted = 0 limit 1",
2579                                                 intval($datarray['uid']),
2580                                                 intval($datarray['contact-id']),
2581                                                 dbesc($datarray['verb']),
2582                                                 dbesc($datarray['parent-uri']),
2583                                                 dbesc($datarray['parent-uri'])
2584                 
2585                                         );
2586                                         if($r && count($r))
2587                                                 continue; 
2588                                 }
2589
2590                                 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2591                                         
2592                                         $xo = parse_xml_string($datarray['object'],false);
2593                                         $xt = parse_xml_string($datarray['target'],false);
2594
2595                                         if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
2596
2597                                                 // fetch the parent item
2598
2599                                                 $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
2600                                                         dbesc($xt->id),
2601                                                         intval($importer['importer_uid'])
2602                                                 );
2603                                                 if(! count($tagp))
2604                                                         continue;       
2605
2606                                                 // extract tag, if not duplicate, and this user allows tags, add to parent item                                         
2607
2608                                                 if($xo->id && $xo->content) {
2609                                                         $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2610                                                         if(! (stristr($tagp[0]['tag'],$newtag))) {
2611                                                                 $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
2612                                                                         intval($importer['importer_uid'])
2613                                                                 );
2614                                                                 if(count($i) && ! intval($i[0]['blocktags'])) {
2615                                                                         q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
2616                                                                                 dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
2617                                                                                 intval($tagp[0]['id']),
2618                                                                                 dbesc(datetime_convert())
2619                                                                         );
2620                                                                 }
2621                                                         }
2622                                                 }                                                                                                       
2623                                         }
2624                                 }
2625
2626 //                              if($community) {
2627 //                                      $newtag = '@[url=' . $a->get_baseurl() . '/profile/' . $importer['nickname'] . ']' . $importer['username'] . '[/url]';
2628 //                                      if(! stristr($datarray['tag'],$newtag)) {
2629 //                                              if(strlen($datarray['tag']))
2630 //                                                      $datarray['tag'] .= ',';
2631 //                                              $datarray['tag'] .= $newtag;
2632 //                                      }
2633 //                              }
2634
2635
2636                                 $posted_id = item_store($datarray);
2637                                 $parent = 0;
2638
2639                                 if($posted_id) {
2640                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2641                                                 intval($posted_id),
2642                                                 intval($importer['importer_uid'])
2643                                         );
2644                                         if(count($r))
2645                                                 $parent = $r[0]['parent'];
2646                         
2647                                         if(! $is_like) {
2648                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
2649                                                         dbesc(datetime_convert()),
2650                                                         intval($importer['importer_uid']),
2651                                                         intval($r[0]['parent'])
2652                                                 );
2653
2654                                                 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2655                                                         dbesc(datetime_convert()),
2656                                                         intval($importer['importer_uid']),
2657                                                         intval($posted_id)
2658                                                 );
2659                                         }
2660
2661                                         if($posted_id && $parent) {
2662                                 
2663                                                 proc_run('php',"include/notifier.php","comment-import","$posted_id");
2664                                         
2665                                                 if((! $is_like) && (! $importer['self'])) {
2666
2667                                                         require_once('include/enotify.php');
2668
2669                                                         notification(array(
2670                                                                 'type'         => NOTIFY_COMMENT,
2671                                                                 'notify_flags' => $importer['notify-flags'],
2672                                                                 'language'     => $importer['language'],
2673                                                                 'to_name'      => $importer['username'],
2674                                                                 'to_email'     => $importer['email'],
2675                                                                 'uid'          => $importer['importer_uid'],
2676                                                                 'item'         => $datarray,
2677                                                                 'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
2678                                                                 'source_name'  => stripslashes($datarray['author-name']),
2679                                                                 'source_link'  => $datarray['author-link'],
2680                                                                 'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
2681                                                                         ? $importer['thumb'] : $datarray['author-avatar']),
2682                                                                 'verb'         => ACTIVITY_POST,
2683                                                                 'otype'        => 'item',
2684                                                                 'parent'       => $parent,
2685
2686                                                         ));
2687
2688                                                 }
2689                                         }
2690
2691                                         return 0;
2692                                         // NOTREACHED
2693                                 }
2694                         }
2695                         else {
2696
2697                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
2698
2699                                 $item_id  = $item->get_id();
2700                                 $datarray = get_atom_elements($feed,$item);
2701
2702                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2703                                         dbesc($item_id),
2704                                         intval($importer['importer_uid'])
2705                                 );
2706
2707                                 // Update content if 'updated' changes
2708
2709                                 if(count($r)) {
2710                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
2711
2712                                                 // do not accept (ignore) an earlier edit than one we currently have.
2713                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2714                                                         continue;
2715
2716                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2717                                                         dbesc($datarray['title']),
2718                                                         dbesc($datarray['body']),
2719                                                         dbesc($datarray['tag']),
2720                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2721                                                         dbesc($item_id),
2722                                                         intval($importer['importer_uid'])
2723                                                 );
2724                                         }
2725
2726                                         // update last-child if it changes
2727
2728                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2729                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
2730                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
2731                                                         dbesc(datetime_convert()),
2732                                                         dbesc($parent_uri),
2733                                                         intval($importer['importer_uid'])
2734                                                 );
2735                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2736                                                         intval($allow[0]['data']),
2737                                                         dbesc(datetime_convert()),
2738                                                         dbesc($item_id),
2739                                                         intval($importer['importer_uid'])
2740                                                 );
2741                                         }
2742                                         continue;
2743                                 }
2744
2745                                 $datarray['parent-uri'] = $parent_uri;
2746                                 $datarray['uid'] = $importer['importer_uid'];
2747                                 $datarray['contact-id'] = $importer['id'];
2748                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
2749                                         $datarray['type'] = 'activity';
2750                                         $datarray['gravity'] = GRAVITY_LIKE;
2751                                         // only one like or dislike per person
2752                                         $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
2753                                                 intval($datarray['uid']),
2754                                                 intval($datarray['contact-id']),
2755                                                 dbesc($datarray['verb']),
2756                                                 dbesc($parent_uri),
2757                                                 dbesc($parent_uri)
2758                                         );
2759                                         if($r && count($r))
2760                                                 continue; 
2761
2762                                 }
2763
2764                                 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2765
2766                                         $xo = parse_xml_string($datarray['object'],false);
2767                                         $xt = parse_xml_string($datarray['target'],false);
2768
2769                                         if($xt->type == ACTIVITY_OBJ_NOTE) {
2770                                                 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
2771                                                         dbesc($xt->id),
2772                                                         intval($importer['importer_uid'])
2773                                                 );
2774                                                 if(! count($r))
2775                                                         continue;                               
2776
2777                                                 // extract tag, if not duplicate, add to parent item                                            
2778                                                 if($xo->content) {
2779                                                         if(! (stristr($r[0]['tag'],trim($xo->content)))) {
2780                                                                 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
2781                                                                         dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
2782                                                                         intval($r[0]['id'])
2783                                                                 );
2784                                                         }
2785                                                 }                                                                                                       
2786                                         }
2787                                 }
2788
2789                                 $posted_id = item_store($datarray);
2790
2791                                 // find out if our user is involved in this conversation and wants to be notified.
2792                         
2793                                 if(!x($datarray['type']) || $datarray['type'] != 'activity') {
2794
2795                                         $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
2796                                                 dbesc($parent_uri),
2797                                                 intval($importer['importer_uid'])
2798                                         );
2799
2800                                         if(count($myconv)) {
2801                                                 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
2802
2803                                                 // first make sure this isn't our own post coming back to us from a wall-to-wall event
2804                                                 if(! link_compare($datarray['author-link'],$importer_url)) {
2805
2806                                                         
2807                                                         foreach($myconv as $conv) {
2808
2809                                                                 // now if we find a match, it means we're in this conversation
2810         
2811                                                                 if(! link_compare($conv['author-link'],$importer_url))
2812                                                                         continue;
2813
2814                                                                 require_once('include/enotify.php');
2815                                                                 
2816                                                                 $conv_parent = $conv['parent'];
2817
2818                                                                 notification(array(
2819                                                                         'type'         => NOTIFY_COMMENT,
2820                                                                         'notify_flags' => $importer['notify-flags'],
2821                                                                         'language'     => $importer['language'],
2822                                                                         'to_name'      => $importer['username'],
2823                                                                         'to_email'     => $importer['email'],
2824                                                                         'uid'          => $importer['importer_uid'],
2825                                                                         'item'         => $datarray,
2826                                                                         'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
2827                                                                         'source_name'  => stripslashes($datarray['author-name']),
2828                                                                         'source_link'  => $datarray['author-link'],
2829                                                                         'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
2830                                                                                 ? $importer['thumb'] : $datarray['author-avatar']),
2831                                                                         'verb'         => ACTIVITY_POST,
2832                                                                         'otype'        => 'item',
2833                                                                         'parent'       => $conv_parent,
2834
2835                                                                 ));
2836
2837                                                                 // only send one notification
2838                                                                 break;
2839                                                         }
2840                                                 }
2841                                         }
2842                                 }
2843                                 continue;
2844                         }
2845                 }
2846
2847                 else {
2848
2849                         // Head post of a conversation. Have we seen it? If not, import it.
2850
2851
2852                         $item_id  = $item->get_id();
2853                         $datarray = get_atom_elements($feed,$item);
2854
2855                         if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2856                                 $ev = bbtoevent($datarray['body']);
2857                                 if(x($ev,'desc') && x($ev,'start')) {
2858                                         $ev['cid'] = $importer['id'];
2859                                         $ev['uid'] = $importer['uid'];
2860                                         $ev['uri'] = $item_id;
2861                                         $ev['edited'] = $datarray['edited'];
2862                                         $ev['private'] = $datarray['private'];
2863
2864                                         $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2865                                                 dbesc($item_id),
2866                                                 intval($importer['uid'])
2867                                         );
2868                                         if(count($r))
2869                                                 $ev['id'] = $r[0]['id'];
2870                                         $xyz = event_store($ev);
2871                                         continue;
2872                                 }
2873                         }
2874
2875                         $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2876                                 dbesc($item_id),
2877                                 intval($importer['importer_uid'])
2878                         );
2879
2880                         // Update content if 'updated' changes
2881
2882                         if(count($r)) {
2883                                 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
2884
2885                                         // do not accept (ignore) an earlier edit than one we currently have.
2886                                         if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2887                                                 continue;
2888
2889                                         $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2890                                                 dbesc($datarray['title']),
2891                                                 dbesc($datarray['body']),
2892                                                 dbesc($datarray['tag']),
2893                                                 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2894                                                 dbesc($item_id),
2895                                                 intval($importer['importer_uid'])
2896                                         );
2897                                 }
2898
2899                                 // update last-child if it changes
2900
2901                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2902                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2903                                         $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2904                                                 intval($allow[0]['data']),
2905                                                 dbesc(datetime_convert()),
2906                                                 dbesc($item_id),
2907                                                 intval($importer['importer_uid'])
2908                                         );
2909                                 }
2910                                 continue;
2911                         }
2912
2913                         // This is my contact on another system, but it's really me.
2914                         // Turn this into a wall post.
2915
2916                         if($importer['remote_self'])
2917                                 $datarray['wall'] = 1;
2918
2919                         $datarray['parent-uri'] = $item_id;
2920                         $datarray['uid'] = $importer['importer_uid'];
2921                         $datarray['contact-id'] = $importer['id'];
2922
2923                         if(! link_compare($datarray['owner-link'],$contact['url'])) {
2924                                 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, 
2925                                 // but otherwise there's a possible data mixup on the sender's system.
2926                                 // the tgroup delivery code called from item_store will correct it if it's a forum,
2927                                 // but we're going to unconditionally correct it here so that the post will always be owned by our contact. 
2928                                 logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
2929                                 $datarray['owner-name']   = $importer['senderName'];
2930                                 $datarray['owner-link']   = $importer['url'];
2931                                 $datarray['owner-avatar'] = $importer['thumb'];
2932                         }
2933
2934                         $r = item_store($datarray);
2935                         continue;
2936                 }
2937         }
2938
2939         return 0;
2940         // NOTREACHED
2941
2942 }
2943
2944
2945 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
2946         $url = notags(trim($datarray['author-link']));
2947         $name = notags(trim($datarray['author-name']));
2948         $photo = notags(trim($datarray['author-avatar']));
2949
2950         $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
2951         if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
2952                 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
2953
2954         if(is_array($contact)) {
2955                 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
2956                         || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
2957                         $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
2958                                 intval(CONTACT_IS_FRIEND),
2959                                 intval($contact['id']),
2960                                 intval($importer['uid'])
2961                         );
2962                 }
2963                 // send email notification to owner?
2964         }
2965         else {
2966         
2967                 // create contact record
2968
2969                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`, 
2970                         `blocked`, `readonly`, `pending`, `writable` )
2971                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
2972                         intval($importer['uid']),
2973                         dbesc(datetime_convert()),
2974                         dbesc($url),
2975                         dbesc(normalise_link($url)),
2976                         dbesc($name),
2977                         dbesc($nick),
2978                         dbesc($photo),
2979                         dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
2980                         intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
2981                 );
2982                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
2983                                 intval($importer['uid']),
2984                                 dbesc($url)
2985                 );
2986                 if(count($r))
2987                                 $contact_record = $r[0];
2988
2989                 // create notification  
2990                 $hash = random_string();
2991
2992                 if(is_array($contact_record)) {
2993                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
2994                                 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
2995                                 intval($importer['uid']),
2996                                 intval($contact_record['id']),
2997                                 dbesc($hash),
2998                                 dbesc(datetime_convert())
2999                         );
3000                 }
3001                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
3002                         intval($importer['uid'])
3003                 );
3004                 $a = get_app();
3005                 if(count($r)) {
3006
3007                         if(intval($r[0]['def_gid'])) {
3008                                 require_once('include/group.php');
3009                                 group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
3010                         }
3011
3012                         if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
3013                                 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
3014                                 $email = replace_macros($email_tpl, array(
3015                                         '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
3016                                         '$url' => $url,
3017                                         '$myname' => $r[0]['username'],
3018                                         '$siteurl' => $a->get_baseurl(),
3019                                         '$sitename' => $a->config['sitename']
3020                                 ));
3021                                 $res = mail($r[0]['email'], 
3022                                         (($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],
3023                                         $email,
3024                                         'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
3025                                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
3026                                         . 'Content-transfer-encoding: 8bit' );
3027                         
3028                         }
3029                 }
3030         }
3031 }
3032
3033 function lose_follower($importer,$contact,$datarray,$item) {
3034
3035         if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
3036                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3037                         intval(CONTACT_IS_SHARING),
3038                         intval($contact['id'])
3039                 );
3040         }
3041         else {
3042                 contact_remove($contact['id']);
3043         }
3044 }
3045
3046 function lose_sharer($importer,$contact,$datarray,$item) {
3047
3048         if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
3049                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3050                         intval(CONTACT_IS_FOLLOWER),
3051                         intval($contact['id'])
3052                 );
3053         }
3054         else {
3055                 contact_remove($contact['id']);
3056         }
3057 }
3058
3059
3060 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
3061
3062         $a = get_app();
3063
3064         if(is_array($importer)) {
3065                 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
3066                         intval($importer['uid'])
3067                 );
3068         }
3069
3070         // Diaspora has different message-ids in feeds than they do 
3071         // through the direct Diaspora protocol. If we try and use
3072         // the feed, we'll get duplicates. So don't.
3073
3074         if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
3075                 return;
3076
3077         $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
3078
3079         // Use a single verify token, even if multiple hubs
3080
3081         $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
3082
3083         $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
3084
3085         logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: '  . $push_url . ' with verifier ' . $verify_token);
3086
3087         if(! strlen($contact['hub-verify'])) {
3088                 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
3089                         dbesc($verify_token),
3090                         intval($contact['id'])
3091                 );
3092         }
3093
3094         post_url($url,$params);
3095
3096         logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
3097                         
3098         return;
3099
3100 }
3101
3102
3103 function atom_author($tag,$name,$uri,$h,$w,$photo) {
3104         $o = '';
3105         if(! $tag)
3106                 return $o;
3107         $name = xmlify($name);
3108         $uri = xmlify($uri);
3109         $h = intval($h);
3110         $w = intval($w);
3111         $photo = xmlify($photo);
3112
3113
3114         $o .= "<$tag>\r\n";
3115         $o .= "<name>$name</name>\r\n";
3116         $o .= "<uri>$uri</uri>\r\n";
3117         $o .= '<link rel="photo"  type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3118         $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3119
3120         call_hooks('atom_author', $o);
3121
3122         $o .= "</$tag>\r\n";
3123         return $o;
3124 }
3125
3126 function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
3127
3128         $a = get_app();
3129
3130         if(! $item['parent'])
3131                 return;
3132
3133         if($item['deleted'])
3134                 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
3135
3136
3137         if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
3138                 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
3139         else
3140                 $body = $item['body'];
3141
3142
3143         $o = "\r\n\r\n<entry>\r\n";
3144
3145         if(is_array($author))
3146                 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
3147         else
3148                 $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']));
3149         if(strlen($item['owner-name']))
3150                 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
3151
3152         if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || ($item['thr-parent'])) {
3153                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
3154                 $o .= '<thr:in-reply-to ref="' . xmlify($parent_item) . '" type="text/html" href="' .  xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['parent']) . '" />' . "\r\n";
3155         }
3156
3157         $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
3158         $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
3159         $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
3160         $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
3161         $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
3162         $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
3163         $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
3164         if($comment)
3165                 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
3166
3167         if($item['location']) {
3168                 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
3169                 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
3170         }
3171
3172         if($item['coord'])
3173                 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
3174
3175         if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
3176                 $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
3177
3178         if($item['extid'])
3179                 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
3180         if($item['bookmark'])
3181                 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
3182
3183         if($item['app'])
3184                 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
3185
3186         if($item['guid'])
3187                 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
3188
3189         if($item['signed_text']) {
3190                 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
3191                 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
3192         }
3193
3194         $verb = construct_verb($item);
3195         $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
3196         $actobj = construct_activity_object($item);
3197         if(strlen($actobj))
3198                 $o .= $actobj;
3199         $actarg = construct_activity_target($item);
3200         if(strlen($actarg))
3201                 $o .= $actarg;
3202
3203         $tags = item_getfeedtags($item);
3204         if(count($tags)) {
3205                 foreach($tags as $t) {
3206                         $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
3207                 }
3208         }
3209
3210         $o .= item_getfeedattach($item);
3211
3212         $mentioned = get_mentions($item);
3213         if($mentioned)
3214                 $o .= $mentioned;
3215         
3216         call_hooks('atom_entry', $o);
3217
3218         $o .= '</entry>' . "\r\n";
3219         
3220         return $o;
3221 }
3222
3223 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
3224         $a = get_app();
3225
3226         logger('fix_private_photos', LOGGER_DEBUG);
3227         $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
3228
3229         $orig_body = $s;
3230         $new_body = '';
3231
3232         $img_start = strpos($orig_body, '[img');
3233         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3234         $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3235         while( ($img_st_close !== false) && ($img_len !== false) ) {
3236
3237                 $img_st_close++; // make it point to AFTER the closing bracket
3238                 $image = substr($orig_body, $img_start + $img_st_close, $img_len);
3239
3240                 logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
3241
3242
3243                 if(stristr($image , $site . '/photo/')) {
3244                         // Only embed locally hosted photos
3245                         $replace = false;
3246                         $i = basename($image);
3247                         $i = str_replace(array('.jpg','.png'),array('',''),$i);
3248                         $x = strpos($i,'-');
3249
3250                         if($x) {
3251                                 $res = substr($i,$x+1);
3252                                 $i = substr($i,0,$x);
3253                                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
3254                                         dbesc($i),
3255                                         intval($res),
3256                                         intval($uid)
3257                                 );
3258                                 if(count($r)) {
3259
3260                                         // Check to see if we should replace this photo link with an embedded image
3261                                         // 1. No need to do so if the photo is public
3262                                         // 2. If there's a contact-id provided, see if they're in the access list
3263                                         //    for the photo. If so, embed it. 
3264                                         // 3. Otherwise, if we have an item, see if the item permissions match the photo
3265                                         //    permissions, regardless of order but first check to see if they're an exact
3266                                         //    match to save some processing overhead.
3267
3268                                         if(has_permissions($r[0])) {
3269                                                 if($cid) {
3270                                                         $recips = enumerate_permissions($r[0]);
3271                                                         if(in_array($cid, $recips)) {
3272                                                                 $replace = true;        
3273                                                         }
3274                                                 }
3275                                                 elseif($item) {
3276                                                         if(compare_permissions($item,$r[0]))
3277                                                                 $replace = true;
3278                                                 }
3279                                         }
3280                                         if($replace) {
3281                                                 $data = $r[0]['data'];
3282                                                 $type = $r[0]['type'];
3283
3284                                                 // If a custom width and height were specified, apply before embedding
3285                                                 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
3286                                                         logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
3287
3288                                                         $width = intval($match[1]);
3289                                                         $height = intval($match[2]);
3290
3291                                                         $ph = new Photo($data, $type);
3292                                                         if($ph->is_valid()) {
3293                                                                 $ph->scaleImage(max($width, $height));
3294                                                                 $data = $ph->imageString();
3295                                                                 $type = $ph->getType();
3296                                                         }
3297                                                 }
3298
3299                                                 logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
3300                                                 $image = 'data:' . $type . ';base64,' . base64_encode($data);
3301                                                 logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
3302                                         }
3303                                 }
3304                         }
3305                 }       
3306
3307                 $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
3308                 $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
3309                 if($orig_body === false)
3310                         $orig_body = '';
3311
3312                 $img_start = strpos($orig_body, '[img');
3313                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3314                 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3315         }
3316
3317         $new_body = $new_body . $orig_body;
3318
3319         return($new_body);
3320 }
3321
3322
3323 function has_permissions($obj) {
3324         if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
3325                 return true;
3326         return false;
3327 }
3328
3329 function compare_permissions($obj1,$obj2) {
3330         // first part is easy. Check that these are exactly the same. 
3331         if(($obj1['allow_cid'] == $obj2['allow_cid'])
3332                 && ($obj1['allow_gid'] == $obj2['allow_gid'])
3333                 && ($obj1['deny_cid'] == $obj2['deny_cid'])
3334                 && ($obj1['deny_gid'] == $obj2['deny_gid']))
3335                 return true;
3336
3337         // This is harder. Parse all the permissions and compare the resulting set.
3338
3339         $recipients1 = enumerate_permissions($obj1);
3340         $recipients2 = enumerate_permissions($obj2);
3341         sort($recipients1);
3342         sort($recipients2);
3343         if($recipients1 == $recipients2)
3344                 return true;
3345         return false;
3346 }
3347
3348 // returns an array of contact-ids that are allowed to see this object
3349
3350 function enumerate_permissions($obj) {
3351         require_once('include/group.php');
3352         $allow_people = expand_acl($obj['allow_cid']);
3353         $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
3354         $deny_people  = expand_acl($obj['deny_cid']);
3355         $deny_groups  = expand_groups(expand_acl($obj['deny_gid']));
3356         $recipients   = array_unique(array_merge($allow_people,$allow_groups));
3357         $deny         = array_unique(array_merge($deny_people,$deny_groups));
3358         $recipients   = array_diff($recipients,$deny);
3359         return $recipients;
3360 }
3361
3362 function item_getfeedtags($item) {
3363         $ret = array();
3364         $matches = false;
3365         $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3366         if($cnt) {
3367                 for($x = 0; $x < $cnt; $x ++) {
3368                         if($matches[1][$x])
3369                                 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
3370                 }
3371         }
3372         $matches = false; 
3373         $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3374         if($cnt) {
3375                 for($x = 0; $x < $cnt; $x ++) {
3376                         if($matches[1][$x])
3377                                 $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
3378                 }
3379         } 
3380         return $ret;
3381 }
3382
3383 function item_getfeedattach($item) {
3384         $ret = '';
3385         $arr = explode(',',$item['attach']);
3386         if(count($arr)) {
3387                 foreach($arr as $r) {
3388                         $matches = false;
3389                         $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
3390                         if($cnt) {
3391                                 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
3392                                 if(intval($matches[2]))
3393                                         $ret .= 'length="' . intval($matches[2]) . '" ';
3394                                 if($matches[4] !== ' ')
3395                                         $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
3396                                 $ret .= ' />' . "\r\n";
3397                         }
3398                 }
3399         }
3400         return $ret;
3401 }
3402
3403
3404         
3405 function item_expire($uid,$days) {
3406
3407         if((! $uid) || ($days < 1))
3408                 return;
3409
3410         // $expire_network_only = save your own wall posts
3411         // and just expire conversations started by others
3412
3413         $expire_network_only = get_pconfig($uid,'expire','network_only');
3414         $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
3415
3416         $r = q("SELECT * FROM `item` 
3417                 WHERE `uid` = %d 
3418                 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY 
3419                 AND `id` = `parent` 
3420                 $sql_extra
3421                 AND `deleted` = 0",
3422                 intval($uid),
3423                 intval($days)
3424         );
3425
3426         if(! count($r))
3427                 return;
3428
3429         $expire_items = get_pconfig($uid, 'expire','items');
3430         $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
3431         
3432         $expire_notes = get_pconfig($uid, 'expire','notes');
3433         $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
3434
3435         $expire_starred = get_pconfig($uid, 'expire','starred');
3436         $expire_starred = (($expire_starred===false)?1:intval($expire_starred)); // default if not set: 1
3437         
3438         $expire_photos = get_pconfig($uid, 'expire','photos');
3439         $expire_photos = (($expire_photos===false)?0:intval($expire_photos)); // default if not set: 0
3440  
3441         logger('expire: # items=' . count($r). "; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
3442
3443         foreach($r as $item) {
3444
3445                 // don't expire filed items
3446
3447                 if(strpos($item['file'],'[') !== false)
3448                         continue;
3449
3450                 // Only expire posts, not photos and photo comments
3451
3452                 if($expire_photos==0 && strlen($item['resource-id']))
3453                         continue;
3454                 if($expire_starred==0 && intval($item['starred']))
3455                         continue;
3456                 if($expire_notes==0 && $item['type']=='note')
3457                         continue;
3458                 if($expire_items==0 && $item['type']!='note')
3459                         continue;
3460
3461                 drop_item($item['id'],false);
3462         }
3463
3464         proc_run('php',"include/notifier.php","expire","$uid");
3465         
3466 }
3467
3468
3469 function drop_items($items) {
3470         $uid = 0;
3471
3472         if(! local_user() && ! remote_user())
3473                 return;
3474
3475         if(count($items)) {
3476                 foreach($items as $item) {
3477                         $owner = drop_item($item,false);
3478                         if($owner && ! $uid)
3479                                 $uid = $owner;
3480                 }
3481         }
3482
3483         // multiple threads may have been deleted, send an expire notification
3484
3485         if($uid)
3486                 proc_run('php',"include/notifier.php","expire","$uid");
3487 }
3488
3489
3490 function drop_item($id,$interactive = true) {
3491
3492         $a = get_app();
3493
3494         // locate item to be deleted
3495
3496         $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
3497                 intval($id)
3498         );
3499
3500         if(! count($r)) {
3501                 if(! $interactive)
3502                         return 0;
3503                 notice( t('Item not found.') . EOL);
3504                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3505         }
3506
3507         $item = $r[0];
3508
3509         $owner = $item['uid'];
3510
3511         // check if logged in user is either the author or owner of this item
3512
3513         if((local_user() == $item['uid']) || (remote_user() == $item['contact-id'])) {
3514
3515                 // delete the item
3516
3517                 $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
3518                         dbesc(datetime_convert()),
3519                         dbesc(datetime_convert()),
3520                         intval($item['id'])
3521                 );
3522
3523                 // clean up categories and tags so they don't end up as orphans
3524
3525                 $matches = false;
3526                 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
3527                 if($cnt) {
3528                         foreach($matches as $mtch) {
3529                                 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
3530                         }
3531                 }
3532
3533                 $matches = false;
3534
3535                 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
3536                 if($cnt) {
3537                         foreach($matches as $mtch) {
3538                                 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
3539                         }
3540                 }
3541
3542                 // If item is a link to a photo resource, nuke all the associated photos 
3543                 // (visitors will not have photo resources)
3544                 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
3545                 // generate a resource-id and therefore aren't intimately linked to the item. 
3546
3547                 if(strlen($item['resource-id'])) {
3548                         q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
3549                                 dbesc($item['resource-id']),
3550                                 intval($item['uid'])
3551                         );
3552                         // ignore the result
3553                 }
3554
3555                 // If item is a link to an event, nuke the event record.
3556
3557                 if(intval($item['event-id'])) {
3558                         q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
3559                                 intval($item['event-id']),
3560                                 intval($item['uid'])
3561                         );
3562                         // ignore the result
3563                 }
3564
3565                 // clean up item_id and sign meta-data tables
3566
3567                 $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
3568                         intval($item['id']),
3569                         intval($item['uid'])
3570                 );
3571
3572                 $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
3573                         intval($item['id']),
3574                         intval($item['uid'])
3575                 );
3576
3577                 // If it's the parent of a comment thread, kill all the kids
3578
3579                 if($item['uri'] == $item['parent-uri']) {
3580                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = ''
3581                                 WHERE `parent-uri` = '%s' AND `uid` = %d ",
3582                                 dbesc(datetime_convert()),
3583                                 dbesc(datetime_convert()),
3584                                 dbesc($item['parent-uri']),
3585                                 intval($item['uid'])
3586                         );
3587                         // ignore the result
3588                 }
3589                 else {
3590                         // ensure that last-child is set in case the comment that had it just got wiped.
3591                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
3592                                 dbesc(datetime_convert()),
3593                                 dbesc($item['parent-uri']),
3594                                 intval($item['uid'])
3595                         );
3596                         // who is the last child now? 
3597                         $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",
3598                                 dbesc($item['parent-uri']),
3599                                 intval($item['uid'])
3600                         );
3601                         if(count($r)) {
3602                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
3603                                         intval($r[0]['id'])
3604                                 );
3605                         }
3606
3607                         // Add a relayable_retraction signature for Diaspora.
3608                         store_diaspora_retract_sig($item, $a->user, $a->get_baseurl());
3609                 }
3610                 $drop_id = intval($item['id']);
3611
3612                 // send the notification upstream/downstream as the case may be
3613
3614                 if(! $interactive)
3615                         return $owner;
3616
3617                 proc_run('php',"include/notifier.php","drop","$drop_id");
3618                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3619                 //NOTREACHED
3620         }
3621         else {
3622                 if(! $interactive)
3623                         return 0;
3624                 notice( t('Permission denied.') . EOL);
3625                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3626                 //NOTREACHED
3627         }
3628         
3629 }
3630
3631
3632 function first_post_date($uid,$wall = false) {
3633         $r = q("select id, created from item 
3634                 where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0 
3635                 and id = parent
3636                 order by created asc limit 1",
3637                 intval($uid),
3638                 intval($wall ? 1 : 0)
3639         );
3640         if(count($r)) {
3641 //              logger('first_post_date: ' . $r[0]['id'] . ' ' . $r[0]['created'], LOGGER_DATA);
3642                 return substr(datetime_convert('',date_default_timezone_get(),$r[0]['created']),0,10);
3643         }
3644         return false;
3645 }
3646
3647 function posted_dates($uid,$wall) {
3648         $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
3649
3650         $dthen = first_post_date($uid,$wall);
3651         if(! $dthen)
3652                 return array();
3653
3654         // If it's near the end of a long month, backup to the 28th so that in 
3655         // consecutive loops we'll always get a whole month difference.
3656
3657         if(intval(substr($dnow,8)) > 28)
3658                 $dnow = substr($dnow,0,8) . '28';
3659         if(intval(substr($dthen,8)) > 28)
3660                 $dnow = substr($dthen,0,8) . '28';
3661
3662         $ret = array();
3663         // Starting with the current month, get the first and last days of every
3664         // month down to and including the month of the first post
3665         while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
3666                 $dstart = substr($dnow,0,8) . '01';
3667                 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
3668                 $start_month = datetime_convert('','',$dstart,'Y-m-d');
3669                 $end_month = datetime_convert('','',$dend,'Y-m-d');
3670                 $str = day_translate(datetime_convert('','',$dnow,'F Y'));
3671                 $ret[] = array($str,$end_month,$start_month);
3672                 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
3673         }
3674         return $ret;
3675 }
3676
3677
3678 function posted_date_widget($url,$uid,$wall) {
3679         $o = '';
3680
3681         // For former Facebook folks that left because of "timeline"
3682
3683         if($wall && intval(get_pconfig($uid,'system','no_wall_archive_widget')))
3684                 return $o;
3685
3686         $ret = posted_dates($uid,$wall);
3687         if(! count($ret))
3688                 return $o;
3689
3690         $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
3691                 '$title' => t('Archives'),
3692                 '$size' => ((count($ret) > 6) ? 6 : count($ret)),
3693                 '$url' => $url,
3694                 '$dates' => $ret
3695         ));
3696         return $o;
3697 }
3698
3699 function store_diaspora_retract_sig($item, $user, $baseurl) {
3700         // Note that we can't add a target_author_signature
3701         // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting
3702         // the comment, that means we're the home of the post, and Diaspora will only
3703         // check the parent_author_signature of retractions that it doesn't have to relay further
3704         //
3705         // I don't think this function gets called for an "unlike," but I'll check anyway
3706
3707         $enabled = intval(get_config('system','diaspora_enabled'));
3708         if(! $enabled) {
3709                 logger('drop_item: diaspora support disabled, not storing retraction signature', LOGGER_DEBUG);
3710                 return;
3711         }
3712
3713         logger('drop_item: storing diaspora retraction signature');
3714
3715         $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
3716
3717         if(local_user() == $item['uid']) {
3718
3719                 $handle = $user['nickname'] . '@' . substr($baseurl, strpos($baseurl,'://') + 3);
3720                 $authorsig = base64_encode(rsa_sign($signed_text,$user['prvkey'],'sha256'));
3721         }
3722         else {
3723                 $r = q("SELECT `nick`, `url` FROM `contact` WHERE `id` = '%d' LIMIT 1",
3724                         $item['contact-id'] // If this function gets called, drop_item() has already checked remote_user() == $item['contact-id']
3725                 );
3726                 if(count($r)) {
3727                         // The below handle only works for NETWORK_DFRN. I think that's ok, because this function
3728                         // only handles DFRN deletes
3729                         $handle_baseurl_start = strpos($r['url'],'://') + 3;
3730                         $handle_baseurl_length = strpos($r['url'],'/profile') - $handle_baseurl_start;
3731                         $handle = $r['nick'] . '@' . substr($r['url'], $handle_baseurl_start, $handle_baseurl_length);
3732                         $authorsig = '';
3733                 }
3734         }
3735
3736         if(isset($handle))
3737                 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
3738                         intval($item['id']),
3739                         dbesc($signed_text),
3740                         dbesc($authorsig),
3741                         dbesc($handle)
3742                 );
3743
3744         return;
3745 }