3 require_once('bbcode.php');
4 require_once('oembed.php');
5 require_once('include/salmon.php');
7 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
10 // default permissions - anonymous user
19 if(strlen($owner_nick) && ! intval($owner_nick)) {
20 $r = q("SELECT `uid`, `nickname`, `timezone` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
24 $owner_id = $r[0]['uid'];
25 $owner_nick = $r[0]['nickname'];
26 $owner_tz = $r[0]['timezone'];
30 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
35 $owner['nickname'] = $owner_nick;
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.
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.
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.
62 $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
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'));
74 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
75 $birthday = datetime_convert($owner_tz,'UTC',$bd,ATOM_TIME);
79 if($dfrn_id && $dfrn_id != '*') {
84 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
88 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
89 $my_id = '1:' . $dfrn_id;
92 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
93 $my_id = '0:' . $dfrn_id;
100 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
108 $groups = init_groups_visitor($contact['id']);
111 for($x = 0; $x < count($groups); $x ++)
112 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
113 $gs = implode('|', $groups);
116 $gs = '<<>>' ; // Impossible to match
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')
124 intval($contact['id']),
125 intval($contact['id']),
131 if($dfrn_id === '' || $dfrn_id === '*')
136 if(! strlen($last_update))
137 $last_update = 'now -30 days';
139 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
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' )
151 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
158 // Will check further below if this actually returned results.
159 // We will provide an empty feed in any case.
163 $feed_template = load_view_file('view/atom_feed.tpl');
167 $hub = get_config('system','huburl');
171 $hubs = explode(',', $hub);
173 foreach($hubs as $h) {
177 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
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" ;
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)) ,
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>' : '')
204 call_hooks('atom_feed', $atom);
206 if(! count($items)) {
208 call_hooks('atom_feed_end', $atom);
210 $atom .= '</feed>' . "\r\n";
214 foreach($items as $item) {
216 // public feeds get html, our own nodes use bbcode
218 if($dfrn_id === '*') {
225 $atom .= atom_entry($item,$type,null,$owner,true);
228 call_hooks('atom_feed_end', $atom);
230 $atom .= '</feed>' . "\r\n";
236 function construct_verb($item) {
238 return $item['verb'];
239 return ACTIVITY_POST;
242 function construct_activity_object($item) {
244 if($item['object']) {
245 $o = '<as:object>' . "\r\n";
246 $r = @simplexml_load_string($item['object']);
248 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
250 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
252 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
254 if(substr($r->link,0,1) === '<')
257 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
260 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
261 $o .= '</as:object>' . "\r\n";
268 function construct_activity_target($item) {
270 if($item['target']) {
271 $o = '<as:target>' . "\r\n";
272 $r = @simplexml_load_string($item['target']);
274 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
276 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
278 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
280 if(substr($r->link,0,1) === '<')
283 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
286 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
287 $o .= '</as:target>' . "\r\n";
297 function get_atom_elements($feed,$item) {
299 require_once('library/HTMLPurifier.auto.php');
300 require_once('include/html2bbcode.php');
302 $best_photo = array();
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());
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
317 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
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']);
329 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
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']);
345 // No photo/profile-link on the item - look at the feed level
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']);
361 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
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'];
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']);
381 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
384 $have_real_body = false;
386 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
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;
395 $maxlen = get_max_import_size();
396 if($maxlen && (strlen($res['body']) > $maxlen))
397 $res['body'] = substr($res['body'],0, $maxlen);
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.
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
410 if((! $have_real_body) || (strpos($res['body'],'<')) || (strpos($res['body'],'>'))) {
412 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
413 '[youtube]$1[/youtube]', $res['body']);
415 $res['body'] = oembed_html2bbcode($res['body']);
417 $config = HTMLPurifier_Config::createDefault();
418 $config->set('Cache.DefinitionImpl', null);
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');
424 $purifier = new HTMLPurifier($config);
425 $res['body'] = $purifier->purify($res['body']);
427 $res['body'] = html2bbcode($res['body']);
430 $res['body'] = escape_tags($res['body']);
433 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
434 if($allow && $allow[0]['data'] == 1)
435 $res['last-child'] = 1;
437 $res['last-child'] = 0;
439 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
440 if($private && $private[0]['data'] == 1)
446 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
448 $res['location'] = unxmlify($rawlocation[0]['data']);
451 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
453 $res['created'] = unxmlify($rawcreated[0]['data']);
456 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
458 $res['edited'] = unxmlify($rawcreated[0]['data']);
461 if(! $res['created'])
462 $res['created'] = $item->get_date();
465 $res['edited'] = $item->get_date();
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']);
478 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
479 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
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']);
489 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
491 $res['coord'] = unxmlify($rawgeo[0]['data']);
494 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
496 // select between supported verbs
499 $res['verb'] = unxmlify($rawverb[0]['data']);
502 // translate OStatus unfollow to activity streams if it happened to get selected
504 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
505 $res['verb'] = ACTIVITY_UNFOLLOW;
509 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
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";
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'];
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,'>'))) {
531 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
532 '[youtube]$1[/youtube]', $body);
534 $config = HTMLPurifier_Config::createDefault();
535 $config->set('Cache.DefinitionImpl', null);
537 $purifier = new HTMLPurifier($config);
538 $body = $purifier->purify($body);
539 $body = html2bbcode($body);
542 $body = escape_tags($body);
544 $res['object'] .= '<content>' . $body . '</content>' . "\n";
547 $res['object'] .= '</object>' . "\n";
550 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
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";
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";
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'];
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,'>'))) {
572 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
573 '[youtube]$1[/youtube]', $body);
575 $config = HTMLPurifier_Config::createDefault();
576 $config->set('Cache.DefinitionImpl', null);
578 $purifier = new HTMLPurifier($config);
579 $body = $purifier->purify($body);
580 $body = html2bbcode($body);
583 $body = escape_tags($body);
585 $res['target'] .= '<content>' . $body . '</content>' . "\n";
588 $res['target'] .= '</target>' . "\n";
591 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
593 call_hooks('parse_atom', $arr);
598 function encode_rel_links($links) {
600 if(! ((is_array($links)) && (count($links))))
602 foreach($links as $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'] . '" ';
619 function item_store($arr) {
622 $arr['gravity'] = intval($arr['gravity']);
623 elseif($arr['parent-uri'] == $arr['uri'])
625 elseif(activity_match($arr['verb'],ACTIVITY_POST))
628 $arr['gravity'] = 6; // extensible catchall
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 );
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'])) : '');
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.
668 if(x($arr,'realbody'))
669 unset($arr['realbody']);
671 $arr['body'] = str_replace(
672 array('&amp;', '&gt;', '&lt;', '&quot;'),
673 array('&' , '>' , '<', '"'),
678 if($arr['parent-uri'] === $arr['uri']) {
680 $allow_cid = $arr['allow_cid'];
681 $allow_gid = $arr['allow_gid'];
682 $deny_cid = $arr['deny_cid'];
683 $deny_gid = $arr['deny_gid'];
687 // find the parent and snarf the item id and ACL's
689 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
690 dbesc($arr['parent-uri']),
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.
700 if($r[0]['uri'] != $r[0]['parent-uri']) {
701 $arr['thr-parent'] = $arr['parent-uri'];
702 $arr['parent-uri'] = $r[0]['parent-uri'];
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'];
712 logger('item_store: item parent was not found - ignoring item');
717 call_hooks('post_remote',$arr);
721 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
723 $r = dbq("INSERT INTO `item` (`"
724 . implode("`, `", array_keys($arr))
726 . implode("', '", array_values($arr))
729 // find the item we just created
731 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
732 $arr['uri'], // already dbesc'd
736 $current_post = $r[0]['id'];
737 logger('item_store: created item ' . $current_post);
740 logger('item_store: could not locate created item');
744 if($arr['parent-uri'] === $arr['uri'])
745 $parent_id = $current_post;
747 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
750 $private = $arr['private'];
752 // Set parent id - and also make sure to inherit the parent's ACL's.
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",
762 intval($current_post)
765 return $current_post;
768 function get_item_contact($item,$contacts) {
769 if(! count($contacts) || (! is_array($item)))
771 foreach($contacts as $contact) {
772 if($contact['id'] == $item['contact-id']) {
781 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
785 if((! strlen($contact['dfrn-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
788 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
790 if($contact['duplex'] && $contact['dfrn-id'])
791 $idtosend = '0:' . $orig_id;
792 if($contact['duplex'] && $contact['issued-id'])
793 $idtosend = '1:' . $orig_id;
795 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
797 $rino_enable = get_config('system','rino_encrypt');
802 $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
804 logger('dfrn_deliver: ' . $url);
806 $xml = fetch_url($url);
808 $curl_stat = $a->get_curl_code();
810 return(-1); // timed out
812 logger('dfrn_deliver: ' . $xml);
817 $res = simplexml_load_string($xml);
819 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
820 return (($res->status) ? $res->status : 3);
823 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
824 $challenge = hex2bin((string) $res->challenge);
825 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
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']);
835 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
836 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
839 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
841 if(strpos($final_dfrn_id,':') == 1)
842 $final_dfrn_id = substr($final_dfrn_id,2);
844 if($final_dfrn_id != $orig_id) {
845 logger('dfrn_deliver: wrong dfrn_id.');
846 // did not decode properly - cannot trust this site
850 $postvars['dfrn_id'] = $idtosend;
851 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
853 $postvars['dissolve'] = '1';
855 if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
856 $postvars['data'] = $atom;
858 elseif($owner['page-flags'] == PAGE_COMMUNITY) {
859 $postvars['data'] = $atom;
862 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
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);
871 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
872 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
875 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
878 logger('md5 rawkey ' . md5($postvars['key']));
880 $postvars['key'] = bin2hex($postvars['key']);
883 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
885 $xml = post_url($contact['notify'],$postvars);
887 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
889 $curl_stat = $a->get_curl_code();
890 if((! $curl_stat) || (! strlen($xml)))
891 return(-1); // timed out
893 $res = simplexml_load_string($xml);
902 * consume_feed - process atom feed and update anything/everything we might need to update
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.
916 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
918 require_once('simplepie/simplepie.inc');
920 $feed = new SimplePie();
921 $feed->set_raw_data($xml);
923 $feed->enable_order_by_date(true);
925 $feed->enable_order_by_date(false);
929 logger('consume_feed: Error parsing XML: ' . $feed->error());
932 // Check at the feed level for updated contact name and/or photo
936 $photo_timestamp = '';
940 $hubs = $feed->get_links('hub');
943 $hub = implode(',', $hubs);
945 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
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'];
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'];
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']);
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;
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'])
973 $resource_id = $r[0]['resource-id'];
977 $resource_id = photo_new_resource();
980 $img_str = fetch_url($photo_url,true);
981 $img = new Photo($img_str);
982 if($img->is_valid()) {
984 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
986 intval($contact['id']),
987 intval($contact['uid'])
991 $img->scaleImageSquare(175);
993 $hash = $resource_id;
994 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
996 $img->scaleImage(80);
997 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
999 $img->scaleImage(48);
1000 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 6);
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'])
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'])
1025 if(strlen($birthday)) {
1026 if(substr($birthday,0,4) != $contact['bdyear']) {
1027 logger('consume_feed: updating birthday: ' . $birthday);
1031 * Add new birthday event for this person
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.
1039 $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
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 ')),
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'])
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
1067 $contact['bdyear'] = substr($birthday,0,4);
1072 // Now process the feed
1073 if($feed->get_item_quantity()) {
1075 // in inverse date order
1077 $items = array_reverse($feed->get_items());
1079 $items = $feed->get_items();
1081 foreach($items as $item) {
1085 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
1086 if(isset($rawdelete[0]['attribs']['']['ref'])) {
1087 $uri = $rawthread[0]['attribs']['']['ref'];
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');
1094 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1096 if($deleted && is_array($contact)) {
1097 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1099 intval($importer['uid']),
1100 intval($contact['id'])
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",
1109 dbesc(datetime_convert()),
1110 dbesc($item['uri']),
1111 intval($importer['uid'])
1115 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1116 `body` = '', `title` = ''
1117 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1119 dbesc(datetime_convert()),
1121 intval($importer['uid'])
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'])
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'])
1137 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1149 $item_id = $item->get_id();
1150 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1151 if(isset($rawthread[0]['attribs']['']['ref'])) {
1153 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1157 if(($is_reply) && is_array($contact)) {
1159 // Have we seen it? If not, import it.
1161 $item_id = $item->get_id();
1163 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1165 intval($importer['uid'])
1167 // FIXME update content if 'updated' changes
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()),
1174 intval($importer['uid'])
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()),
1180 intval($importer['uid'])
1185 $datarray = get_atom_elements($feed,$item);
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()),
1193 intval($importer['uid'])
1195 $datarray['last-child'] = 1;
1198 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1199 // one way feed - no remote comment ability
1200 $datarray['last-child'] = 0;
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;
1210 $r = item_store($datarray);
1215 // Head post of a conversation. Have we seen it? If not, import it.
1217 $item_id = $item->get_id();
1218 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1220 intval($importer['uid'])
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()),
1229 intval($importer['uid'])
1234 $datarray = get_atom_elements($feed,$item);
1236 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1237 logger('consume-feed: New follower');
1238 new_follower($importer,$contact,$datarray,$item);
1241 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
1242 lose_follower($importer,$contact,$datarray,$item);
1245 if(! is_array($contact))
1248 if($contact['network'] === 'stat') {
1249 if(strlen($datarray['title']))
1250 unset($datarray['title']);
1251 $datarray['last-child'] = 1;
1254 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1255 // one way feed - no remote comment ability
1256 $datarray['last-child'] = 0;
1259 $datarray['parent-uri'] = $item_id;
1260 $datarray['uid'] = $importer['uid'];
1261 $datarray['contact-id'] = $contact['id'];
1262 $r = item_store($datarray);
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']));
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'];
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",
1283 intval($contact['id']),
1284 intval($importer['uid'])
1288 // send email notification to owner?
1292 // create contact record - set to readonly
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()),
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']),
1312 $contact_record = $r[0];
1314 // create notification
1315 $hash = random_string();
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']),
1323 dbesc(datetime_convert())
1326 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1327 intval($importer['uid'])
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]')),
1336 '$myname' => $r[0]['username'],
1337 '$siteurl' => $a->get_baseurl(),
1338 '$sitename' => $a->config['sitename']
1340 $res = mail($r[0]['email'],
1341 t("You have a new follower at ") . $a->config['sitename'],
1343 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
1350 function lose_follower($importer,$contact,$datarray,$item) {
1352 if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
1353 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1355 intval($contact['id'])
1359 contact_remove($contact['id']);
1364 function subscribe_to_hub($url,$importer,$contact) {
1366 if(is_array($importer)) {
1367 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1368 intval($importer['uid'])
1374 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1376 // Use a single verify token, even if multiple hubs
1378 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1380 $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1382 logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
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'])
1391 post_url($url,$params);
1397 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1401 $name = xmlify($name);
1402 $uri = xmlify($uri);
1405 $photo = xmlify($photo);
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";
1414 call_hooks('atom_author', $o);
1416 $o .= "</$tag>\r\n";
1420 function atom_entry($item,$type,$author,$owner,$comment = false) {
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";
1427 $o = "\r\n\r\n<entry>\r\n";
1429 if(is_array($author))
1430 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
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']);
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";
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";
1447 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
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";
1455 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
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";
1460 $verb = construct_verb($item);
1461 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1462 $actobj = construct_activity_object($item);
1465 $actarg = construct_activity_target($item);
1469 $mentioned = get_mentions($item);
1473 call_hooks('atom_entry', $o);
1475 $o .= '</entry>' . "\r\n";