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