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
12 if(! strlen($owner_nick))
15 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
17 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`
18 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
19 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
27 $owner_id = $owner['user_uid'];
28 $owner_nick = $owner['nickname'];
30 $birthday = feed_birthday($owner_id,$owner['timezone']);
32 if(strlen($dfrn_id)) {
37 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
41 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
42 $my_id = '1:' . $dfrn_id;
45 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46 $my_id = '0:' . $dfrn_id;
53 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
61 $groups = init_groups_visitor($contact['id']);
64 for($x = 0; $x < count($groups); $x ++)
65 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
66 $gs = implode('|', $groups);
69 $gs = '<<>>' ; // Impossible to match
71 $sql_extra = sprintf("
72 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
73 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
74 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
75 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
77 intval($contact['id']),
78 intval($contact['id']),
84 if($dfrn_id === '' || $dfrn_id === '*')
89 if(! strlen($last_update))
90 $last_update = 'now -30 days';
92 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
94 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
95 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
96 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
97 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
98 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
99 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
100 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`parent` != 0
101 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
102 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
104 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
111 // Will check further below if this actually returned results.
112 // We will provide an empty feed if that is the case.
116 $feed_template = load_view_file('view/atom_feed.tpl');
120 $hubxml = feed_hublinks();
122 $salmon = feed_salmonlinks($owner_nick);
124 $atom .= replace_macros($feed_template, array(
125 '$version' => xmlify(FRIENDIKA_VERSION),
126 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
127 '$feed_title' => xmlify($owner['name']),
128 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
130 '$salmon' => $salmon,
131 '$name' => xmlify($owner['name']),
132 '$profile_page' => xmlify($owner['url']),
133 '$photo' => xmlify($owner['photo']),
134 '$thumb' => xmlify($owner['thumb']),
135 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
136 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
137 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
138 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : '')
141 call_hooks('atom_feed', $atom);
143 if(! count($items)) {
145 call_hooks('atom_feed_end', $atom);
147 $atom .= '</feed>' . "\r\n";
151 foreach($items as $item) {
153 // public feeds get html, our own nodes use bbcode
155 if($dfrn_id === '') {
162 $atom .= atom_entry($item,$type,null,$owner,true);
165 call_hooks('atom_feed_end', $atom);
167 $atom .= '</feed>' . "\r\n";
173 function construct_verb($item) {
175 return $item['verb'];
176 return ACTIVITY_POST;
179 function construct_activity_object($item) {
181 if($item['object']) {
182 $o = '<as:object>' . "\r\n";
183 $r = parse_xml_string($item['object']);
185 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
187 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
189 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
191 if(substr($r->link,0,1) === '<')
194 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
197 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
198 $o .= '</as:object>' . "\r\n";
205 function construct_activity_target($item) {
207 if($item['target']) {
208 $o = '<as:target>' . "\r\n";
209 $r = parse_xml_string($item['target']);
211 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
213 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
215 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
217 if(substr($r->link,0,1) === '<')
220 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
223 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
224 $o .= '</as:target>' . "\r\n";
234 function get_atom_elements($feed,$item) {
236 require_once('library/HTMLPurifier.auto.php');
237 require_once('include/html2bbcode.php');
239 $best_photo = array();
243 $author = $item->get_author();
245 $res['author-name'] = unxmlify($author->get_name());
246 $res['author-link'] = unxmlify($author->get_link());
249 $res['author-name'] = unxmlify($feed->get_title());
250 $res['author-link'] = unxmlify($feed->get_permalink());
252 $res['uri'] = unxmlify($item->get_id());
253 $res['title'] = unxmlify($item->get_title());
254 $res['body'] = unxmlify($item->get_content());
255 $res['plink'] = unxmlify($item->get_link(0));
257 // look for a photo. We should check media size and find the best one,
258 // but for now let's just find any author photo
260 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
262 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
263 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
264 foreach($base as $link) {
265 if(! $res['author-avatar']) {
266 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
267 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
272 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
274 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
275 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
276 if($base && count($base)) {
277 foreach($base as $link) {
278 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
279 $res['author-link'] = unxmlify($link['attribs']['']['href']);
280 if(! $res['author-avatar']) {
281 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
282 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
288 // No photo/profile-link on the item - look at the feed level
290 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
291 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
292 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
293 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
294 foreach($base as $link) {
295 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
296 $res['author-link'] = unxmlify($link['attribs']['']['href']);
297 if(! $res['author-avatar']) {
298 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
299 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
304 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
306 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
307 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
309 if($base && count($base)) {
310 foreach($base as $link) {
311 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
312 $res['author-link'] = unxmlify($link['attribs']['']['href']);
313 if(! (x($res,'author-avatar'))) {
314 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
315 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
324 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
327 $have_real_body = false;
329 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
331 $have_real_body = true;
332 $res['body'] = $rawenv[0]['data'];
333 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
334 // make sure nobody is trying to sneak some html tags by us
335 $res['body'] = notags(base64url_decode($res['body']));
338 $maxlen = get_max_import_size();
339 if($maxlen && (strlen($res['body']) > $maxlen))
340 $res['body'] = substr($res['body'],0, $maxlen);
342 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
343 // the content type. Our own network only emits text normally, though it might have been converted to
344 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
345 // have to assume it is all html and needs to be purified.
347 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
348 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
349 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
352 if((strpos($res['body'],'<') !== false) || (strpos($res['body'],'>') !== false)) {
354 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
355 '[youtube]$1[/youtube]', $res['body']);
357 $res['body'] = oembed_html2bbcode($res['body']);
359 $config = HTMLPurifier_Config::createDefault();
360 $config->set('Cache.DefinitionImpl', null);
362 // we shouldn't need a whitelist, because the bbcode converter
363 // will strip out any unsupported tags.
364 // $config->set('HTML.Allowed', 'p,b,a[href],i');
366 $purifier = new HTMLPurifier($config);
367 $res['body'] = $purifier->purify($res['body']);
369 $res['body'] = html2bbcode($res['body']);
372 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
373 if($allow && $allow[0]['data'] == 1)
374 $res['last-child'] = 1;
376 $res['last-child'] = 0;
378 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
379 if($private && $private[0]['data'] == 1)
385 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
387 $res['location'] = unxmlify($rawlocation[0]['data']);
390 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
392 $res['created'] = unxmlify($rawcreated[0]['data']);
395 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
397 $res['edited'] = unxmlify($rawedited[0]['data']);
399 if((x($res,'edited')) && (! (x($res,'created'))))
400 $res['created'] = $res['edited'];
402 if(! $res['created'])
403 $res['created'] = $item->get_date('c');
406 $res['edited'] = $item->get_date('c');
409 // Disallow time travelling posts
411 $d1 = strtotime($res['created']);
412 $d2 = strtotime($res['edited']);
413 $d3 = strtotime('now');
416 $res['created'] = datetime_convert();
418 $res['edited'] = datetime_convert();
420 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
421 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
422 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
423 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
424 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
425 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
426 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
427 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
428 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
430 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
431 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
433 foreach($base as $link) {
434 if(! $res['owner-avatar']) {
435 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
436 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
441 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
443 $res['coord'] = unxmlify($rawgeo[0]['data']);
446 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
448 // select between supported verbs
451 $res['verb'] = unxmlify($rawverb[0]['data']);
454 // translate OStatus unfollow to activity streams if it happened to get selected
456 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
457 $res['verb'] = ACTIVITY_UNFOLLOW;
461 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
464 $res['object'] = '<object>' . "\n";
465 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
466 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
467 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
469 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
470 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
471 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
472 $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
473 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
474 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
475 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
476 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
478 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
479 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
480 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
481 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
483 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
484 '[youtube]$1[/youtube]', $body);
486 $config = HTMLPurifier_Config::createDefault();
487 $config->set('Cache.DefinitionImpl', null);
489 $purifier = new HTMLPurifier($config);
490 $body = $purifier->purify($body);
491 $body = html2bbcode($body);
494 $res['object'] .= '<content>' . $body . '</content>' . "\n";
497 $res['object'] .= '</object>' . "\n";
500 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
503 $res['target'] = '<target>' . "\n";
504 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
505 $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
507 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
508 $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
510 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
511 $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
512 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
513 $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
514 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
515 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
517 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
518 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
519 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
520 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
522 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
523 '[youtube]$1[/youtube]', $body);
525 $config = HTMLPurifier_Config::createDefault();
526 $config->set('Cache.DefinitionImpl', null);
528 $purifier = new HTMLPurifier($config);
529 $body = $purifier->purify($body);
530 $body = html2bbcode($body);
533 $res['target'] .= '<content>' . $body . '</content>' . "\n";
536 $res['target'] .= '</target>' . "\n";
539 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
541 call_hooks('parse_atom', $arr);
546 function encode_rel_links($links) {
548 if(! ((is_array($links)) && (count($links))))
550 foreach($links as $link) {
552 if($link['attribs']['']['rel'])
553 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
554 if($link['attribs']['']['type'])
555 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
556 if($link['attribs']['']['href'])
557 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
558 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
559 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
560 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
561 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
567 function item_store($arr,$force_parent = false) {
570 $arr['gravity'] = intval($arr['gravity']);
571 elseif($arr['parent-uri'] == $arr['uri'])
573 elseif(activity_match($arr['verb'],ACTIVITY_POST))
576 $arr['gravity'] = 6; // extensible catchall
579 $arr['type'] = 'remote';
581 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
583 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
584 $arr['body'] = strip_tags($arr['body']);
587 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
588 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
589 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
590 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
591 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
592 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
593 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
594 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
595 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
596 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
597 $arr['changed'] = datetime_convert();
598 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
599 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
600 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
601 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
602 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
604 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
605 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
606 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
607 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
608 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
609 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
610 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
611 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
612 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
613 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
614 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
615 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
616 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
618 if($arr['parent-uri'] === $arr['uri']) {
620 $allow_cid = $arr['allow_cid'];
621 $allow_gid = $arr['allow_gid'];
622 $deny_cid = $arr['deny_cid'];
623 $deny_gid = $arr['deny_gid'];
627 // find the parent and snarf the item id and ACL's
628 // and anything else we need to inherit
630 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
631 dbesc($arr['parent-uri']),
637 // is the new message multi-level threaded?
638 // even though we don't support it now, preserve the info
639 // and re-attach to the conversation parent.
641 if($r[0]['uri'] != $r[0]['parent-uri']) {
642 $arr['thr-parent'] = $arr['parent-uri'];
643 $arr['parent-uri'] = $r[0]['parent-uri'];
646 $parent_id = $r[0]['id'];
647 $parent_deleted = $r[0]['deleted'];
648 $allow_cid = $r[0]['allow_cid'];
649 $allow_gid = $r[0]['allow_gid'];
650 $deny_cid = $r[0]['deny_cid'];
651 $deny_gid = $r[0]['deny_gid'];
652 $arr['wall'] = $r[0]['wall'];
656 // Allow one to see reply tweets from status.net even when
657 // we don't have or can't see the original post.
660 logger('item_store: $force_parent=true, reply converted to top-level post.');
662 $arr['thr-parent'] = $arr['parent-uri'];
663 $arr['parent-uri'] = $arr['uri'];
666 logger('item_store: item parent was not found - ignoring item');
672 call_hooks('post_remote',$arr);
676 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
678 $r = dbq("INSERT INTO `item` (`"
679 . implode("`, `", array_keys($arr))
681 . implode("', '", array_values($arr))
684 // find the item we just created
686 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
687 $arr['uri'], // already dbesc'd
691 $current_post = $r[0]['id'];
692 logger('item_store: created item ' . $current_post);
695 logger('item_store: could not locate created item');
699 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
700 $parent_id = $current_post;
702 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
705 $private = $arr['private'];
707 // Set parent id - and also make sure to inherit the parent's ACL's.
709 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
710 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
717 intval($parent_deleted),
718 intval($current_post)
722 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
725 if($arr['last-child']) {
726 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
729 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, $dissolve = false) {
753 if((! strlen($contact['issued-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 if(strpos($xml,'<?xml') === false) {
786 logger('dfrn_deliver: no valid XML returned');
787 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
791 $res = parse_xml_string($xml);
793 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
794 return (($res->status) ? $res->status : 3);
797 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
798 $challenge = hex2bin((string) $res->challenge);
799 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
800 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
805 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
806 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
807 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
810 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
811 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
814 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
816 if(strpos($final_dfrn_id,':') == 1)
817 $final_dfrn_id = substr($final_dfrn_id,2);
819 if($final_dfrn_id != $orig_id) {
820 logger('dfrn_deliver: wrong dfrn_id.');
821 // did not decode properly - cannot trust this site
825 $postvars['dfrn_id'] = $idtosend;
826 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
828 $postvars['dissolve'] = '1';
830 if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
831 $postvars['data'] = $atom;
833 elseif($owner['page-flags'] == PAGE_COMMUNITY) {
834 $postvars['data'] = $atom;
837 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
840 if($rino && $rino_allowed && (! $dissolve)) {
841 $key = substr(random_string(),0,16);
842 $data = bin2hex(aes_encrypt($postvars['data'],$key));
843 $postvars['data'] = $data;
844 logger('rino: sent key = ' . $key);
847 if($dfrn_version >= 2.1) {
848 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
849 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
852 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
856 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
857 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
860 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
864 logger('md5 rawkey ' . md5($postvars['key']));
866 $postvars['key'] = bin2hex($postvars['key']);
869 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
871 $xml = post_url($contact['notify'],$postvars);
873 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
875 $curl_stat = $a->get_curl_code();
876 if((! $curl_stat) || (! strlen($xml)))
877 return(-1); // timed out
880 if(strpos($xml,'<?xml') === false) {
881 logger('dfrn_deliver: phase 2: no valid XML returned');
882 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
886 $res = parse_xml_string($xml);
895 * consume_feed - process atom feed and update anything/everything we might need to update
897 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
899 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
900 * It is this person's stuff that is going to be updated.
901 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
902 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
903 * have a contact record.
904 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
905 * might not) try and subscribe to it.
909 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $secure_feed = false) {
911 require_once('simplepie/simplepie.inc');
913 $feed = new SimplePie();
914 $feed->set_raw_data($xml);
916 $feed->enable_order_by_date(true);
918 $feed->enable_order_by_date(false);
922 logger('consume_feed: Error parsing XML: ' . $feed->error());
924 $permalink = $feed->get_permalink();
926 // Check at the feed level for updated contact name and/or photo
930 $photo_timestamp = '';
934 $hubs = $feed->get_links('hub');
937 $hub = implode(',', $hubs);
939 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
941 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
942 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
943 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
944 $new_name = $elems['name'][0]['data'];
946 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
947 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
948 $photo_url = $elems['link'][0]['attribs']['']['href'];
951 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
952 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
956 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
957 logger('consume_feed: Updating photo for ' . $contact['name']);
958 require_once("Photo.php");
959 $photo_failure = false;
962 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
963 intval($contact['id']),
964 intval($contact['uid'])
967 $resource_id = $r[0]['resource-id'];
971 $resource_id = photo_new_resource();
974 $img_str = fetch_url($photo_url,true);
975 $img = new Photo($img_str);
976 if($img->is_valid()) {
978 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
980 intval($contact['id']),
981 intval($contact['uid'])
985 $img->scaleImageSquare(175);
987 $hash = $resource_id;
988 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
990 $img->scaleImage(80);
991 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
993 $img->scaleImage(48);
994 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 6);
998 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
999 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1000 dbesc(datetime_convert()),
1001 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
1002 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
1003 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
1004 intval($contact['uid']),
1005 intval($contact['id'])
1010 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1011 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1012 dbesc(notags(trim($new_name))),
1013 dbesc(datetime_convert()),
1014 intval($contact['uid']),
1015 intval($contact['id'])
1019 if(strlen($birthday)) {
1020 if(substr($birthday,0,4) != $contact['bdyear']) {
1021 logger('consume_feed: updating birthday: ' . $birthday);
1025 * Add new birthday event for this person
1027 * $bdtext is just a readable placeholder in case the event is shared
1028 * with others. We will replace it during presentation to our $importer
1029 * to contain a sparkle link and perhaps a photo.
1033 $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1036 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1037 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1038 intval($contact['uid']),
1039 intval($contact['id']),
1040 dbesc(datetime_convert()),
1041 dbesc(datetime_convert()),
1042 dbesc(datetime_convert('UTC','UTC', $birthday)),
1043 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1051 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1052 dbesc(substr($birthday,0,4)),
1053 intval($contact['uid']),
1054 intval($contact['id'])
1057 // This function is called twice without reloading the contact
1058 // Make sure we only create one event. This is why &$contact
1059 // is a reference var in this function
1061 $contact['bdyear'] = substr($birthday,0,4);
1067 // process any deleted entries
1069 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1070 if(is_array($del_entries) && count($del_entries)) {
1071 foreach($del_entries as $dentry) {
1073 if(isset($dentry['attribs']['']['ref'])) {
1074 $uri = $dentry['attribs']['']['ref'];
1076 if(isset($dentry['attribs']['']['when'])) {
1077 $when = $dentry['attribs']['']['when'];
1078 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1081 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1083 if($deleted && is_array($contact)) {
1084 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1086 intval($importer['uid']),
1087 intval($contact['id'])
1092 if(! $item['deleted'])
1093 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1095 if($item['uri'] == $item['parent-uri']) {
1096 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1097 `body` = '', `title` = ''
1098 WHERE `parent-uri` = '%s' AND `uid` = %d",
1100 dbesc(datetime_convert()),
1101 dbesc($item['uri']),
1102 intval($importer['uid'])
1106 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1107 `body` = '', `title` = ''
1108 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1110 dbesc(datetime_convert()),
1112 intval($importer['uid'])
1114 if($item['last-child']) {
1115 // ensure that last-child is set in case the comment that had it just got wiped.
1116 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1117 dbesc(datetime_convert()),
1118 dbesc($item['parent-uri']),
1119 intval($item['uid'])
1121 // who is the last child now?
1122 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
1123 ORDER BY `created` DESC LIMIT 1",
1124 dbesc($item['parent-uri']),
1125 intval($importer['uid'])
1128 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1139 // Now process the feed
1141 if($feed->get_item_quantity()) {
1143 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1145 // in inverse date order
1147 $items = array_reverse($feed->get_items());
1149 $items = $feed->get_items();
1152 foreach($items as $item) {
1155 $item_id = $item->get_id();
1156 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1157 if(isset($rawthread[0]['attribs']['']['ref'])) {
1159 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1162 if(($is_reply) && is_array($contact)) {
1164 // Have we seen it? If not, import it.
1166 $item_id = $item->get_id();
1167 $datarray = get_atom_elements($feed,$item);
1169 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1171 intval($importer['uid'])
1174 // Update content if 'updated' changes
1177 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1178 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1179 dbesc($datarray['body']),
1180 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1182 intval($importer['uid'])
1186 // update last-child if it changes
1188 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1189 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
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 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1196 intval($allow[0]['data']),
1197 dbesc(datetime_convert()),
1199 intval($importer['uid'])
1205 $force_parent = false;
1206 if($contact['network'] === 'stat') {
1207 $force_parent = true;
1208 if(strlen($datarray['title']))
1209 unset($datarray['title']);
1210 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1211 dbesc(datetime_convert()),
1213 intval($importer['uid'])
1215 $datarray['last-child'] = 1;
1218 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1219 // one way feed - no remote comment ability
1220 $datarray['last-child'] = 0;
1222 $datarray['parent-uri'] = $parent_uri;
1223 $datarray['uid'] = $importer['uid'];
1224 $datarray['contact-id'] = $contact['id'];
1225 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1226 $datarray['type'] = 'activity';
1227 $datarray['gravity'] = GRAVITY_LIKE;
1230 $r = item_store($datarray,$force_parent);
1236 // Head post of a conversation. Have we seen it? If not, import it.
1238 $item_id = $item->get_id();
1240 $datarray = get_atom_elements($feed,$item);
1242 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1244 intval($importer['uid'])
1247 // Update content if 'updated' changes
1250 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1251 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1252 dbesc($datarray['body']),
1253 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1255 intval($importer['uid'])
1259 // update last-child if it changes
1261 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1262 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1263 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1264 intval($allow[0]['data']),
1265 dbesc(datetime_convert()),
1267 intval($importer['uid'])
1273 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1274 logger('consume-feed: New follower');
1275 new_follower($importer,$contact,$datarray,$item);
1278 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
1279 lose_follower($importer,$contact,$datarray,$item);
1282 if(! is_array($contact))
1285 if($contact['network'] === 'stat' || stristr($permalink,'twitter.com')) {
1286 if(strlen($datarray['title']))
1287 unset($datarray['title']);
1288 $datarray['last-child'] = 1;
1291 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1292 // one way feed - no remote comment ability
1293 $datarray['last-child'] = 0;
1296 $datarray['parent-uri'] = $item_id;
1297 $datarray['uid'] = $importer['uid'];
1298 $datarray['contact-id'] = $contact['id'];
1299 $r = item_store($datarray);
1307 function new_follower($importer,$contact,$datarray,$item) {
1308 $url = notags(trim($datarray['author-link']));
1309 $name = notags(trim($datarray['author-name']));
1310 $photo = notags(trim($datarray['author-avatar']));
1312 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
1313 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
1314 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
1316 if(is_array($contact)) {
1317 if($contact['network'] == 'stat' && $contact['rel'] == REL_FAN) {
1318 $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
1320 intval($contact['id']),
1321 intval($importer['uid'])
1325 // send email notification to owner?
1329 // create contact record - set to readonly
1331 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
1332 `blocked`, `readonly`, `pending` )
1333 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
1334 intval($importer['uid']),
1335 dbesc(datetime_convert()),
1343 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
1344 intval($importer['uid']),
1349 $contact_record = $r[0];
1351 // create notification
1352 $hash = random_string();
1354 if(is_array($contact_record)) {
1355 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
1356 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
1357 intval($importer['uid']),
1358 intval($contact_record['id']),
1360 dbesc(datetime_convert())
1363 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1364 intval($importer['uid'])
1368 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
1369 $email_tpl = load_view_file('view/follow_notify_eml.tpl');
1370 $email = replace_macros($email_tpl, array(
1371 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
1373 '$myname' => $r[0]['username'],
1374 '$siteurl' => $a->get_baseurl(),
1375 '$sitename' => $a->config['sitename']
1377 $res = mail($r[0]['email'],
1378 t("You have a new follower at ") . $a->config['sitename'],
1380 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
1387 function lose_follower($importer,$contact,$datarray,$item) {
1389 if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
1390 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1392 intval($contact['id'])
1396 contact_remove($contact['id']);
1401 function subscribe_to_hub($url,$importer,$contact) {
1403 if(is_array($importer)) {
1404 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1405 intval($importer['uid'])
1411 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1413 // Use a single verify token, even if multiple hubs
1415 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1417 $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1419 logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
1421 if(! strlen($contact['hub-verify'])) {
1422 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
1423 dbesc($verify_token),
1424 intval($contact['id'])
1428 post_url($url,$params);
1434 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1438 $name = xmlify($name);
1439 $uri = xmlify($uri);
1442 $photo = xmlify($photo);
1446 $o .= "<name>$name</name>\r\n";
1447 $o .= "<uri>$uri</uri>\r\n";
1448 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1449 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1451 call_hooks('atom_author', $o);
1453 $o .= "</$tag>\r\n";
1457 function atom_entry($item,$type,$author,$owner,$comment = false) {
1459 if($item['deleted'])
1460 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1464 $o = "\r\n\r\n<entry>\r\n";
1466 if(is_array($author))
1467 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1469 $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
1470 if(strlen($item['owner-name']))
1471 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1473 if($item['parent'] != $item['id'])
1474 $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";
1476 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1477 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1478 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1479 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1480 $o .= '<dfrn:env>' . base64url_encode($item['body'], true) . '</dfrn:env>' . "\r\n";
1481 $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
1482 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1484 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1486 if($item['location']) {
1487 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1488 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
1492 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1494 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
1495 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
1497 $verb = construct_verb($item);
1498 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1499 $actobj = construct_activity_object($item);
1502 $actarg = construct_activity_target($item);
1506 $mentioned = get_mentions($item);
1510 call_hooks('atom_entry', $o);
1512 $o .= '</entry>' . "\r\n";
1517 function item_expire($uid,$days) {
1519 if((! $uid) || (! $days))
1522 $r = q("SELECT * FROM `item`
1524 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY
1534 logger('expire: # items=' . count($r) );
1536 foreach($r as $item) {
1538 // Only expire posts, not photos and photo comments
1540 if(strlen($item['resource-id']))
1543 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1544 dbesc(datetime_convert()),
1545 dbesc(datetime_convert()),
1551 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1552 dbesc(datetime_convert()),
1553 dbesc(datetime_convert()),
1554 dbesc($item['parent-uri']),
1555 intval($item['uid'])
1560 proc_run('php',"include/notifier.php","expire","$uid");