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 AND `item`.`parent` != 0
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 // make sure nobody is trying to sneak some html tags by us
392 $res['body'] = notags(base64url_decode($res['body']));
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((strpos($res['body'],'<') !== false) || (strpos($res['body'],'>') !== false)) {
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 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
431 if($allow && $allow[0]['data'] == 1)
432 $res['last-child'] = 1;
434 $res['last-child'] = 0;
436 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
437 if($private && $private[0]['data'] == 1)
443 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
445 $res['location'] = unxmlify($rawlocation[0]['data']);
448 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
450 $res['created'] = unxmlify($rawcreated[0]['data']);
453 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
455 $res['edited'] = unxmlify($rawedited[0]['data']);
457 if((x($res,'edited')) && (! (x($res,'created'))))
458 $res['created'] = $res['edited'];
460 if(! $res['created'])
461 $res['created'] = $item->get_date('c');
464 $res['edited'] = $item->get_date('c');
467 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
468 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
469 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
470 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
471 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
472 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
473 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
474 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
475 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
477 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
478 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
480 foreach($base as $link) {
481 if(! $res['owner-avatar']) {
482 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
483 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
488 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
490 $res['coord'] = unxmlify($rawgeo[0]['data']);
493 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
495 // select between supported verbs
498 $res['verb'] = unxmlify($rawverb[0]['data']);
501 // translate OStatus unfollow to activity streams if it happened to get selected
503 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
504 $res['verb'] = ACTIVITY_UNFOLLOW;
508 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
511 $res['object'] = '<object>' . "\n";
512 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
513 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
514 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
516 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
517 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
518 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
519 $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
520 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
521 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
522 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
523 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
525 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
526 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
527 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
528 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
530 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
531 '[youtube]$1[/youtube]', $body);
533 $config = HTMLPurifier_Config::createDefault();
534 $config->set('Cache.DefinitionImpl', null);
536 $purifier = new HTMLPurifier($config);
537 $body = $purifier->purify($body);
538 $body = html2bbcode($body);
541 $res['object'] .= '<content>' . $body . '</content>' . "\n";
544 $res['object'] .= '</object>' . "\n";
547 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
550 $res['target'] = '<target>' . "\n";
551 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
552 $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
554 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
555 $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
557 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
558 $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
559 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
560 $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
561 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
562 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
564 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
565 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
566 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
567 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
569 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
570 '[youtube]$1[/youtube]', $body);
572 $config = HTMLPurifier_Config::createDefault();
573 $config->set('Cache.DefinitionImpl', null);
575 $purifier = new HTMLPurifier($config);
576 $body = $purifier->purify($body);
577 $body = html2bbcode($body);
580 $res['target'] .= '<content>' . $body . '</content>' . "\n";
583 $res['target'] .= '</target>' . "\n";
586 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
588 call_hooks('parse_atom', $arr);
593 function encode_rel_links($links) {
595 if(! ((is_array($links)) && (count($links))))
597 foreach($links as $link) {
599 if($link['attribs']['']['rel'])
600 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
601 if($link['attribs']['']['type'])
602 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
603 if($link['attribs']['']['href'])
604 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
605 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
606 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
607 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
608 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
614 function item_store($arr,$force_parent = false) {
617 $arr['gravity'] = intval($arr['gravity']);
618 elseif($arr['parent-uri'] == $arr['uri'])
620 elseif(activity_match($arr['verb'],ACTIVITY_POST))
623 $arr['gravity'] = 6; // extensible catchall
626 $arr['type'] = 'remote';
628 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
630 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
631 $arr['body'] = strip_tags($arr['body']);
634 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
635 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
636 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
637 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
638 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
639 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
640 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
641 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
642 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
643 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
644 $arr['changed'] = datetime_convert();
645 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
646 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
647 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
648 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
649 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
651 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
652 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
653 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
654 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
655 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
656 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
657 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
658 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
659 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
660 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
661 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
662 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
664 if($arr['parent-uri'] === $arr['uri']) {
666 $allow_cid = $arr['allow_cid'];
667 $allow_gid = $arr['allow_gid'];
668 $deny_cid = $arr['deny_cid'];
669 $deny_gid = $arr['deny_gid'];
673 // find the parent and snarf the item id and ACL's
675 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
676 dbesc($arr['parent-uri']),
682 // is the new message multi-level threaded?
683 // even though we don't support it now, preserve the info
684 // and re-attach to the conversation parent.
686 if($r[0]['uri'] != $r[0]['parent-uri']) {
687 $arr['thr-parent'] = $arr['parent-uri'];
688 $arr['parent-uri'] = $r[0]['parent-uri'];
691 $parent_id = $r[0]['id'];
692 $parent_deleted = $r[0]['deleted'];
693 $allow_cid = $r[0]['allow_cid'];
694 $allow_gid = $r[0]['allow_gid'];
695 $deny_cid = $r[0]['deny_cid'];
696 $deny_gid = $r[0]['deny_gid'];
700 // Allow one to see reply tweets from status.net even when
701 // we don't have or can't see the original post.
704 logger('item_store: $force_parent=true, reply converted to top-level post.');
706 $arr['thr-parent'] = $arr['parent-uri'];
707 $arr['parent-uri'] = $arr['uri'];
710 logger('item_store: item parent was not found - ignoring item');
716 call_hooks('post_remote',$arr);
720 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
722 $r = dbq("INSERT INTO `item` (`"
723 . implode("`, `", array_keys($arr))
725 . implode("', '", array_values($arr))
728 // find the item we just created
730 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
731 $arr['uri'], // already dbesc'd
735 $current_post = $r[0]['id'];
736 logger('item_store: created item ' . $current_post);
739 logger('item_store: could not locate created item');
743 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
744 $parent_id = $current_post;
746 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
749 $private = $arr['private'];
751 // Set parent id - and also make sure to inherit the parent's ACL's.
753 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
754 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
761 intval($parent_deleted),
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['issued-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 if(strpos($xml,'<?xml') === false) {
818 logger('dfrn_deliver: no valid XML returned');
819 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
823 $res = simplexml_load_string($xml);
825 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
826 return (($res->status) ? $res->status : 3);
829 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
830 $challenge = hex2bin((string) $res->challenge);
831 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
832 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
837 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
838 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
839 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
842 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
843 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
846 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
848 if(strpos($final_dfrn_id,':') == 1)
849 $final_dfrn_id = substr($final_dfrn_id,2);
851 if($final_dfrn_id != $orig_id) {
852 logger('dfrn_deliver: wrong dfrn_id.');
853 // did not decode properly - cannot trust this site
857 $postvars['dfrn_id'] = $idtosend;
858 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
860 $postvars['dissolve'] = '1';
862 if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
863 $postvars['data'] = $atom;
865 elseif($owner['page-flags'] == PAGE_COMMUNITY) {
866 $postvars['data'] = $atom;
869 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
872 if($rino && $rino_allowed && (! $dissolve)) {
873 $key = substr(random_string(),0,16);
874 $data = bin2hex(aes_encrypt($postvars['data'],$key));
875 $postvars['data'] = $data;
876 logger('rino: sent key = ' . $key);
879 if($dfrn_version >= 2.1) {
880 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
881 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
884 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
888 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
889 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
892 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
896 logger('md5 rawkey ' . md5($postvars['key']));
898 $postvars['key'] = bin2hex($postvars['key']);
901 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
903 $xml = post_url($contact['notify'],$postvars);
905 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
907 $curl_stat = $a->get_curl_code();
908 if((! $curl_stat) || (! strlen($xml)))
909 return(-1); // timed out
912 if(strpos($xml,'<?xml') === false) {
913 logger('dfrn_deliver: phase 2: no valid XML returned');
914 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
918 $res = simplexml_load_string($xml);
927 * consume_feed - process atom feed and update anything/everything we might need to update
929 * $xml = the (atom) feed to consume - no RSS spoken here, it might partially work since simplepie
930 * handles both, but we don't claim it will work well, and are reasonably certain it won't.
931 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
932 * It is this person's stuff that is going to be updated.
933 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
934 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
935 * have a contact record.
936 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
937 * might not) try and subscribe to it.
941 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
943 require_once('simplepie/simplepie.inc');
945 $feed = new SimplePie();
946 $feed->set_raw_data($xml);
948 $feed->enable_order_by_date(true);
950 $feed->enable_order_by_date(false);
954 logger('consume_feed: Error parsing XML: ' . $feed->error());
957 // Check at the feed level for updated contact name and/or photo
961 $photo_timestamp = '';
965 $hubs = $feed->get_links('hub');
968 $hub = implode(',', $hubs);
970 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
972 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
973 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
974 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
975 $new_name = $elems['name'][0]['data'];
977 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
978 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
979 $photo_url = $elems['link'][0]['attribs']['']['href'];
982 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
983 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
987 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
988 logger('consume_feed: Updating photo for ' . $contact['name']);
989 require_once("Photo.php");
990 $photo_failure = false;
993 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
994 intval($contact['id']),
995 intval($contact['uid'])
998 $resource_id = $r[0]['resource-id'];
1002 $resource_id = photo_new_resource();
1005 $img_str = fetch_url($photo_url,true);
1006 $img = new Photo($img_str);
1007 if($img->is_valid()) {
1009 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1010 dbesc($resource_id),
1011 intval($contact['id']),
1012 intval($contact['uid'])
1016 $img->scaleImageSquare(175);
1018 $hash = $resource_id;
1019 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
1021 $img->scaleImage(80);
1022 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
1024 $img->scaleImage(48);
1025 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 6);
1029 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1030 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1031 dbesc(datetime_convert()),
1032 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
1033 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
1034 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
1035 intval($contact['uid']),
1036 intval($contact['id'])
1041 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1042 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1043 dbesc(notags(trim($new_name))),
1044 dbesc(datetime_convert()),
1045 intval($contact['uid']),
1046 intval($contact['id'])
1050 if(strlen($birthday)) {
1051 if(substr($birthday,0,4) != $contact['bdyear']) {
1052 logger('consume_feed: updating birthday: ' . $birthday);
1056 * Add new birthday event for this person
1058 * $bdtext is just a readable placeholder in case the event is shared
1059 * with others. We will replace it during presentation to our $importer
1060 * to contain a sparkle link and perhaps a photo.
1064 $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1067 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1068 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1069 intval($contact['uid']),
1070 intval($contact['id']),
1071 dbesc(datetime_convert()),
1072 dbesc(datetime_convert()),
1073 dbesc(datetime_convert('UTC','UTC', $birthday)),
1074 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1082 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1083 dbesc(substr($birthday,0,4)),
1084 intval($contact['uid']),
1085 intval($contact['id'])
1088 // This function is called twice without reloading the contact
1089 // Make sure we only create one event. This is why &$contact
1090 // is a reference var in this function
1092 $contact['bdyear'] = substr($birthday,0,4);
1097 // Now process the feed
1098 if($feed->get_item_quantity()) {
1100 // in inverse date order
1102 $items = array_reverse($feed->get_items());
1104 $items = $feed->get_items();
1106 foreach($items as $item) {
1110 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
1111 if(isset($rawdelete[0]['attribs']['']['ref'])) {
1112 $uri = $rawthread[0]['attribs']['']['ref'];
1114 if(isset($rawdelete[0]['attribs']['']['when'])) {
1115 $when = $rawthread[0]['attribs']['']['when'];
1116 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1119 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1121 if($deleted && is_array($contact)) {
1122 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1124 intval($importer['uid']),
1125 intval($contact['id'])
1129 if($item['uri'] == $item['parent-uri']) {
1130 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1131 `body` = '', `title` = ''
1132 WHERE `parent-uri` = '%s' AND `uid` = %d",
1134 dbesc(datetime_convert()),
1135 dbesc($item['uri']),
1136 intval($importer['uid'])
1140 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1141 `body` = '', `title` = ''
1142 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1144 dbesc(datetime_convert()),
1146 intval($importer['uid'])
1148 if($item['last-child']) {
1149 // ensure that last-child is set in case the comment that had it just got wiped.
1150 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1151 dbesc(datetime_convert()),
1152 dbesc($item['parent-uri']),
1153 intval($item['uid'])
1155 // who is the last child now?
1156 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
1157 ORDER BY `created` DESC LIMIT 1",
1158 dbesc($item['parent-uri']),
1159 intval($importer['uid'])
1162 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1174 $item_id = $item->get_id();
1175 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1176 if(isset($rawthread[0]['attribs']['']['ref'])) {
1178 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1182 if(($is_reply) && is_array($contact)) {
1184 // Have we seen it? If not, import it.
1186 $item_id = $item->get_id();
1188 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1190 intval($importer['uid'])
1192 // FIXME update content if 'updated' changes
1194 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1195 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1196 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1197 dbesc(datetime_convert()),
1199 intval($importer['uid'])
1201 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1202 intval($allow[0]['data']),
1203 dbesc(datetime_convert()),
1205 intval($importer['uid'])
1210 $datarray = get_atom_elements($feed,$item);
1211 $force_parent = false;
1212 if($contact['network'] === 'stat') {
1213 $force_parent = true;
1214 if(strlen($datarray['title']))
1215 unset($datarray['title']);
1216 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1217 dbesc(datetime_convert()),
1219 intval($importer['uid'])
1221 $datarray['last-child'] = 1;
1224 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1225 // one way feed - no remote comment ability
1226 $datarray['last-child'] = 0;
1228 $datarray['parent-uri'] = $parent_uri;
1229 $datarray['uid'] = $importer['uid'];
1230 $datarray['contact-id'] = $contact['id'];
1231 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1232 $datarray['type'] = 'activity';
1233 $datarray['gravity'] = GRAVITY_LIKE;
1236 $r = item_store($datarray,$force_parent);
1241 // Head post of a conversation. Have we seen it? If not, import it.
1243 $item_id = $item->get_id();
1244 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1246 intval($importer['uid'])
1249 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1250 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1251 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1252 intval($allow[0]['data']),
1253 dbesc(datetime_convert()),
1255 intval($importer['uid'])
1260 $datarray = get_atom_elements($feed,$item);
1262 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1263 logger('consume-feed: New follower');
1264 new_follower($importer,$contact,$datarray,$item);
1267 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
1268 lose_follower($importer,$contact,$datarray,$item);
1271 if(! is_array($contact))
1274 if($contact['network'] === 'stat') {
1275 if(strlen($datarray['title']))
1276 unset($datarray['title']);
1277 $datarray['last-child'] = 1;
1280 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1281 // one way feed - no remote comment ability
1282 $datarray['last-child'] = 0;
1285 $datarray['parent-uri'] = $item_id;
1286 $datarray['uid'] = $importer['uid'];
1287 $datarray['contact-id'] = $contact['id'];
1288 $r = item_store($datarray);
1296 function new_follower($importer,$contact,$datarray,$item) {
1297 $url = notags(trim($datarray['author-link']));
1298 $name = notags(trim($datarray['author-name']));
1299 $photo = notags(trim($datarray['author-avatar']));
1301 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
1302 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
1303 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
1305 if(is_array($contact)) {
1306 if($contact['network'] == 'stat' && $contact['rel'] == REL_FAN) {
1307 $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
1309 intval($contact['id']),
1310 intval($importer['uid'])
1314 // send email notification to owner?
1318 // create contact record - set to readonly
1320 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
1321 `blocked`, `readonly`, `pending` )
1322 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
1323 intval($importer['uid']),
1324 dbesc(datetime_convert()),
1332 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
1333 intval($importer['uid']),
1338 $contact_record = $r[0];
1340 // create notification
1341 $hash = random_string();
1343 if(is_array($contact_record)) {
1344 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
1345 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
1346 intval($importer['uid']),
1347 intval($contact_record['id']),
1349 dbesc(datetime_convert())
1352 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1353 intval($importer['uid'])
1357 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
1358 $email_tpl = load_view_file('view/follow_notify_eml.tpl');
1359 $email = replace_macros($email_tpl, array(
1360 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
1362 '$myname' => $r[0]['username'],
1363 '$siteurl' => $a->get_baseurl(),
1364 '$sitename' => $a->config['sitename']
1366 $res = mail($r[0]['email'],
1367 t("You have a new follower at ") . $a->config['sitename'],
1369 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
1376 function lose_follower($importer,$contact,$datarray,$item) {
1378 if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
1379 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1381 intval($contact['id'])
1385 contact_remove($contact['id']);
1390 function subscribe_to_hub($url,$importer,$contact) {
1392 if(is_array($importer)) {
1393 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1394 intval($importer['uid'])
1400 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1402 // Use a single verify token, even if multiple hubs
1404 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1406 $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1408 logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
1410 if(! strlen($contact['hub-verify'])) {
1411 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
1412 dbesc($verify_token),
1413 intval($contact['id'])
1417 post_url($url,$params);
1423 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1427 $name = xmlify($name);
1428 $uri = xmlify($uri);
1431 $photo = xmlify($photo);
1435 $o .= "<name>$name</name>\r\n";
1436 $o .= "<uri>$uri</uri>\r\n";
1437 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1438 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1440 call_hooks('atom_author', $o);
1442 $o .= "</$tag>\r\n";
1446 function atom_entry($item,$type,$author,$owner,$comment = false) {
1448 if($item['deleted'])
1449 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1453 $o = "\r\n\r\n<entry>\r\n";
1455 if(is_array($author))
1456 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1458 $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
1459 if(strlen($item['owner-name']))
1460 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1462 if($item['parent'] != $item['id'])
1463 $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";
1465 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1466 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1467 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1468 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1469 $o .= '<dfrn:env>' . base64url_encode($item['body'], true) . '</dfrn:env>' . "\r\n";
1470 $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
1471 $o .= '<link rel="alternate" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1473 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1475 if($item['location']) {
1476 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1477 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
1481 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1483 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
1484 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
1486 $verb = construct_verb($item);
1487 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1488 $actobj = construct_activity_object($item);
1491 $actarg = construct_activity_target($item);
1495 $mentioned = get_mentions($item);
1499 call_hooks('atom_entry', $o);
1501 $o .= '</entry>' . "\r\n";