3 require_once('bbcode.php');
5 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
8 // default permissions - anonymous user
17 if(strlen($owner_nick) && ! intval($owner_nick)) {
18 $r = q("SELECT `uid`, `nickname`, `timezone` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
22 $owner_id = $r[0]['uid'];
23 $owner_nick = $r[0]['nickname'];
24 $owner_tz = $r[0]['timezone'];
28 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
33 $owner['nickname'] = $owner_nick;
41 * Determine the next birthday, but only if the birthday is published
42 * in the default profile. We _could_ also look for a private profile that the
43 * recipient can see, but somebody could get mad at us if they start getting
44 * public birthday greetings when they haven't made this info public.
46 * Assuming we are able to publish this info, we are then going to convert
47 * the start time from the owner's timezone to UTC.
49 * This will potentially solve the problem found with some social networks
50 * where birthdays are converted to the viewer's timezone and salutations from
51 * elsewhere in the world show up on the wrong day. We will convert it to the
52 * viewer's timezone also, but first we are going to convert it from the birthday
53 * person's timezone to GMT - so the viewer may find the birthday starting at
54 * 6:00PM the day before, but that will correspond to midnight to the birthday person.
60 $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
65 $tmp_dob = substr($p[0]['dob'],5);
66 if(intval($tmp_dob)) {
67 $y = datetime_convert($owner_tz,$owner_tz,'now','Y');
68 $bd = $y . '-' . $tmp_dob . ' 00:00';
69 $t_dob = strtotime($bd);
70 $now = strtotime(datetime_convert($owner_tz,$owner_tz,'now'));
72 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
73 $birthday = datetime_convert($owner_tz,'UTC',$bd,ATOM_TIME);
77 if($dfrn_id && $dfrn_id != '*') {
82 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
86 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
87 $my_id = '1:' . $dfrn_id;
90 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
91 $my_id = '0:' . $dfrn_id;
98 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
106 $groups = init_groups_visitor($contact['id']);
109 for($x = 0; $x < count($groups); $x ++)
110 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
111 $gs = implode('|', $groups);
114 $gs = '<<>>' ; // Impossible to match
116 $sql_extra = sprintf("
117 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
118 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
119 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
120 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
122 intval($contact['id']),
123 intval($contact['id']),
129 if($dfrn_id === '' || $dfrn_id === '*')
134 if(! strlen($last_update))
135 $last_update = 'now -30 days';
137 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
139 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
140 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
141 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
142 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
143 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
144 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
145 WHERE `item`.`uid` = %d AND `item`.`visible` = 1
146 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
147 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
149 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
156 // Will check further below if this actually returned results.
157 // We will provide an empty feed in any case.
161 $feed_template = load_view_file('view/atom_feed.tpl');
165 $hub = get_config('system','huburl');
169 $hubs = explode(',', $hub);
171 foreach($hubs as $h) {
175 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
180 $salmon = '<link rel="salmon" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ;
181 $salmon .= '<link rel="http://salmon-protocol.org/ns/salmon-replies" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ;
182 $salmon .= '<link rel="http://salmon-protocol.org/ns/salmon-mention" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ;
185 $atom .= replace_macros($feed_template, array(
186 '$version' => xmlify(FRIENDIKA_VERSION),
187 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
188 '$feed_title' => xmlify($owner['name']),
189 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
191 '$salmon' => $salmon,
192 '$name' => xmlify($owner['name']),
193 '$profile_page' => xmlify($owner['url']),
194 '$photo' => xmlify($owner['photo']),
195 '$thumb' => xmlify($owner['thumb']),
196 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
197 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
198 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
199 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : '')
202 call_hooks('atom_feed', $atom);
204 if(! count($items)) {
206 call_hooks('atom_feed_end', $atom);
208 $atom .= '</feed>' . "\r\n";
212 foreach($items as $item) {
214 // public feeds get html, our own nodes use bbcode
216 if($dfrn_id === '*') {
223 $atom .= atom_entry($item,$type,null,$owner,true);
226 call_hooks('atom_feed_end', $atom);
228 $atom .= '</feed>' . "\r\n";
234 function construct_verb($item) {
236 return $item['verb'];
237 return ACTIVITY_POST;
240 function construct_activity_object($item) {
242 if($item['object']) {
243 $o = '<as:object>' . "\r\n";
244 $r = @simplexml_load_string($item['object']);
246 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
248 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
250 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
252 if(substr($r->link,0,1) === '<')
255 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
258 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
259 $o .= '</as:object>' . "\r\n";
266 function construct_activity_target($item) {
268 if($item['target']) {
269 $o = '<as:target>' . "\r\n";
270 $r = @simplexml_load_string($item['target']);
272 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
274 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
276 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
278 if(substr($r->link,0,1) === '<')
281 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
284 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
285 $o .= '</as:target>' . "\r\n";
295 function get_atom_elements($feed,$item) {
297 require_once('library/HTMLPurifier.auto.php');
298 require_once('include/html2bbcode.php');
300 $best_photo = array();
304 $author = $item->get_author();
305 $res['author-name'] = unxmlify($author->get_name());
306 $res['author-link'] = unxmlify($author->get_link());
307 $res['uri'] = unxmlify($item->get_id());
308 $res['title'] = unxmlify($item->get_title());
309 $res['body'] = unxmlify($item->get_content());
312 // look for a photo. We should check media size and find the best one,
313 // but for now let's just find any author photo
315 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
317 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
318 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
319 foreach($base as $link) {
320 if(! $res['author-avatar']) {
321 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
322 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
327 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
329 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
330 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
331 if($base && count($base)) {
332 foreach($base as $link) {
333 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
334 $res['author-link'] = unxmlify($link['attribs']['']['href']);
335 if(! $res['author-avatar']) {
336 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
337 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
343 // No photo/profile-link on the item - look at the feed level
345 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
346 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
347 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
348 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
349 foreach($base as $link) {
350 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
351 $res['author-link'] = unxmlify($link['attribs']['']['href']);
352 if(! $res['author-avatar']) {
353 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
354 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
359 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
361 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
362 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
364 if($base && count($base)) {
365 foreach($base as $link) {
366 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
367 $res['author-link'] = unxmlify($link['attribs']['']['href']);
368 if(! (x($res,'author-avatar'))) {
369 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
370 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
378 $maxlen = get_max_import_size();
379 if($maxlen && (strlen($res['body']) > $maxlen))
380 $res['body'] = substr($res['body'],0, $maxlen);
382 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
383 // the content type. Our own network only emits text normally, though it might have been converted to
384 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
385 // have to assume it is all html and needs to be purified.
387 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
388 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
389 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
393 if((strpos($res['body'],'<')) || (strpos($res['body'],'>'))) {
395 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
396 '[youtube]$1[/youtube]', $res['body']);
398 $config = HTMLPurifier_Config::createDefault();
399 $config->set('Cache.DefinitionImpl', null);
401 // we shouldn't need a whitelist, because the bbcode converter
402 // will strip out any unsupported tags.
403 // $config->set('HTML.Allowed', 'p,b,a[href],i');
405 $purifier = new HTMLPurifier($config);
406 $res['body'] = $purifier->purify($res['body']);
408 $res['body'] = html2bbcode($res['body']);
411 $res['body'] = escape_tags($res['body']);
414 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
415 if($allow && $allow[0]['data'] == 1)
416 $res['last-child'] = 1;
418 $res['last-child'] = 0;
420 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
421 if($private && $private[0]['data'] == 1)
426 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
428 $res['created'] = unxmlify($rawcreated[0]['data']);
430 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
432 $res['location'] = unxmlify($rawlocation[0]['data']);
435 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
437 $res['edited'] = unxmlify($rawcreated[0]['data']);
439 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
440 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
441 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
442 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
443 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
444 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
445 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
446 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
447 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
449 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
450 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
452 foreach($base as $link) {
453 if(! $res['owner-avatar']) {
454 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
455 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
460 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
462 $res['coord'] = unxmlify($rawgeo[0]['data']);
465 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
467 // select between supported verbs
470 $res['verb'] = unxmlify($rawverb[0]['data']);
473 // translate OStatus unfollow to activity streams if it happened to get selected
475 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
476 $res['verb'] = ACTIVITY_UNFOLLOW;
480 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
483 $res['object'] = '<object>' . "\n";
484 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
485 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
486 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
488 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
489 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
490 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
491 $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
492 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
493 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
494 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
495 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
497 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
498 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
499 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
500 if((strpos($body,'<')) || (strpos($body,'>'))) {
502 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
503 '[youtube]$1[/youtube]', $body);
505 $config = HTMLPurifier_Config::createDefault();
506 $config->set('Cache.DefinitionImpl', null);
508 $purifier = new HTMLPurifier($config);
509 $body = $purifier->purify($body);
510 $body = html2bbcode($body);
513 $body = escape_tags($body);
515 $res['object'] .= '<content>' . $body . '</content>' . "\n";
518 $res['object'] .= '</object>' . "\n";
521 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
524 $res['target'] = '<target>' . "\n";
525 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
526 $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
528 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
529 $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
531 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
532 $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
533 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
534 $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
535 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
536 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
538 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
539 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
540 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
541 if((strpos($body,'<')) || (strpos($body,'>'))) {
543 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
544 '[youtube]$1[/youtube]', $body);
546 $config = HTMLPurifier_Config::createDefault();
547 $config->set('Cache.DefinitionImpl', null);
549 $purifier = new HTMLPurifier($config);
550 $body = $purifier->purify($body);
551 $body = html2bbcode($body);
554 $body = escape_tags($body);
556 $res['target'] .= '<content>' . $body . '</content>' . "\n";
559 $res['target'] .= '</target>' . "\n";
562 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
564 call_hooks('parse_atom', $arr);
569 function encode_rel_links($links) {
571 if(! ((is_array($links)) && (count($links))))
573 foreach($links as $link) {
575 if($link['attribs']['']['rel'])
576 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
577 if($link['attribs']['']['type'])
578 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
579 if($link['attribs']['']['href'])
580 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
581 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
582 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
583 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
584 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
590 function item_store($arr) {
593 $arr['gravity'] = intval($arr['gravity']);
594 elseif($arr['parent-uri'] == $arr['uri'])
596 elseif(activity_match($arr['verb'],ACTIVITY_POST))
599 $arr['gravity'] = 6; // extensible catchall
602 $arr['type'] = 'remote';
603 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
604 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
605 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
606 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
607 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
608 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
609 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
610 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
611 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
612 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
613 $arr['changed'] = datetime_convert();
614 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
615 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
616 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
617 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
618 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
620 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
621 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
622 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
623 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
624 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
625 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
626 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
627 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
628 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
629 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
630 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
631 $arr['body'] = ((x($arr,'body')) ? escape_tags(trim($arr['body'])) : '');
633 // The content body has been through a lot of filtering and transport escaping by now.
634 // We don't want to skip any filters, however a side effect of all this filtering
635 // is that ampersands and <> may have been double encoded, depending on which filter chain
636 // they came through.
638 $arr['body'] = str_replace(
639 array('&amp;', '&gt;', '&lt;', '&quot;'),
640 array('&' , '>' , '<', '"'),
646 if($arr['parent-uri'] === $arr['uri']) {
648 $allow_cid = $arr['allow_cid'];
649 $allow_gid = $arr['allow_gid'];
650 $deny_cid = $arr['deny_cid'];
651 $deny_gid = $arr['deny_gid'];
655 // find the parent and snarf the item id and ACL's
657 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
658 dbesc($arr['parent-uri']),
664 // is the new message multi-level threaded?
665 // even though we don't support it now, preserve the info
666 // and re-attach to the conversation parent.
668 if($r[0]['uri'] != $r[0]['parent-uri']) {
669 $arr['thr-parent'] = $arr['parent-uri'];
670 $arr['parent-uri'] = $r[0]['parent-uri'];
673 $parent_id = $r[0]['id'];
674 $allow_cid = $r[0]['allow_cid'];
675 $allow_gid = $r[0]['allow_gid'];
676 $deny_cid = $r[0]['deny_cid'];
677 $deny_gid = $r[0]['deny_gid'];
680 logger('item_store: item parent was not found - ignoring item');
685 call_hooks('post_remote',$arr);
689 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
691 $r = dbq("INSERT INTO `item` (`"
692 . implode("`, `", array_keys($arr))
694 . implode("', '", array_values($arr))
697 // find the item we just created
699 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
700 $arr['uri'], // already dbesc'd
704 $current_post = $r[0]['id'];
705 logger('item_store: created item ' . $current_post);
708 logger('item_store: could not locate created item');
712 if($arr['parent-uri'] === $arr['uri'])
713 $parent_id = $current_post;
715 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
718 $private = $arr['private'];
720 // Set parent id - and also make sure to inherit the parent's ACL's.
722 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
723 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d WHERE `id` = %d LIMIT 1",
730 intval($current_post)
733 return $current_post;
736 function get_item_contact($item,$contacts) {
737 if(! count($contacts) || (! is_array($item)))
739 foreach($contacts as $contact) {
740 if($contact['id'] == $item['contact-id']) {
749 function dfrn_deliver($owner,$contact,$atom) {
753 if((! strlen($contact['dfrn-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
756 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
758 if($contact['duplex'] && $contact['dfrn-id'])
759 $idtosend = '0:' . $orig_id;
760 if($contact['duplex'] && $contact['issued-id'])
761 $idtosend = '1:' . $orig_id;
763 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
765 $rino_enable = get_config('system','rino_encrypt');
770 $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
772 logger('dfrn_deliver: ' . $url);
774 $xml = fetch_url($url);
776 $curl_stat = $a->get_curl_code();
778 return(-1); // timed out
780 logger('dfrn_deliver: ' . $xml);
785 $res = simplexml_load_string($xml);
787 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
788 return (($res->status) ? $res->status : 3);
791 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
792 $challenge = hex2bin((string) $res->challenge);
793 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
798 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
799 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
800 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
803 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
804 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
807 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
809 if(strpos($final_dfrn_id,':') == 1)
810 $final_dfrn_id = substr($final_dfrn_id,2);
812 if($final_dfrn_id != $orig_id) {
813 logger('dfrn_deliver: wrong dfrn_id.');
814 // did not decode properly - cannot trust this site
818 $postvars['dfrn_id'] = $idtosend;
819 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
821 if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
822 $postvars['data'] = $atom;
824 elseif($owner['page-flags'] == PAGE_COMMUNITY) {
825 $postvars['data'] = $atom;
828 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
831 if($rino && $rino_allowed) {
832 $key = substr(random_string(),0,16);
833 $data = bin2hex(aes_encrypt($postvars['data'],$key));
834 $postvars['data'] = $data;
835 logger('rino: sent key = ' . $key);
837 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
838 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
841 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
844 logger('md5 rawkey ' . md5($postvars['key']));
846 $postvars['key'] = bin2hex($postvars['key']);
849 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
851 $xml = post_url($contact['notify'],$postvars);
853 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
855 $curl_stat = $a->get_curl_code();
856 if((! $curl_stat) || (! strlen($xml)))
857 return(-1); // timed out
859 $res = simplexml_load_string($xml);
868 * consume_feed - process atom feed and update anything/everything we might need to update
870 * $xml = the (atom) feed to consume - no RSS spoken here, it might partially work since simplepie
871 * handles both, but we don't claim it will work well, and are reasonably certain it won't.
872 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
873 * It is this person's stuff that is going to be updated.
874 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
875 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
876 * have a contact record.
877 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
878 * might not) try and subscribe to it.
882 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
884 require_once('simplepie/simplepie.inc');
886 $feed = new SimplePie();
887 $feed->set_raw_data($xml);
889 $feed->enable_order_by_date(true);
891 $feed->enable_order_by_date(false);
894 // Check at the feed level for updated contact name and/or photo
898 $photo_timestamp = '';
902 $hubs = $feed->get_links('hub');
905 $hub = implode(',', $hubs);
907 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
909 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
910 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
911 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
912 $new_name = $elems['name'][0]['data'];
914 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
915 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
916 $photo_url = $elems['link'][0]['attribs']['']['href'];
919 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
920 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
924 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
925 logger('consume_feed: Updating photo for ' . $contact['name']);
926 require_once("Photo.php");
927 $photo_failure = false;
930 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
931 intval($contact['id']),
932 intval($contact['uid'])
935 $resource_id = $r[0]['resource-id'];
939 $resource_id = photo_new_resource();
942 $img_str = fetch_url($photo_url,true);
943 $img = new Photo($img_str);
944 if($img->is_valid()) {
946 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
948 intval($contact['id']),
949 intval($contact['uid'])
953 $img->scaleImageSquare(175);
955 $hash = $resource_id;
956 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
958 $img->scaleImage(80);
959 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
961 $img->scaleImage(48);
962 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 6);
966 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
967 WHERE `uid` = %d AND `id` = %d LIMIT 1",
968 dbesc(datetime_convert()),
969 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
970 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
971 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
972 intval($contact['uid']),
973 intval($contact['id'])
978 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
979 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
980 dbesc(notags(trim($new_name))),
981 dbesc(datetime_convert()),
982 intval($contact['uid']),
983 intval($contact['id'])
987 if(strlen($birthday)) {
988 if(substr($birthday,0,4) != $contact['bdyear']) {
989 logger('consume_feed: updating birthday: ' . $birthday);
993 * Add new birthday event for this person
995 * $bdtext is just a readable placeholder in case the event is shared
996 * with others. We will replace it during presentation to our $importer
997 * to contain a sparkle link and perhaps a photo.
1001 $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1004 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1005 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1006 intval($contact['uid']),
1007 intval($contact['id']),
1008 dbesc(datetime_convert()),
1009 dbesc(datetime_convert()),
1010 dbesc(datetime_convert('UTC','UTC', $birthday)),
1011 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1019 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1020 dbesc(substr($birthday,0,4)),
1021 intval($contact['uid']),
1022 intval($contact['id'])
1025 // This function is called twice without reloading the contact
1026 // Make sure we only create one event. This is why &$contact
1027 // is a reference var in this function
1029 $contact['bdyear'] = substr($birthday,0,4);
1034 // Now process the feed
1035 if($feed->get_item_quantity()) {
1037 // in inverse date order
1039 $items = array_reverse($feed->get_items());
1041 $items = $feed->get_items();
1043 foreach($items as $item) {
1047 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
1048 if(isset($rawdelete[0]['attribs']['']['ref'])) {
1049 $uri = $rawthread[0]['attribs']['']['ref'];
1051 if(isset($rawdelete[0]['attribs']['']['when'])) {
1052 $when = $rawthread[0]['attribs']['']['when'];
1053 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1056 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1058 if($deleted && is_array($contact)) {
1059 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1061 intval($importer['uid']),
1062 intval($contact['id'])
1066 if($item['uri'] == $item['parent-uri']) {
1067 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1068 `body` = '', `title` = ''
1069 WHERE `parent-uri` = '%s' AND `uid` = %d",
1071 dbesc(datetime_convert()),
1072 dbesc($item['uri']),
1073 intval($importer['uid'])
1077 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1078 `body` = '', `title` = ''
1079 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1081 dbesc(datetime_convert()),
1083 intval($importer['uid'])
1085 if($item['last-child']) {
1086 // ensure that last-child is set in case the comment that had it just got wiped.
1087 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1088 dbesc(datetime_convert()),
1089 dbesc($item['parent-uri']),
1090 intval($item['uid'])
1092 // who is the last child now?
1093 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
1094 ORDER BY `created` DESC LIMIT 1",
1095 dbesc($item['parent-uri']),
1096 intval($importer['uid'])
1099 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1111 $item_id = $item->get_id();
1112 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1113 if(isset($rawthread[0]['attribs']['']['ref'])) {
1115 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1119 if(($is_reply) && is_array($contact)) {
1121 // Have we seen it? If not, import it.
1123 $item_id = $item->get_id();
1125 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1127 intval($importer['uid'])
1129 // FIXME update content if 'updated' changes
1131 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1132 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1133 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1134 dbesc(datetime_convert()),
1136 intval($importer['uid'])
1138 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1139 intval($allow[0]['data']),
1140 dbesc(datetime_convert()),
1142 intval($importer['uid'])
1147 $datarray = get_atom_elements($feed,$item);
1148 if($contact['network'] === 'stat') {
1149 if(strlen($datarray['title']))
1150 unset($datarray['title']);
1151 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1152 dbesc(datetime_convert()),
1154 intval($importer['uid'])
1156 $datarray['last-child'] = 1;
1158 $datarray['parent-uri'] = $parent_uri;
1159 $datarray['uid'] = $importer['uid'];
1160 $datarray['contact-id'] = $contact['id'];
1161 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1162 $datarray['type'] = 'activity';
1163 $datarray['gravity'] = GRAVITY_LIKE;
1166 $r = item_store($datarray);
1171 // Head post of a conversation. Have we seen it? If not, import it.
1173 $item_id = $item->get_id();
1174 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1176 intval($importer['uid'])
1179 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1180 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1181 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1182 intval($allow[0]['data']),
1183 dbesc(datetime_convert()),
1185 intval($importer['uid'])
1190 $datarray = get_atom_elements($feed,$item);
1192 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1193 logger('consume-feed: New follower');
1194 new_follower($importer,$contact,$datarray,$item);
1197 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
1198 lose_follower($importer,$contact,$datarray,$item);
1201 if(! is_array($contact))
1204 if($contact['network'] === 'stat') {
1205 if(strlen($datarray['title']))
1206 unset($datarray['title']);
1207 $datarray['last-child'] = 1;
1210 $datarray['parent-uri'] = $item_id;
1211 $datarray['uid'] = $importer['uid'];
1212 $datarray['contact-id'] = $contact['id'];
1213 $r = item_store($datarray);
1222 function new_follower($importer,$contact,$datarray,$item) {
1223 $url = notags(trim($datarray['author-link']));
1224 $name = notags(trim($datarray['author-name']));
1225 $photo = notags(trim($datarray['author-avatar']));
1227 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
1228 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
1229 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
1231 if(is_array($contact)) {
1232 if($contact['network'] == 'stat' && $contact['rel'] == REL_FAN) {
1233 $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
1235 intval($contact['id']),
1236 intval($importer['uid'])
1240 // send email notification to owner?
1244 // create contact record - set to readonly
1246 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
1247 `blocked`, `readonly`, `pending` )
1248 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
1249 intval($importer['uid']),
1250 dbesc(datetime_convert()),
1258 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
1259 intval($importer['uid']),
1264 $contact_record = $r[0];
1266 // create notification
1267 $hash = random_string();
1269 if(is_array($contact_record)) {
1270 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
1271 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
1272 intval($importer['uid']),
1273 intval($contact_record['id']),
1275 dbesc(datetime_convert())
1278 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1279 intval($importer['uid'])
1283 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
1284 $email_tpl = load_view_file('view/follow_notify_eml.tpl');
1285 $email = replace_macros($email_tpl, array(
1286 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
1288 '$myname' => $r[0]['username'],
1289 '$siteurl' => $a->get_baseurl(),
1290 '$sitename' => $a->config['sitename']
1292 $res = mail($r[0]['email'],
1293 t("You have a new follower at ") . $a->config['sitename'],
1295 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
1302 function lose_follower($importer,$contact,$datarray,$item) {
1304 if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
1305 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1307 intval($contact['id'])
1311 contact_remove($contact['id']);
1316 function subscribe_to_hub($url,$importer,$contact) {
1318 if(is_array($importer)) {
1319 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1320 intval($importer['uid'])
1326 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1328 // Use a single verify token, even if multiple hubs
1330 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1332 $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1334 logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
1336 if(! strlen($contact['hub-verify'])) {
1337 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
1338 dbesc($verify_token),
1339 intval($contact['id'])
1343 post_url($url,$params);
1349 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1353 $name = xmlify($name);
1354 $uri = xmlify($uri);
1357 $photo = xmlify($photo);
1361 $o .= "<name>$name</name>\r\n";
1362 $o .= "<uri>$uri</uri>\r\n";
1363 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1364 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1366 call_hooks('atom_author', $o);
1368 $o .= "</$tag>\r\n";
1372 function atom_entry($item,$type,$author,$owner,$comment = false) {
1374 if($item['deleted'])
1375 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1379 $o = "\r\n\r\n<entry>\r\n";
1381 if(is_array($author))
1382 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1384 $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
1385 if(strlen($item['owner-name']))
1386 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1388 if($item['parent'] != $item['id'])
1389 $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";
1391 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1392 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1393 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1394 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1395 $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
1396 $o .= '<link rel="alternate" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1398 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1400 if($item['location']) {
1401 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1402 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
1406 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1408 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
1409 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
1411 $verb = construct_verb($item);
1412 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1413 $actobj = construct_activity_object($item);
1416 $actarg = construct_activity_target($item);
1420 $mentioned = get_mentions($item);
1424 call_hooks('atom_entry', $o);
1426 $o .= '</entry>' . "\r\n";