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