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