]> git.mxchange.org Git - friendica.git/blob - include/items.php
provide ability to survive transport mangling when travelling through html networks
[friendica.git] / include / items.php
1 <?php
2
3 require_once('bbcode.php');
4 require_once('oembed.php');
5 require_once('include/salmon.php');
6
7 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
8
9
10         // default permissions - anonymous user
11
12         $sql_extra = " 
13                 AND `allow_cid` = '' 
14                 AND `allow_gid` = '' 
15                 AND `deny_cid`  = '' 
16                 AND `deny_gid`  = '' 
17         ";
18
19         if(strlen($owner_nick) && ! intval($owner_nick)) {
20                 $r = q("SELECT `uid`, `nickname`, `timezone` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
21                         dbesc($owner_nick)
22                 );
23                 if(count($r)) {
24                         $owner_id = $r[0]['uid'];
25                         $owner_nick = $r[0]['nickname'];
26                         $owner_tz = $r[0]['timezone'];
27                 }
28         }
29
30         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
31                 intval($owner_id)
32         );
33         if(count($r)) {
34                 $owner = $r[0];
35                 $owner['nickname'] = $owner_nick;
36         }
37         else
38                 killme();
39
40
41         /**
42          *
43          * Determine the next birthday, but only if the birthday is published
44          * in the default profile. We _could_ also look for a private profile that the
45          * recipient can see, but somebody could get mad at us if they start getting
46          * public birthday greetings when they haven't made this info public. 
47          *
48          * Assuming we are able to publish this info, we are then going to convert
49          * the start time from the owner's timezone to UTC. 
50          *
51          * This will potentially solve the problem found with some social networks
52          * where birthdays are converted to the viewer's timezone and salutations from
53          * elsewhere in the world show up on the wrong day. We will convert it to the
54          * viewer's timezone also, but first we are going to convert it from the birthday
55          * person's timezone to GMT - so the viewer may find the birthday starting at
56          * 6:00PM the day before, but that will correspond to midnight to the birthday person.
57          *
58          */
59
60         $birthday = '';
61
62         $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
63                 intval($owner_id)
64         );
65
66         if($p && count($p)) {
67                 $tmp_dob = substr($p[0]['dob'],5);
68                 if(intval($tmp_dob)) {
69                         $y = datetime_convert($owner_tz,$owner_tz,'now','Y');
70                         $bd = $y . '-' . $tmp_dob . ' 00:00';
71                         $t_dob = strtotime($bd);
72                         $now = strtotime(datetime_convert($owner_tz,$owner_tz,'now'));
73                         if($t_dob < $now)
74                                 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
75                         $birthday = datetime_convert($owner_tz,'UTC',$bd,ATOM_TIME); 
76                 }
77         }
78
79         if($dfrn_id && $dfrn_id != '*') {
80
81                 $sql_extra = '';
82                 switch($direction) {
83                         case (-1):
84                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
85                                 $my_id = $dfrn_id;
86                                 break;
87                         case 0:
88                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
89                                 $my_id = '1:' . $dfrn_id;
90                                 break;
91                         case 1:
92                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
93                                 $my_id = '0:' . $dfrn_id;
94                                 break;
95                         default:
96                                 return false;
97                                 break; // NOTREACHED
98                 }
99
100                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
101                         intval($owner_id)
102                 );
103
104                 if(! count($r))
105                         return false;
106
107                 $contact = $r[0];
108                 $groups = init_groups_visitor($contact['id']);
109
110                 if(count($groups)) {
111                         for($x = 0; $x < count($groups); $x ++) 
112                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
113                         $gs = implode('|', $groups);
114                 }
115                 else
116                         $gs = '<<>>' ; // Impossible to match 
117
118                 $sql_extra = sprintf(" 
119                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
120                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
121                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
122                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
123                 ",
124                         intval($contact['id']),
125                         intval($contact['id']),
126                         dbesc($gs),
127                         dbesc($gs)
128                 );
129         }
130
131         if($dfrn_id === '' || $dfrn_id === '*')
132                 $sort = 'DESC';
133         else
134                 $sort = 'ASC';
135
136         if(! strlen($last_update))
137                 $last_update = 'now -30 days';
138
139         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
140
141         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
142                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
143                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
144                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
145                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
146                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
147                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
148                 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
149                 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
150                 $sql_extra
151                 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
152                 intval($owner_id),
153                 dbesc($check_date),
154                 dbesc($check_date),
155                 dbesc($sort)
156         );
157
158         // Will check further below if this actually returned results.
159         // We will provide an empty feed in any case.
160
161         $items = $r;
162
163         $feed_template = load_view_file('view/atom_feed.tpl');
164
165         $atom = '';
166
167         $hub = get_config('system','huburl');
168
169         $hubxml = '';
170         if(strlen($hub)) {
171                 $hubs = explode(',', $hub);
172                 if(count($hubs)) {
173                         foreach($hubs as $h) {
174                                 $h = trim($h);
175                                 if(! strlen($h))
176                                         continue;
177                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
178                         }
179                 }
180         }
181
182         $salmon = '<link rel="salmon" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ; 
183         $salmon .= '<link rel="http://salmon-protocol.org/ns/salmon-replies" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ; 
184         $salmon .= '<link rel="http://salmon-protocol.org/ns/salmon-mention" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ; 
185
186
187         $atom .= replace_macros($feed_template, array(
188                 '$version'      => xmlify(FRIENDIKA_VERSION),
189                 '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
190                 '$feed_title'   => xmlify($owner['name']),
191                 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
192                 '$hub'          => $hubxml,
193                 '$salmon'       => $salmon,
194                 '$name'         => xmlify($owner['name']),
195                 '$profile_page' => xmlify($owner['url']),
196                 '$photo'        => xmlify($owner['photo']),
197                 '$thumb'        => xmlify($owner['thumb']),
198                 '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
199                 '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
200                 '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) , 
201                 '$birthday'     => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : '')
202         ));
203
204         call_hooks('atom_feed', $atom);
205
206         if(! count($items)) {
207
208                 call_hooks('atom_feed_end', $atom);
209
210                 $atom .= '</feed>' . "\r\n";
211                 return $atom;
212         }
213
214         foreach($items as $item) {
215
216                 // public feeds get html, our own nodes use bbcode
217
218                 if($dfrn_id === '*') {
219                         $type = 'html';
220                 }
221                 else {
222                         $type = 'text';
223                 }
224
225                 $atom .= atom_entry($item,$type,null,$owner,true);
226         }
227
228         call_hooks('atom_feed_end', $atom);
229
230         $atom .= '</feed>' . "\r\n";
231
232         return $atom;
233 }
234
235
236 function construct_verb($item) {
237         if($item['verb'])
238                 return $item['verb'];
239         return ACTIVITY_POST;
240 }
241
242 function construct_activity_object($item) {
243
244         if($item['object']) {
245                 $o = '<as:object>' . "\r\n";
246                 $r = @simplexml_load_string($item['object']);
247                 if($r->type)
248                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
249                 if($r->id)
250                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
251                 if($r->title)
252                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
253                 if($r->link) {
254                         if(substr($r->link,0,1) === '<') 
255                                 $o .= $r->link;
256                         else
257                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
258                 }
259                 if($r->content)
260                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
261                 $o .= '</as:object>' . "\r\n";
262                 return $o;
263         }
264
265         return '';
266
267
268 function construct_activity_target($item) {
269
270         if($item['target']) {
271                 $o = '<as:target>' . "\r\n";
272                 $r = @simplexml_load_string($item['target']);
273                 if($r->type)
274                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
275                 if($r->id)
276                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
277                 if($r->title)
278                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
279                 if($r->link) {
280                         if(substr($r->link,0,1) === '<') 
281                                 $o .= $r->link;
282                         else
283                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
284                 }
285                 if($r->content)
286                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
287                 $o .= '</as:target>' . "\r\n";
288                 return $o;
289         }
290
291         return '';
292
293
294
295
296
297 function get_atom_elements($feed,$item) {
298
299         require_once('library/HTMLPurifier.auto.php');
300         require_once('include/html2bbcode.php');
301
302         $best_photo = array();
303
304         $res = array();
305
306         $author = $item->get_author();
307         $res['author-name'] = unxmlify($author->get_name());
308         $res['author-link'] = unxmlify($author->get_link());
309         $res['uri'] = unxmlify($item->get_id());
310         $res['title'] = unxmlify($item->get_title());
311         $res['body'] = unxmlify($item->get_content());
312
313
314         // look for a photo. We should check media size and find the best one,
315         // but for now let's just find any author photo
316
317         $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
318
319         if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
320                 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
321                 foreach($base as $link) {
322                         if(! $res['author-avatar']) {
323                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
324                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
325                         }
326                 }
327         }                       
328
329         $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
330
331         if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
332                 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
333                 if($base && count($base)) {
334                         foreach($base as $link) {
335                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
336                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
337                                 if(! $res['author-avatar']) {
338                                         if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
339                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
340                                 }
341                         }
342                 }
343         }
344
345         // No photo/profile-link on the item - look at the feed level
346
347         if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
348                 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
349                 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
350                         $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
351                         foreach($base as $link) {
352                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
353                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
354                                 if(! $res['author-avatar']) {
355                                         if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
356                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
357                                 }
358                         }
359                 }                       
360
361                 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
362
363                 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
364                         $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
365
366                         if($base && count($base)) {
367                                 foreach($base as $link) {
368                                         if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
369                                                 $res['author-link'] = unxmlify($link['attribs']['']['href']);
370                                         if(! (x($res,'author-avatar'))) {
371                                                 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
372                                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
373                                         }
374                                 }
375                         }
376                 }
377         }
378
379
380         /**
381          * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
382          */
383
384         $have_real_body = false;
385
386         $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
387         if($rawenv) {
388                 $have_real_body = true;
389                 $res['body'] = $rawenv[0]['data'];
390                 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
391                 $res['body'] = base64url_decode($res['body']);
392                 $res['realbody'] = true;
393         }
394
395         $maxlen = get_max_import_size();
396         if($maxlen && (strlen($res['body']) > $maxlen))
397                 $res['body'] = substr($res['body'],0, $maxlen);
398
399         // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
400         // the content type. Our own network only emits text normally, though it might have been converted to 
401         // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
402         // have to assume it is all html and needs to be purified.
403
404         // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
405         // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
406         // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
407         // html.
408
409
410         if((! $have_real_body) || (strpos($res['body'],'<')) || (strpos($res['body'],'>'))) {
411
412                 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
413                         '[youtube]$1[/youtube]', $res['body']);
414
415                 $res['body'] = oembed_html2bbcode($res['body']);
416         
417                 $config = HTMLPurifier_Config::createDefault();
418                 $config->set('Cache.DefinitionImpl', null);
419
420                 // we shouldn't need a whitelist, because the bbcode converter
421                 // will strip out any unsupported tags.
422                 // $config->set('HTML.Allowed', 'p,b,a[href],i'); 
423
424                 $purifier = new HTMLPurifier($config);
425                 $res['body'] = $purifier->purify($res['body']);
426
427                 $res['body'] = html2bbcode($res['body']);
428         }
429         else
430                 $res['body'] = escape_tags($res['body']);
431         
432
433         $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
434         if($allow && $allow[0]['data'] == 1)
435                 $res['last-child'] = 1;
436         else
437                 $res['last-child'] = 0;
438
439         $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
440         if($private && $private[0]['data'] == 1)
441                 $res['private'] = 1;
442         else
443                 $res['private'] = 0;
444
445
446         $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
447         if($rawlocation)
448                 $res['location'] = unxmlify($rawlocation[0]['data']);
449
450
451         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
452         if($rawcreated)
453                 $res['created'] = unxmlify($rawcreated[0]['data']);
454
455
456         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
457         if($rawedited)
458                 $res['edited'] = unxmlify($rawcreated[0]['data']);
459
460
461         if(! $res['created'])
462                 $res['created'] = $item->get_date();
463
464         if(! $res['edited'])
465                 $res['edited'] = $item->get_date();
466
467
468         $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
469         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
470                 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
471         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
472                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
473         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
474                 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
475         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
476                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
477
478         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
479                 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
480
481                 foreach($base as $link) {
482                         if(! $res['owner-avatar']) {
483                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')                 
484                                         $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
485                         }
486                 }
487         }
488
489         $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
490         if($rawgeo)
491                 $res['coord'] = unxmlify($rawgeo[0]['data']);
492
493
494         $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
495
496         // select between supported verbs
497
498         if($rawverb) {
499                 $res['verb'] = unxmlify($rawverb[0]['data']);
500         }
501
502         // translate OStatus unfollow to activity streams if it happened to get selected
503                 
504         if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
505                 $res['verb'] = ACTIVITY_UNFOLLOW;
506
507                 
508
509         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
510
511         if($rawobj) {
512                 $res['object'] = '<object>' . "\n";
513                 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
514                         $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
515                         $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
516                 }       
517                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
518                         $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
519                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
520                         $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
521                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
522                         $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
523                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
524                         $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
525                         if(! $body)
526                                 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
527                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
528                         $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
529                         if((strpos($body,'<')) || (strpos($body,'>'))) {
530
531                                 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
532                                         '[youtube]$1[/youtube]', $body);
533
534                                 $config = HTMLPurifier_Config::createDefault();
535                                 $config->set('Cache.DefinitionImpl', null);
536
537                                 $purifier = new HTMLPurifier($config);
538                                 $body = $purifier->purify($body);
539                                 $body = html2bbcode($body);
540                         }
541                         else
542                                 $body = escape_tags($body);
543
544                         $res['object'] .= '<content>' . $body . '</content>' . "\n";
545                 }
546
547                 $res['object'] .= '</object>' . "\n";
548         }
549
550         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
551
552         if($rawobj) {
553                 $res['target'] = '<target>' . "\n";
554                 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
555                         $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
556                 }       
557                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
558                         $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
559
560                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
561                         $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
562                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
563                         $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
564                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
565                         $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
566                         if(! $body)
567                                 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
568                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
569                         $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
570                         if((strpos($body,'<')) || (strpos($body,'>'))) {
571
572                                 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
573                                         '[youtube]$1[/youtube]', $body);
574
575                                 $config = HTMLPurifier_Config::createDefault();
576                                 $config->set('Cache.DefinitionImpl', null);
577
578                                 $purifier = new HTMLPurifier($config);
579                                 $body = $purifier->purify($body);
580                                 $body = html2bbcode($body);
581                         }
582                         else
583                                 $body = escape_tags($body);
584
585                         $res['target'] .= '<content>' . $body . '</content>' . "\n";
586                 }
587
588                 $res['target'] .= '</target>' . "\n";
589         }
590
591         $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
592
593         call_hooks('parse_atom', $arr);
594
595         return $res;
596 }
597
598 function encode_rel_links($links) {
599         $o = '';
600         if(! ((is_array($links)) && (count($links))))
601                 return $o;
602         foreach($links as $link) {
603                 $o .= '<link ';
604                 if($link['attribs']['']['rel'])
605                         $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
606                 if($link['attribs']['']['type'])
607                         $o .= 'type="' . $link['attribs']['']['type'] . '" ';
608                 if($link['attribs']['']['href'])
609                         $o .= 'href="' . $link['attribs']['']['href'] . '" ';
610                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
611                         $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
612                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
613                         $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
614                 $o .= ' />' . "\n" ;
615         }
616         return xmlify($o);
617 }
618
619 function item_store($arr) {
620
621         if($arr['gravity'])
622                 $arr['gravity'] = intval($arr['gravity']);
623         elseif($arr['parent-uri'] == $arr['uri'])
624                 $arr['gravity'] = 0;
625         elseif(activity_match($arr['verb'],ACTIVITY_POST))
626                 $arr['gravity'] = 6;
627         else      
628                 $arr['gravity'] = 6;   // extensible catchall
629
630         if(! x($arr,'type'))
631                 $arr['type']      = 'remote';
632         $arr['wall']          = ((x($arr,'wall'))          ? intval($arr['wall'])                : 0);
633         $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : random_string());
634         $arr['author-name']   = ((x($arr,'author-name'))   ? notags(trim($arr['author-name']))   : '');
635         $arr['author-link']   = ((x($arr,'author-link'))   ? notags(trim($arr['author-link']))   : '');
636         $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
637         $arr['owner-name']    = ((x($arr,'owner-name'))    ? notags(trim($arr['owner-name']))    : '');
638         $arr['owner-link']    = ((x($arr,'owner-link'))    ? notags(trim($arr['owner-link']))    : '');
639         $arr['owner-avatar']  = ((x($arr,'owner-avatar'))  ? notags(trim($arr['owner-avatar']))  : '');
640         $arr['created']       = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
641         $arr['edited']        = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
642         $arr['changed']       = datetime_convert();
643         $arr['title']         = ((x($arr,'title'))         ? notags(trim($arr['title']))         : '');
644         $arr['location']      = ((x($arr,'location'))      ? notags(trim($arr['location']))      : '');
645         $arr['coord']         = ((x($arr,'coord'))         ? notags(trim($arr['coord']))         : '');
646         $arr['last-child']    = ((x($arr,'last-child'))    ? intval($arr['last-child'])          : 0 );
647         $arr['visible']       = ((x($arr,'visible') !== false) ? intval($arr['visible'])         : 1 );
648         $arr['deleted']       = 0;
649         $arr['parent-uri']    = ((x($arr,'parent-uri'))    ? notags(trim($arr['parent-uri']))    : '');
650         $arr['verb']          = ((x($arr,'verb'))          ? notags(trim($arr['verb']))          : '');
651         $arr['object-type']   = ((x($arr,'object-type'))   ? notags(trim($arr['object-type']))   : '');
652         $arr['object']        = ((x($arr,'object'))        ? trim($arr['object'])                : '');
653         $arr['target-type']   = ((x($arr,'target-type'))   ? notags(trim($arr['target-type']))   : '');
654         $arr['target']        = ((x($arr,'target'))        ? trim($arr['target'])                : '');
655         $arr['allow_cid']     = ((x($arr,'allow_cid'))     ? trim($arr['allow_cid'])             : '');
656         $arr['allow_gid']     = ((x($arr,'allow_gid'))     ? trim($arr['allow_gid'])             : '');
657         $arr['deny_cid']      = ((x($arr,'deny_cid'))      ? trim($arr['deny_cid'])              : '');
658         $arr['deny_gid']      = ((x($arr,'deny_gid'))      ? trim($arr['deny_gid'])              : '');
659         $arr['private']       = ((x($arr,'private'))       ? intval($arr['private'])             : 0 );
660         $arr['body']          = ((x($arr,'body'))          ? escape_tags(trim($arr['body']))     : '');
661
662         // The content body may have been through a lot of filtering and transport escaping by now. 
663         // We don't want to skip any filters, however a side effect of all this filtering 
664         // is that ampersands and <> may have been double encoded, depending on which filter chain
665         // they came through. The presence of $res['realbody'] means we have something encoded in a 
666         // transport safe manner at the source and does not require any filter corrections. 
667
668         if(x($arr,'realbody'))
669                 unset($arr['realbody']);
670         else {
671                 $arr['body']          = str_replace(
672                                                                         array('&amp;amp;', '&amp;gt;', '&amp;lt;', '&amp;quot;'),
673                                                                         array('&amp;'    , '&gt;'    , '&lt;',     '&quot;'),
674                                                                         $arr['body']
675                                                                 );
676         }
677
678         if($arr['parent-uri'] === $arr['uri']) {
679                 $parent_id = 0;
680                 $allow_cid = $arr['allow_cid'];
681                 $allow_gid = $arr['allow_gid'];
682                 $deny_cid  = $arr['deny_cid'];
683                 $deny_gid  = $arr['deny_gid'];
684         }
685         else { 
686
687                 // find the parent and snarf the item id and ACL's
688
689                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
690                         dbesc($arr['parent-uri']),
691                         intval($arr['uid'])
692                 );
693
694                 if(count($r)) {
695
696                         // is the new message multi-level threaded?
697                         // even though we don't support it now, preserve the info
698                         // and re-attach to the conversation parent.
699
700                         if($r[0]['uri'] != $r[0]['parent-uri']) {
701                                 $arr['thr-parent'] = $arr['parent-uri'];
702                                 $arr['parent-uri'] = $r[0]['parent-uri'];
703                         }
704
705                         $parent_id = $r[0]['id'];
706                         $allow_cid = $r[0]['allow_cid'];
707                         $allow_gid = $r[0]['allow_gid'];
708                         $deny_cid  = $r[0]['deny_cid'];
709                         $deny_gid  = $r[0]['deny_gid'];
710                 }
711                 else {
712                         logger('item_store: item parent was not found - ignoring item');
713                         return 0;
714                 }
715         }
716
717         call_hooks('post_remote',$arr);
718
719         dbesc_array($arr);
720
721         logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
722
723         $r = dbq("INSERT INTO `item` (`" 
724                         . implode("`, `", array_keys($arr)) 
725                         . "`) VALUES ('" 
726                         . implode("', '", array_values($arr)) 
727                         . "')" );
728
729         // find the item we just created
730
731         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
732                 $arr['uri'],           // already dbesc'd
733                 intval($arr['uid'])
734         );
735         if(count($r)) {
736                 $current_post = $r[0]['id'];
737                 logger('item_store: created item ' . $current_post);
738         }
739         else {
740                 logger('item_store: could not locate created item');
741                 return 0;
742         }
743
744         if($arr['parent-uri'] === $arr['uri'])
745                 $parent_id = $current_post;
746  
747         if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
748                 $private = 1;
749         else
750                 $private = $arr['private']; 
751
752         // Set parent id - and also make sure to inherit the parent's ACL's.
753
754         $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
755                 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d WHERE `id` = %d LIMIT 1",
756                 intval($parent_id),
757                 dbesc($allow_cid),
758                 dbesc($allow_gid),
759                 dbesc($deny_cid),
760                 dbesc($deny_gid),
761                 intval($private),
762                 intval($current_post)
763         );
764
765         return $current_post;
766 }
767
768 function get_item_contact($item,$contacts) {
769         if(! count($contacts) || (! is_array($item)))
770                 return false;
771         foreach($contacts as $contact) {
772                 if($contact['id'] == $item['contact-id']) {
773                         return $contact;
774                         break; // NOTREACHED
775                 }
776         }
777         return false;
778 }
779
780
781 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
782
783         $a = get_app();
784
785         if((! strlen($contact['dfrn-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
786                 return 3;
787
788         $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
789
790         if($contact['duplex'] && $contact['dfrn-id'])
791                 $idtosend = '0:' . $orig_id;
792         if($contact['duplex'] && $contact['issued-id'])
793                 $idtosend = '1:' . $orig_id;            
794
795         $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
796
797         $rino_enable = get_config('system','rino_encrypt');
798
799         if(! $rino_enable)
800                 $rino = 0;
801
802         $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
803
804         logger('dfrn_deliver: ' . $url);
805
806         $xml = fetch_url($url);
807
808         $curl_stat = $a->get_curl_code();
809         if(! $curl_stat)
810                 return(-1); // timed out
811
812         logger('dfrn_deliver: ' . $xml);
813
814         if(! $xml)
815                 return 3;
816
817         $res = simplexml_load_string($xml);
818
819         if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
820                 return (($res->status) ? $res->status : 3);
821
822         $postvars     = array();
823         $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
824         $challenge    = hex2bin((string) $res->challenge);
825         $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
826
827         $final_dfrn_id = '';
828
829
830         if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
831                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
832                 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
833         }
834         else {
835                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
836                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
837         }
838
839         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
840
841         if(strpos($final_dfrn_id,':') == 1)
842                 $final_dfrn_id = substr($final_dfrn_id,2);
843
844         if($final_dfrn_id != $orig_id) {
845                 logger('dfrn_deliver: wrong dfrn_id.');
846                 // did not decode properly - cannot trust this site 
847                 return 3;
848         }
849
850         $postvars['dfrn_id']      = $idtosend;
851         $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
852         if($dissolve)
853                 $postvars['dissolve'] = '1';
854
855         if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
856                 $postvars['data'] = $atom;
857         }
858         elseif($owner['page-flags'] == PAGE_COMMUNITY) {
859                 $postvars['data'] = $atom;
860         }
861         else {
862                 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
863         }
864
865         if($rino && $rino_allowed && (! $dissolve)) {
866                 $key = substr(random_string(),0,16);
867                 $data = bin2hex(aes_encrypt($postvars['data'],$key));
868                 $postvars['data'] = $data;
869                 logger('rino: sent key = ' . $key);     
870
871                 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
872                         openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
873                 }
874                 else {
875                         openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
876                 }
877
878                 logger('md5 rawkey ' . md5($postvars['key']));
879
880                 $postvars['key'] = bin2hex($postvars['key']);
881         }
882
883         logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
884
885         $xml = post_url($contact['notify'],$postvars);
886
887         logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
888
889         $curl_stat = $a->get_curl_code();
890         if((! $curl_stat) || (! strlen($xml)))
891                 return(-1); // timed out
892
893         $res = simplexml_load_string($xml);
894
895         return $res->status;
896  
897 }
898
899
900 /*
901  *
902  * consume_feed - process atom feed and update anything/everything we might need to update
903  *
904  * $xml = the (atom) feed to consume - no RSS spoken here, it might partially work since simplepie 
905  *        handles both, but we don't claim it will work well, and are reasonably certain it won't.
906  * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
907  *             It is this person's stuff that is going to be updated.
908  * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
909  *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST 
910  *             have a contact record.
911  * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or 
912  *        might not) try and subscribe to it.
913  *
914  */
915
916 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
917
918         require_once('simplepie/simplepie.inc');
919
920         $feed = new SimplePie();
921         $feed->set_raw_data($xml);
922         if($datedir)
923                 $feed->enable_order_by_date(true);
924         else
925                 $feed->enable_order_by_date(false);
926         $feed->init();
927
928         if($feed->error())
929                 logger('consume_feed: Error parsing XML: ' . $feed->error());
930
931
932         // Check at the feed level for updated contact name and/or photo
933
934         $name_updated  = '';
935         $new_name = '';
936         $photo_timestamp = '';
937         $photo_url = '';
938         $birthday = '';
939
940         $hubs = $feed->get_links('hub');
941
942         if(count($hubs))
943                 $hub = implode(',', $hubs);
944
945         $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
946         if($rawtags) {
947                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
948                 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
949                         $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
950                         $new_name = $elems['name'][0]['data'];
951                 } 
952                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
953                         $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
954                         $photo_url = $elems['link'][0]['attribs']['']['href'];
955                 }
956
957                 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
958                         $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
959                 }
960         }
961
962         if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
963                 logger('consume_feed: Updating photo for ' . $contact['name']);
964                 require_once("Photo.php");
965                 $photo_failure = false;
966                 $have_photo = false;
967
968                 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
969                         intval($contact['id']),
970                         intval($contact['uid'])
971                 );
972                 if(count($r)) {
973                         $resource_id = $r[0]['resource-id'];
974                         $have_photo = true;
975                 }
976                 else {
977                         $resource_id = photo_new_resource();
978                 }
979                         
980                 $img_str = fetch_url($photo_url,true);
981                 $img = new Photo($img_str);
982                 if($img->is_valid()) {
983                         if($have_photo) {
984                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
985                                         dbesc($resource_id),
986                                         intval($contact['id']),
987                                         intval($contact['uid'])
988                                 );
989                         }
990                                 
991                         $img->scaleImageSquare(175);
992                                 
993                         $hash = $resource_id;
994                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
995                                 
996                         $img->scaleImage(80);
997                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
998
999                         $img->scaleImage(48);
1000                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 6);
1001
1002                         $a = get_app();
1003
1004                         q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'  
1005                                 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1006                                 dbesc(datetime_convert()),
1007                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
1008                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
1009                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
1010                                 intval($contact['uid']),
1011                                 intval($contact['id'])
1012                         );
1013                 }
1014         }
1015
1016         if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1017                 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1018                         dbesc(notags(trim($new_name))),
1019                         dbesc(datetime_convert()),
1020                         intval($contact['uid']),
1021                         intval($contact['id'])
1022                 );
1023         }
1024
1025         if(strlen($birthday)) {
1026                 if(substr($birthday,0,4) != $contact['bdyear']) {
1027                         logger('consume_feed: updating birthday: ' . $birthday);
1028
1029                         /**
1030                          *
1031                          * Add new birthday event for this person
1032                          *
1033                          * $bdtext is just a readable placeholder in case the event is shared
1034                          * with others. We will replace it during presentation to our $importer
1035                          * to contain a sparkle link and perhaps a photo. 
1036                          *
1037                          */
1038                          
1039                         $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1040
1041
1042                         $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1043                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1044                                 intval($contact['uid']),
1045                                 intval($contact['id']),
1046                                 dbesc(datetime_convert()),
1047                                 dbesc(datetime_convert()),
1048                                 dbesc(datetime_convert('UTC','UTC', $birthday)),
1049                                 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1050                                 dbesc($bdtext),
1051                                 dbesc('birthday')
1052                         );
1053                         
1054
1055                         // update bdyear
1056
1057                         q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1058                                 dbesc(substr($birthday,0,4)),
1059                                 intval($contact['uid']),
1060                                 intval($contact['id'])
1061                         );
1062
1063                         // This function is called twice without reloading the contact
1064                         // Make sure we only create one event. This is why &$contact 
1065                         // is a reference var in this function
1066
1067                         $contact['bdyear'] = substr($birthday,0,4);
1068                 }
1069
1070         }
1071
1072         // Now process the feed
1073         if($feed->get_item_quantity()) {                
1074
1075         // in inverse date order
1076                 if ($datedir)
1077                         $items = array_reverse($feed->get_items());
1078                 else
1079                         $items = $feed->get_items();
1080
1081                 foreach($items as $item) {
1082
1083                         $deleted = false;
1084
1085                         $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
1086                         if(isset($rawdelete[0]['attribs']['']['ref'])) {
1087                                 $uri = $rawthread[0]['attribs']['']['ref'];
1088                                 $deleted = true;
1089                                 if(isset($rawdelete[0]['attribs']['']['when'])) {
1090                                         $when = $rawthread[0]['attribs']['']['when'];
1091                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1092                                 }
1093                                 else
1094                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1095                         }
1096                         if($deleted && is_array($contact)) {
1097                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1098                                         dbesc($uri),
1099                                         intval($importer['uid']),
1100                                         intval($contact['id'])
1101                                 );
1102                                 if(count($r)) {
1103                                         $item = $r[0];
1104                                         if($item['uri'] == $item['parent-uri']) {
1105                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1106                                                         `body` = '', `title` = ''
1107                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
1108                                                         dbesc($when),
1109                                                         dbesc(datetime_convert()),
1110                                                         dbesc($item['uri']),
1111                                                         intval($importer['uid'])
1112                                                 );
1113                                         }
1114                                         else {
1115                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1116                                                         `body` = '', `title` = '' 
1117                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1118                                                         dbesc($when),
1119                                                         dbesc(datetime_convert()),
1120                                                         dbesc($uri),
1121                                                         intval($importer['uid'])
1122                                                 );
1123                                                 if($item['last-child']) {
1124                                                         // ensure that last-child is set in case the comment that had it just got wiped.
1125                                                         $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1126                                                                 dbesc(datetime_convert()),
1127                                                                 dbesc($item['parent-uri']),
1128                                                                 intval($item['uid'])
1129                                                         );
1130                                                         // who is the last child now? 
1131                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d 
1132                                                                 ORDER BY `created` DESC LIMIT 1",
1133                                                                         dbesc($item['parent-uri']),
1134                                                                         intval($importer['uid'])
1135                                                         );
1136                                                         if(count($r)) {
1137                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1138                                                                         intval($r[0]['id'])
1139                                                                 );
1140                                                         }
1141                                                 }       
1142                                         }
1143                                 }       
1144                                 continue;
1145                         }
1146
1147
1148                         $is_reply = false;              
1149                         $item_id = $item->get_id();
1150                         $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1151                         if(isset($rawthread[0]['attribs']['']['ref'])) {
1152                                 $is_reply = true;
1153                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1154                         }
1155
1156
1157                         if(($is_reply) && is_array($contact)) {
1158         
1159                                 // Have we seen it? If not, import it.
1160         
1161                                 $item_id = $item->get_id();
1162         
1163                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1164                                         dbesc($item_id),
1165                                         intval($importer['uid'])
1166                                 );
1167                                 // FIXME update content if 'updated' changes
1168                                 if(count($r)) {
1169                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1170                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1171                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1172                                                         dbesc(datetime_convert()),
1173                                                         dbesc($parent_uri),
1174                                                         intval($importer['uid'])
1175                                                 );
1176                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1177                                                         intval($allow[0]['data']),
1178                                                         dbesc(datetime_convert()),
1179                                                         dbesc($item_id),
1180                                                         intval($importer['uid'])
1181                                                 );
1182                                         }
1183                                         continue;
1184                                 }
1185                                 $datarray = get_atom_elements($feed,$item);
1186
1187                                 if($contact['network'] === 'stat') {
1188                                         if(strlen($datarray['title']))
1189                                                 unset($datarray['title']);
1190                                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1191                                                 dbesc(datetime_convert()),
1192                                                 dbesc($parent_uri),
1193                                                 intval($importer['uid'])
1194                                         );
1195                                         $datarray['last-child'] = 1;
1196                                 }
1197
1198                                 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1199                                         // one way feed - no remote comment ability
1200                                         $datarray['last-child'] = 0;
1201                                 }
1202                                 $datarray['parent-uri'] = $parent_uri;
1203                                 $datarray['uid'] = $importer['uid'];
1204                                 $datarray['contact-id'] = $contact['id'];
1205                                 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1206                                         $datarray['type'] = 'activity';
1207                                         $datarray['gravity'] = GRAVITY_LIKE;
1208                                 }
1209
1210                                 $r = item_store($datarray);
1211                                 continue;
1212                         }
1213
1214                         else {
1215                                 // Head post of a conversation. Have we seen it? If not, import it.
1216
1217                                 $item_id = $item->get_id();
1218                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1219                                         dbesc($item_id),
1220                                         intval($importer['uid'])
1221                                 );
1222                                 if(count($r)) {
1223                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1224                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1225                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1226                                                         intval($allow[0]['data']),
1227                                                         dbesc(datetime_convert()),
1228                                                         dbesc($item_id),
1229                                                         intval($importer['uid'])
1230                                                 );
1231                                         }
1232                                         continue;
1233                                 }
1234                                 $datarray = get_atom_elements($feed,$item);
1235
1236                                 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1237                                         logger('consume-feed: New follower');
1238                                         new_follower($importer,$contact,$datarray,$item);
1239                                         return;
1240                                 }
1241                                 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW))  {
1242                                         lose_follower($importer,$contact,$datarray,$item);
1243                                         return;
1244                                 }
1245                                 if(! is_array($contact))
1246                                         return;
1247
1248                                 if($contact['network'] === 'stat') {
1249                                         if(strlen($datarray['title']))
1250                                                 unset($datarray['title']);
1251                                         $datarray['last-child'] = 1;
1252                                 }
1253
1254                                 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1255                                         // one way feed - no remote comment ability
1256                                         $datarray['last-child'] = 0;
1257                                 }
1258
1259                                 $datarray['parent-uri'] = $item_id;
1260                                 $datarray['uid'] = $importer['uid'];
1261                                 $datarray['contact-id'] = $contact['id'];
1262                                 $r = item_store($datarray);
1263                                 continue;
1264
1265                         }
1266                 }
1267         }
1268 }
1269
1270 function new_follower($importer,$contact,$datarray,$item) {
1271         $url = notags(trim($datarray['author-link']));
1272         $name = notags(trim($datarray['author-name']));
1273         $photo = notags(trim($datarray['author-avatar']));
1274
1275         $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
1276         if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
1277                 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
1278
1279         if(is_array($contact)) {
1280                 if($contact['network'] == 'stat' && $contact['rel'] == REL_FAN) {
1281                         $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
1282                                 intval(REL_BUD),
1283                                 intval($contact['id']),
1284                                 intval($importer['uid'])
1285                         );
1286                 }
1287
1288                 // send email notification to owner?
1289         }
1290         else {
1291         
1292                 // create contact record - set to readonly
1293
1294                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`, 
1295                         `blocked`, `readonly`, `pending` )
1296                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
1297                         intval($importer['uid']),
1298                         dbesc(datetime_convert()),
1299                         dbesc($url),
1300                         dbesc($name),
1301                         dbesc($nick),
1302                         dbesc($photo),
1303                         dbesc('stat'),
1304                         intval(REL_VIP)
1305                 );
1306                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
1307                                 intval($importer['uid']),
1308                                 dbesc($url),
1309                                 intval(REL_VIP)
1310                 );
1311                 if(count($r))
1312                                 $contact_record = $r[0];
1313
1314                 // create notification  
1315                 $hash = random_string();
1316
1317                 if(is_array($contact_record)) {
1318                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
1319                                 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
1320                                 intval($importer['uid']),
1321                                 intval($contact_record['id']),
1322                                 dbesc($hash),
1323                                 dbesc(datetime_convert())
1324                         );
1325                 }
1326                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1327                         intval($importer['uid'])
1328                 );
1329                 $a = get_app();
1330                 if(count($r)) {
1331                         if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
1332                                 $email_tpl = load_view_file('view/follow_notify_eml.tpl');
1333                                 $email = replace_macros($email_tpl, array(
1334                                         '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
1335                                         '$url' => $url,
1336                                         '$myname' => $r[0]['username'],
1337                                         '$siteurl' => $a->get_baseurl(),
1338                                         '$sitename' => $a->config['sitename']
1339                                 ));
1340                                 $res = mail($r[0]['email'], 
1341                                         t("You have a new follower at ") . $a->config['sitename'],
1342                                         $email,
1343                                         'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
1344                         
1345                         }
1346                 }
1347         }
1348 }
1349
1350 function lose_follower($importer,$contact,$datarray,$item) {
1351
1352         if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
1353                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1354                         intval(REL_FAN),
1355                         intval($contact['id'])
1356                 );
1357         }
1358         else {
1359                 contact_remove($contact['id']);
1360         }
1361 }
1362
1363
1364 function subscribe_to_hub($url,$importer,$contact) {
1365
1366         if(is_array($importer)) {
1367                 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1368                         intval($importer['uid'])
1369                 );
1370         }
1371         if(! count($r))
1372                 return;
1373
1374         $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1375
1376         // Use a single verify token, even if multiple hubs
1377
1378         $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1379
1380         $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1381
1382         logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
1383
1384         if(! strlen($contact['hub-verify'])) {
1385                 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
1386                         dbesc($verify_token),
1387                         intval($contact['id'])
1388                 );
1389         }
1390
1391         post_url($url,$params);                 
1392         return;
1393
1394 }
1395
1396
1397 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1398         $o = '';
1399         if(! $tag)
1400                 return $o;
1401         $name = xmlify($name);
1402         $uri = xmlify($uri);
1403         $h = intval($h);
1404         $w = intval($w);
1405         $photo = xmlify($photo);
1406
1407
1408         $o .= "<$tag>\r\n";
1409         $o .= "<name>$name</name>\r\n";
1410         $o .= "<uri>$uri</uri>\r\n";
1411         $o .= '<link rel="photo"  type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1412         $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1413
1414         call_hooks('atom_author', $o);
1415
1416         $o .= "</$tag>\r\n";
1417         return $o;
1418 }
1419
1420 function atom_entry($item,$type,$author,$owner,$comment = false) {
1421
1422         if($item['deleted'])
1423                 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1424
1425         $a = get_app();
1426
1427         $o = "\r\n\r\n<entry>\r\n";
1428
1429         if(is_array($author))
1430                 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1431         else
1432                 $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
1433         if(strlen($item['owner-name']))
1434                 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1435
1436         if($item['parent'] != $item['id'])
1437                 $o .= '<thr:in-reply-to ref="' . xmlify($item['parent-uri']) . '" type="text/html" href="' .  xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1438
1439         $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1440         $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1441         $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1442         $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1443         $o .= '<dfrn:env>' . base64url_encode($item['body'], true) . '</dfrn:env>' . "\r\n";
1444         $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
1445         $o .= '<link rel="alternate" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1446         if($comment)
1447                 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1448
1449         if($item['location']) {
1450                 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1451                 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
1452         }
1453
1454         if($item['coord'])
1455                 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1456
1457         if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
1458                 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
1459
1460         $verb = construct_verb($item);
1461         $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1462         $actobj = construct_activity_object($item);
1463         if(strlen($actobj))
1464                 $o .= $actobj;
1465         $actarg = construct_activity_target($item);
1466         if(strlen($actarg))
1467                 $o .= $actarg;
1468
1469         $mentioned = get_mentions($item);
1470         if($mentioned)
1471                 $o .= $mentioned;
1472         
1473         call_hooks('atom_entry', $o);
1474
1475         $o .= '</entry>' . "\r\n";
1476         
1477         return $o;
1478 }
1479