3 require_once('bbcode.php');
5 function get_feed_for(&$a, $dfrn_id, $owner_id, $last_update, $direction = 0) {
8 // default permissions - anonymous user
17 if(strlen($owner_id) && ! intval($owner_id)) {
18 $r = q("SELECT `uid`, `nickname` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
22 $owner_id = $r[0]['uid'];
23 $owner_nick = $r[0]['nickname'];
27 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
32 $owner['nickname'] = $owner_nick;
37 if($dfrn_id && $dfrn_id != '*') {
42 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
46 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
47 $my_id = '1:' . $dfrn_id;
50 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
51 $my_id = '0:' . $dfrn_id;
58 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
66 $groups = init_groups_visitor($contact['id']);
69 for($x = 0; $x < count($groups); $x ++)
70 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
71 $gs = implode('|', $groups);
74 $gs = '<<>>' ; // Impossible to match
76 $sql_extra = sprintf("
77 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
78 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
79 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
80 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
82 intval($contact['id']),
83 intval($contact['id']),
89 if($dfrn_id === '' || $dfrn_id === '*')
94 if(! strlen($last_update))
95 $last_update = 'now -30 days';
97 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
99 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
100 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
101 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
102 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
103 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
104 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
105 WHERE `item`.`uid` = %d AND `item`.`visible` = 1
106 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
107 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
109 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
116 // Will check further below if this actually returned results.
117 // We will provide an empty feed in any case.
121 $feed_template = load_view_file('view/atom_feed.tpl');
125 $hub = get_config('system','huburl');
129 $hubs = explode(',', $hub);
131 foreach($hubs as $h) {
135 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
140 $salmon = '<link rel="salmon" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ;
141 $salmon .= '<link rel="http://salmon-protocol.org/ns/salmon-replies" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ;
142 $salmon .= '<link rel="http://salmon-protocol.org/ns/salmon-mention" href="' . xmlify($a->get_baseurl() . '/salmon/' . $owner_nick) . '" />' . "\n" ;
145 $atom .= replace_macros($feed_template, array(
146 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
147 '$feed_title' => xmlify($owner['name']),
148 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
150 '$salmon' => $salmon,
151 '$name' => xmlify($owner['name']),
152 '$profile_page' => xmlify($owner['url']),
153 '$photo' => xmlify($owner['photo']),
154 '$thumb' => xmlify($owner['thumb']),
155 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
156 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
157 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME))
161 if(! count($items)) {
162 $atom .= '</feed>' . "\r\n";
166 foreach($items as $item) {
168 // public feeds get html, our own nodes use bbcode
170 if($dfrn_id === '*') {
177 $atom .= atom_entry($item,$type,null,$owner,true);
180 $atom .= '</feed>' . "\r\n";
185 function construct_verb($item) {
187 return $item['verb'];
188 return ACTIVITY_POST;
191 function construct_activity($item) {
193 if($item['object']) {
194 $o = '<as:object>' . "\r\n";
195 $r = @simplexml_load_string($item['object']);
197 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
199 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
201 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
203 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
205 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
206 $o .= '</as:object>' . "\r\n";
216 function get_atom_elements($feed,$item) {
218 require_once('library/HTMLPurifier.auto.php');
219 require_once('include/html2bbcode.php');
221 $best_photo = array();
225 $author = $item->get_author();
226 $res['author-name'] = unxmlify($author->get_name());
227 $res['author-link'] = unxmlify($author->get_link());
228 $res['uri'] = unxmlify($item->get_id());
229 $res['title'] = unxmlify($item->get_title());
230 $res['body'] = unxmlify($item->get_content());
233 // look for a photo. We should check media size and find the best one,
234 // but for now let's just find any author photo
236 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
238 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
239 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
240 foreach($base as $link) {
241 if(! $res['author-avatar']) {
242 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
243 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
248 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
250 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
251 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
252 if($base && count($base)) {
253 foreach($base as $link) {
254 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
255 $res['author-link'] = unxmlify($link['attribs']['']['href']);
256 if(! $res['author-avatar']) {
257 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
258 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
264 // No photo/profile-link on the item - look at the feed level
266 if((! $res['author-link']) || (! $res['author-avatar'])) {
267 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
268 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
269 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
270 foreach($base as $link) {
271 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
272 $res['author-link'] = unxmlify($link['attribs']['']['href']);
273 if(! $res['author-avatar']) {
274 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
275 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
280 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
282 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
283 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
285 if($base && count($base)) {
286 foreach($base as $link) {
287 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
288 $res['author-link'] = unxmlify($link['attribs']['']['href']);
289 if(! $res['author-avatar']) {
290 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
291 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
299 $maxlen = get_max_import_size();
300 if($maxlen && (strlen($res['body']) > $maxlen))
301 $res['body'] = substr($res['body'],0, $maxlen);
303 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
304 // the content type. Our own network only emits text normally, though it might have been converted to
305 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
306 // have to assume it is all html and needs to be purified.
308 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
309 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
310 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
314 if(strpos($res['body'],'<')) {
316 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
317 '[youtube]$1[/youtube]', $res['body']);
319 $config = HTMLPurifier_Config::createDefault();
320 $config->set('Cache.DefinitionImpl', null);
322 // we shouldn't need a whitelist, because the bbcode converter
323 // will strip out any unsupported tags.
324 // $config->set('HTML.Allowed', 'p,b,a[href],i');
326 $purifier = new HTMLPurifier($config);
327 $res['body'] = $purifier->purify($res['body']);
331 $res['body'] = html2bbcode($res['body']);
334 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
335 if($allow && $allow[0]['data'] == 1)
336 $res['last-child'] = 1;
338 $res['last-child'] = 0;
340 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
342 $res['created'] = unxmlify($rawcreated[0]['data']);
344 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
346 $res['location'] = unxmlify($rawlocation[0]['data']);
349 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
351 $res['edited'] = unxmlify($rawcreated[0]['data']);
353 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
354 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
355 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
356 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
357 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
358 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
359 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
360 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
361 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
363 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
364 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
366 foreach($base as $link) {
367 if(! $res['owner-avatar']) {
368 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
369 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
374 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
376 $res['coord'] = unxmlify($rawgeo[0]['data']);
379 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
380 // select between supported verbs
382 $res['verb'] = unxmlify($rawverb[0]['data']);
384 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
387 $res['object'] = '<object>' . "\n";
388 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
389 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
390 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
392 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
393 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
395 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] === 'alternate')
396 $res['object'] .= '<link>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href'] . '</link>' . "\n";
397 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
398 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
399 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
400 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
402 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
403 if(strpos($body,'<')) {
405 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
406 '[youtube]$1[/youtube]', $body);
408 $config = HTMLPurifier_Config::createDefault();
409 $config->set('Cache.DefinitionImpl', null);
411 $purifier = new HTMLPurifier($config);
412 $body = $purifier->purify($body);
415 $body = html2bbcode($body);
416 $res['object'] .= '<content>' . $body . '</content>' . "\n";
419 $res['object'] .= '</object>' . "\n";
425 function item_store($arr) {
428 $arr['gravity'] = intval($arr['gravity']);
429 elseif($arr['parent-uri'] == $arr['uri'])
431 elseif(activity_match($arr['verb'],ACTIVITY_POST))
435 $arr['type'] = 'remote';
436 $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
437 $arr['uri'] = notags(trim($arr['uri']));
438 $arr['author-name'] = notags(trim($arr['author-name']));
439 $arr['author-link'] = notags(trim($arr['author-link']));
440 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
441 $arr['owner-name'] = notags(trim($arr['owner-name']));
442 $arr['owner-link'] = notags(trim($arr['owner-link']));
443 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
444 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
445 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
446 $arr['changed'] = datetime_convert();
447 $arr['title'] = notags(trim($arr['title']));
448 $arr['location'] = notags(trim($arr['location']));
449 $arr['coord'] = notags(trim($arr['coord']));
450 $arr['body'] = escape_tags(trim($arr['body']));
451 $arr['last-child'] = intval($arr['last-child']);
452 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1);
454 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
455 $arr['verb'] = notags(trim($arr['verb']));
456 $arr['object-type'] = notags(trim($arr['object-type']));
457 $arr['object'] = trim($arr['object']);
460 $parent_missing = false;
464 $r = q("INSERT INTO `item` (`"
465 . implode("`, `", array_keys($arr))
467 . implode("', '", array_values($arr))
470 // find the parent and snarf the item id and ACL's
472 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
473 dbesc($arr['parent-uri']),
478 $parent_id = $r[0]['id'];
479 $allow_cid = $r[0]['allow_cid'];
480 $allow_gid = $r[0]['allow_gid'];
481 $deny_cid = $r[0]['deny_cid'];
482 $deny_gid = $r[0]['deny_gid'];
485 $parent_missing = true;
488 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
489 $arr['uri'], // already dbesc'd
493 $current_post = $r[0]['id'];
497 if($parent_missing) {
499 // perhaps the parent was deleted, but in any case, this thread is dead
500 // and unfortunately our brand new item now has to be destroyed
502 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
503 intval($current_post)
508 // Set parent id - all of the parent's ACL's are also inherited by this post
510 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
511 `deny_cid` = '%s', `deny_gid` = '%s' WHERE `id` = %d LIMIT 1",
517 intval($current_post)
520 return $current_post;
523 function get_item_contact($item,$contacts) {
524 if(! count($contacts) || (! is_array($item)))
526 foreach($contacts as $contact) {
527 if($contact['id'] == $item['contact-id']) {
536 function dfrn_deliver($owner,$contact,$atom) {
539 if((! strlen($contact['dfrn-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
542 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
544 if($contact['duplex'] && $contact['dfrn-id'])
545 $idtosend = '0:' . $orig_id;
546 if($contact['duplex'] && $contact['issued-id'])
547 $idtosend = '1:' . $orig_id;
549 $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION ;
551 logger('dfrn_deliver: ' . $url);
553 $xml = fetch_url($url);
555 logger('dfrn_deliver: ' . $xml);
560 $res = simplexml_load_string($xml);
562 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
563 return (($res->status) ? $res->status : 3);
566 $sent_dfrn_id = hex2bin($res->dfrn_id);
567 $challenge = hex2bin($res->challenge);
573 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
574 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
575 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
578 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
579 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
582 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
584 if(strpos($final_dfrn_id,':') == 1)
585 $final_dfrn_id = substr($final_dfrn_id,2);
587 if($final_dfrn_id != $orig_id) {
588 logger('dfrn_deliver: wrong dfrn_id.');
589 // did not decode properly - cannot trust this site
593 $postvars['dfrn_id'] = $idtosend;
594 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
596 if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
597 $postvars['data'] = $atom;
599 elseif($owner['page-flags'] == PAGE_COMMUNITY) {
600 $postvars['data'] = $atom;
603 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
606 $xml = post_url($contact['notify'],$postvars);
608 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true) . "\n" . "RECEIVING: " . $xml);
610 $res = simplexml_load_string($xml);
619 * consume_feed - process atom feed and update anything/everything we might need to update
621 * $xml = the (atom) feed to consume - no RSS spoken here, it might partially work since simplepie
622 * handles both, but we don't claim it will work well, and are reasonably certain it won't.
623 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
624 * It is this person's stuff that is going to be updated.
625 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
626 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
627 * have a contact record.
628 * $hub = should wefind ahub declation in the feed, pass it back to our calling process, who might (or
629 * might not) try and subscribe to it.
633 function consume_feed($xml,$importer,$contact, &$hub) {
635 require_once('simplepie/simplepie.inc');
637 $feed = new SimplePie();
638 $feed->set_raw_data($xml);
639 $feed->enable_order_by_date(false);
642 // Check at the feed level for updated contact name and/or photo
643 $debugging = get_config('system','debugging');
647 $photo_timestamp = '';
651 $hubs = $feed->get_links('hub');
654 $hub = implode(',', $hubs);
656 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
658 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
659 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
660 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
661 $new_name = $elems['name'][0]['data'];
663 if(($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
664 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
665 $photo_url = $elems['link'][0]['attribs']['']['href'];
668 if(! $photo_timestamp) {
669 $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
670 if($photo_rawupdate) {
671 $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
672 $photo_url = $feed->get_image_url();
675 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
677 require_once("Photo.php");
678 $photo_failure = false;
680 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
681 intval($contact['id']),
682 intval($contact['uid'])
685 $resource_id = $r[0]['resource-id'];
686 $img_str = fetch_url($photo_url,true);
687 $img = new Photo($img_str);
688 if($img->is_valid()) {
689 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
691 intval($contact['id']),
692 intval($contact['uid'])
695 $img->scaleImageSquare(175);
697 $hash = $resource_id;
698 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
700 $img->scaleImage(80);
701 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
703 q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
704 dbesc(datetime_convert()),
705 intval($contact['uid']),
706 intval($contact['id'])
712 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
713 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
714 dbesc(notags(trim($new_name))),
715 dbesc(datetime_convert()),
716 intval($contact['uid']),
717 intval($contact['id'])
721 // Now process the feed
722 if($feed->get_item_quantity()) {
723 foreach($feed->get_items() as $item) {
727 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
728 if(isset($rawdelete[0]['attribs']['']['ref'])) {
729 $uri = $rawthread[0]['attribs']['']['ref'];
731 if(isset($rawdelete[0]['attribs']['']['when'])) {
732 $when = $rawthread[0]['attribs']['']['when'];
733 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
736 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
738 if($deleted && is_array($contact)) {
739 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
741 intval($importer['uid']),
742 intval($contact['id'])
746 if($item['uri'] == $item['parent-uri']) {
747 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
748 `body` = '', `title` = ''
749 WHERE `parent-uri` = '%s' AND `uid` = %d",
751 dbesc(datetime_convert()),
753 intval($importer['uid'])
757 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
758 `body` = '', `title` = ''
759 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
761 dbesc(datetime_convert()),
763 intval($importer['uid'])
765 if($item['last-child']) {
766 // ensure that last-child is set in case the comment that had it just got wiped.
767 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
768 dbesc(datetime_convert()),
769 dbesc($item['parent-uri']),
772 // who is the last child now?
773 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
774 ORDER BY `created` DESC LIMIT 1",
775 dbesc($item['parent-uri']),
776 intval($importer['uid'])
779 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
791 $item_id = $item->get_id();
792 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
793 if(isset($rawthread[0]['attribs']['']['ref'])) {
795 $parent_uri = $rawthread[0]['attribs']['']['ref'];
799 if(($is_reply) && is_array($contact)) {
801 // Have we seen it? If not, import it.
803 $item_id = $item->get_id();
805 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
807 intval($importer['uid'])
809 // FIXME update content if 'updated' changes
811 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
812 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
813 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
814 dbesc(datetime_convert()),
816 intval($importer['uid'])
818 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
819 intval($allow[0]['data']),
820 dbesc(datetime_convert()),
822 intval($importer['uid'])
828 $datarray = get_atom_elements($feed,$item);
829 if($contact['network'] === 'stat' && strlen($datarray['title']))
830 unset($datarray['title']);
831 $datarray['parent-uri'] = $parent_uri;
832 $datarray['uid'] = $importer['uid'];
833 $datarray['contact-id'] = $contact['id'];
834 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
835 $datarray['type'] = 'activity';
836 $datarray['gravity'] = GRAVITY_LIKE;
839 $r = item_store($datarray);
844 // Head post of a conversation. Have we seen it? If not, import it.
846 $item_id = $item->get_id();
847 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
849 intval($importer['uid'])
852 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
853 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
854 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
855 intval($allow[0]['data']),
856 dbesc(datetime_convert()),
858 intval($importer['uid'])
863 $datarray = get_atom_elements($feed,$item);
865 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
866 logger('consume-feed: New follower');
867 new_follower($importer,$contact,$datarray,$item);
870 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
871 lose_follower($importer,$contact,$datarray,$item);
874 if(! is_array($contact))
877 if($contact['network'] === 'stat') {
878 if(strlen($datarray['title']))
879 unset($datarray['title']);
880 if(($contact['rel'] == REL_VIP) || ($contact['rel'] == REL_BUD))
881 $datarray['last-child'] = 1;
883 $datarray['parent-uri'] = $item_id;
884 $datarray['uid'] = $importer['uid'];
885 $datarray['contact-id'] = $contact['id'];
886 $r = item_store($datarray);
895 function new_follower($importer,$contact,$datarray,$item) {
896 $url = notags(trim($datarray['author-link']));
897 $name = notags(trim($datarray['author-name']));
898 $photo = notags(trim($datarray['author-avatar']));
900 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
901 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
902 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
904 if(is_array($contact)) {
905 if($contact['network'] == 'stat' && $contact['rel'] == REL_FAN) {
906 $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
908 intval($contact['id']),
909 intval($importer['uid'])
913 // send email notification to owner?
917 // create contact record - set to readonly
919 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
920 `blocked`, `readonly`, `pending` )
921 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
922 intval($importer['uid']),
923 dbesc(datetime_convert()),
931 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
932 intval($importer['uid']),
937 $contact_record = $r[0];
939 // create notification
940 $hash = random_string();
942 if(is_array($contact_record)) {
943 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
944 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
945 intval($importer['uid']),
946 intval($contact_record['id']),
948 dbesc(datetime_convert())
954 function lose_follower($importer,$contact,$datarray,$item) {
956 if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
957 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
959 intval($contact['id'])
963 contact_remove($contact['id']);
968 function subscribe_to_hub($url,$importer,$contact) {
970 if(is_array($importer)) {
971 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
972 intval($importer['uid'])
978 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
980 // Use a single verify token, even if multiple hubs
982 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
984 $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
986 if(! strlen($contact['hub-verify'])) {
987 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
988 dbesc($verify_token),
989 intval($contact['id'])
993 post_url($url,$params);
999 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1003 $name = xmlify($name);
1004 $uri = xmlify($uri);
1007 $photo = xmlify($photo);
1011 $o .= "<name>$name</name>\r\n";
1012 $o .= "<uri>$uri</uri>\r\n";
1013 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1014 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1015 $o .= "</$tag>\r\n";
1019 function atom_entry($item,$type,$author,$owner,$comment = false) {
1021 if($item['deleted'])
1022 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1028 if(is_array($author))
1029 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1031 $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
1032 if(strlen($item['owner-name']))
1033 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1035 if($item['parent'] != $item['id'])
1036 $o .= '<thr:in-reply-to ref="' . xmlify($item['parent-uri']) . '" />' . "\r\n";
1038 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1039 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1040 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1041 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1042 $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
1043 $o .= '<link rel="alternate" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1045 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1046 if($item['location'])
1047 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1049 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1051 $verb = construct_verb($item);
1052 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1053 $actobj = construct_activity($item);
1057 $mentioned = get_mentions($item);
1061 $o .= '</entry>' . "\r\n";