+
+function get_item_contact($item,$contacts) {
+ if(! count($contacts) || (! is_array($item)))
+ return false;
+ foreach($contacts as $contact) {
+ if($contact['id'] == $item['contact-id']) {
+ return $contact;
+ break; // NOTREACHED
+ }
+ }
+ return false;
+}
+
+
+function dfrn_deliver($owner,$contact,$atom) {
+
+ $a = get_app();
+
+ if((! strlen($contact['dfrn-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
+ return 3;
+
+ $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
+
+ if($contact['duplex'] && $contact['dfrn-id'])
+ $idtosend = '0:' . $orig_id;
+ if($contact['duplex'] && $contact['issued-id'])
+ $idtosend = '1:' . $orig_id;
+
+ $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
+
+ $rino_enable = get_config('system','rino_encrypt');
+
+ if(! $rino_enable)
+ $rino = 0;
+
+ $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
+
+ logger('dfrn_deliver: ' . $url);
+
+ $xml = fetch_url($url);
+
+ $curl_stat = $a->get_curl_code();
+ if(! $curl_stat)
+ return(-1); // timed out
+
+ logger('dfrn_deliver: ' . $xml);
+
+ if(! $xml)
+ return 3;
+
+ $res = simplexml_load_string($xml);
+
+ if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
+ return (($res->status) ? $res->status : 3);
+
+ $postvars = array();
+ $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
+ $challenge = hex2bin((string) $res->challenge);
+ $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
+
+ $final_dfrn_id = '';
+
+
+ if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
+ openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
+ openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
+ }
+ else {
+ openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
+ openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
+ }
+
+ $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
+
+ if(strpos($final_dfrn_id,':') == 1)
+ $final_dfrn_id = substr($final_dfrn_id,2);
+
+ if($final_dfrn_id != $orig_id) {
+ logger('dfrn_deliver: wrong dfrn_id.');
+ // did not decode properly - cannot trust this site
+ return 3;
+ }
+
+ $postvars['dfrn_id'] = $idtosend;
+ $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
+
+ if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
+ $postvars['data'] = $atom;
+ }
+ elseif($owner['page-flags'] == PAGE_COMMUNITY) {
+ $postvars['data'] = $atom;
+ }
+ else {
+ $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
+ }
+
+ if($rino && $rino_allowed) {
+ $key = substr(random_string(),0,16);
+ $data = bin2hex(aes_encrypt($postvars['data'],$key));
+ $postvars['data'] = $data;
+ logger('rino: sent key = ' . $key);
+
+ if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
+ openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
+ }
+ else {
+ openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
+ }
+
+ logger('md5 rawkey ' . md5($postvars['key']));
+
+ $postvars['key'] = bin2hex($postvars['key']);
+ }
+
+ logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
+
+ $xml = post_url($contact['notify'],$postvars);
+
+ logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
+
+ $curl_stat = $a->get_curl_code();
+ if((! $curl_stat) || (! strlen($xml)))
+ return(-1); // timed out
+
+ $res = simplexml_load_string($xml);
+
+ return $res->status;
+
+}
+
+
+/*
+ *
+ * consume_feed - process atom feed and update anything/everything we might need to update
+ *
+ * $xml = the (atom) feed to consume - no RSS spoken here, it might partially work since simplepie
+ * handles both, but we don't claim it will work well, and are reasonably certain it won't.
+ * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
+ * It is this person's stuff that is going to be updated.
+ * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
+ * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
+ * have a contact record.
+ * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
+ * might not) try and subscribe to it.
+ *
+ */
+
+function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
+
+ require_once('simplepie/simplepie.inc');
+
+ $feed = new SimplePie();
+ $feed->set_raw_data($xml);
+ if($datedir)
+ $feed->enable_order_by_date(true);
+ else
+ $feed->enable_order_by_date(false);
+ $feed->init();
+
+ // Check at the feed level for updated contact name and/or photo
+
+ $name_updated = '';
+ $new_name = '';
+ $photo_timestamp = '';
+ $photo_url = '';
+ $birthday = '';
+
+ $hubs = $feed->get_links('hub');
+
+ if(count($hubs))
+ $hub = implode(',', $hubs);
+
+ $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
+ if($rawtags) {
+ $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
+ if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
+ $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
+ $new_name = $elems['name'][0]['data'];
+ }
+ if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
+ $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
+ $photo_url = $elems['link'][0]['attribs']['']['href'];
+ }
+
+ if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
+ $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
+ }
+ }
+
+ if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
+ logger('consume_feed: Updating photo for ' . $contact['name']);
+ require_once("Photo.php");
+ $photo_failure = false;
+ $have_photo = false;
+
+ $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
+ intval($contact['id']),
+ intval($contact['uid'])
+ );
+ if(count($r)) {
+ $resource_id = $r[0]['resource-id'];
+ $have_photo = true;
+ }
+ else {
+ $resource_id = photo_new_resource();
+ }
+
+ $img_str = fetch_url($photo_url,true);
+ $img = new Photo($img_str);
+ if($img->is_valid()) {
+ if($have_photo) {
+ q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
+ dbesc($resource_id),
+ intval($contact['id']),
+ intval($contact['uid'])
+ );
+ }
+
+ $img->scaleImageSquare(175);
+
+ $hash = $resource_id;
+ $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
+
+ $img->scaleImage(80);
+ $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
+
+ $img->scaleImage(48);
+ $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 6);
+
+ $a = get_app();
+
+ q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
+ WHERE `uid` = %d AND `id` = %d LIMIT 1",
+ dbesc(datetime_convert()),
+ dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
+ dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
+ dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
+ intval($contact['uid']),
+ intval($contact['id'])
+ );
+ }
+ }
+
+ if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
+ q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+ dbesc(notags(trim($new_name))),
+ dbesc(datetime_convert()),
+ intval($contact['uid']),
+ intval($contact['id'])
+ );
+ }
+
+ if(strlen($birthday)) {
+ if(substr($birthday,0,4) != $contact['bdyear']) {
+ logger('consume_feed: updating birthday: ' . $birthday);
+
+ /**
+ *
+ * Add new birthday event for this person
+ *
+ * $bdtext is just a readable placeholder in case the event is shared
+ * with others. We will replace it during presentation to our $importer
+ * to contain a sparkle link and perhaps a photo.
+ *
+ */
+
+ $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
+
+
+ $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
+ VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
+ intval($contact['uid']),
+ intval($contact['id']),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert('UTC','UTC', $birthday)),
+ dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
+ dbesc($bdtext),
+ dbesc('birthday')
+ );
+
+
+ // update bdyear
+
+ q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+ dbesc(substr($birthday,0,4)),
+ intval($contact['uid']),
+ intval($contact['id'])
+ );
+
+ // This function is called twice without reloading the contact
+ // Make sure we only create one event. This is why &$contact
+ // is a reference var in this function
+
+ $contact['bdyear'] = substr($birthday,0,4);
+ }
+
+ }
+
+ // Now process the feed
+ if($feed->get_item_quantity()) {
+
+ // in inverse date order
+ if ($datedir)
+ $items = array_reverse($feed->get_items());
+ else
+ $items = $feed->get_items();
+
+ foreach($items as $item) {
+
+ $deleted = false;
+
+ $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
+ if(isset($rawdelete[0]['attribs']['']['ref'])) {
+ $uri = $rawthread[0]['attribs']['']['ref'];
+ $deleted = true;
+ if(isset($rawdelete[0]['attribs']['']['when'])) {
+ $when = $rawthread[0]['attribs']['']['when'];
+ $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
+ }
+ else
+ $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
+ }
+ if($deleted && is_array($contact)) {
+ $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
+ dbesc($uri),
+ intval($importer['uid']),
+ intval($contact['id'])
+ );
+ if(count($r)) {
+ $item = $r[0];
+ if($item['uri'] == $item['parent-uri']) {
+ $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
+ `body` = '', `title` = ''
+ WHERE `parent-uri` = '%s' AND `uid` = %d",
+ dbesc($when),
+ dbesc(datetime_convert()),
+ dbesc($item['uri']),
+ intval($importer['uid'])
+ );
+ }
+ else {
+ $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
+ `body` = '', `title` = ''
+ WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($when),
+ dbesc(datetime_convert()),
+ dbesc($uri),
+ intval($importer['uid'])
+ );
+ if($item['last-child']) {
+ // ensure that last-child is set in case the comment that had it just got wiped.
+ $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
+ dbesc(datetime_convert()),
+ dbesc($item['parent-uri']),
+ intval($item['uid'])
+ );
+ // who is the last child now?
+ $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
+ ORDER BY `created` DESC LIMIT 1",
+ dbesc($item['parent-uri']),
+ intval($importer['uid'])
+ );
+ if(count($r)) {
+ q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
+ intval($r[0]['id'])
+ );
+ }
+ }
+ }
+ }
+ continue;
+ }
+
+
+ $is_reply = false;
+ $item_id = $item->get_id();
+ $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
+ if(isset($rawthread[0]['attribs']['']['ref'])) {
+ $is_reply = true;
+ $parent_uri = $rawthread[0]['attribs']['']['ref'];
+ }
+
+
+ if(($is_reply) && is_array($contact)) {
+
+ // Have we seen it? If not, import it.
+
+ $item_id = $item->get_id();
+
+ $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($item_id),
+ intval($importer['uid'])
+ );
+ // FIXME update content if 'updated' changes
+ if(count($r)) {
+ $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
+ if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
+ $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
+ dbesc(datetime_convert()),
+ dbesc($parent_uri),
+ intval($importer['uid'])
+ );
+ $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ intval($allow[0]['data']),
+ dbesc(datetime_convert()),
+ dbesc($item_id),
+ intval($importer['uid'])
+ );
+ }
+ continue;
+ }
+ $datarray = get_atom_elements($feed,$item);
+ if($contact['network'] === 'stat') {
+ if(strlen($datarray['title']))
+ unset($datarray['title']);
+ $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
+ dbesc(datetime_convert()),
+ dbesc($parent_uri),
+ intval($importer['uid'])
+ );
+ $datarray['last-child'] = 1;
+ }
+ $datarray['parent-uri'] = $parent_uri;
+ $datarray['uid'] = $importer['uid'];
+ $datarray['contact-id'] = $contact['id'];
+ if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
+ $datarray['type'] = 'activity';
+ $datarray['gravity'] = GRAVITY_LIKE;
+ }
+
+ $r = item_store($datarray);
+ continue;
+ }
+
+ else {
+ // Head post of a conversation. Have we seen it? If not, import it.
+
+ $item_id = $item->get_id();
+ $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($item_id),
+ intval($importer['uid'])
+ );
+ if(count($r)) {
+ $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
+ if($allow && $allow[0]['data'] != $r[0]['last-child']) {
+ $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ intval($allow[0]['data']),
+ dbesc(datetime_convert()),
+ dbesc($item_id),
+ intval($importer['uid'])
+ );
+ }
+ continue;
+ }
+ $datarray = get_atom_elements($feed,$item);
+
+ if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
+ logger('consume-feed: New follower');
+ new_follower($importer,$contact,$datarray,$item);
+ return;
+ }
+ if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
+ lose_follower($importer,$contact,$datarray,$item);
+ return;
+ }
+ if(! is_array($contact))
+ return;
+
+ if($contact['network'] === 'stat') {
+ if(strlen($datarray['title']))
+ unset($datarray['title']);
+ $datarray['last-child'] = 1;
+ }
+
+ $datarray['parent-uri'] = $item_id;
+ $datarray['uid'] = $importer['uid'];
+ $datarray['contact-id'] = $contact['id'];
+ $r = item_store($datarray);
+ continue;
+
+ }
+ }
+ }
+
+}
+
+function new_follower($importer,$contact,$datarray,$item) {
+ $url = notags(trim($datarray['author-link']));
+ $name = notags(trim($datarray['author-name']));
+ $photo = notags(trim($datarray['author-avatar']));
+
+ $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
+ if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
+ $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
+
+ if(is_array($contact)) {
+ if($contact['network'] == 'stat' && $contact['rel'] == REL_FAN) {
+ $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
+ intval(REL_BUD),
+ intval($contact['id']),
+ intval($importer['uid'])
+ );
+ }
+
+ // send email notification to owner?
+ }
+ else {
+
+ // create contact record - set to readonly
+
+ $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
+ `blocked`, `readonly`, `pending` )
+ VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
+ intval($importer['uid']),
+ dbesc(datetime_convert()),
+ dbesc($url),
+ dbesc($name),
+ dbesc($nick),
+ dbesc($photo),
+ dbesc('stat'),
+ intval(REL_VIP)
+ );
+ $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
+ intval($importer['uid']),
+ dbesc($url),
+ intval(REL_VIP)
+ );
+ if(count($r))
+ $contact_record = $r[0];
+
+ // create notification
+ $hash = random_string();
+
+ if(is_array($contact_record)) {
+ $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
+ VALUES ( %d, %d, 0, 0, '%s', '%s' )",
+ intval($importer['uid']),
+ intval($contact_record['id']),
+ dbesc($hash),
+ dbesc(datetime_convert())
+ );
+ }
+ $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
+ intval($importer['uid'])
+ );
+ $a = get_app();
+ if(count($r)) {
+ if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
+ $email_tpl = load_view_file('view/follow_notify_eml.tpl');
+ $email = replace_macros($email_tpl, array(
+ '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
+ '$url' => $url,
+ '$myname' => $r[0]['username'],
+ '$siteurl' => $a->get_baseurl(),
+ '$sitename' => $a->config['sitename']
+ ));
+ $res = mail($r[0]['email'],
+ t("You have a new follower at ") . $a->config['sitename'],
+ $email,
+ 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
+
+ }
+ }
+ }
+}
+
+function lose_follower($importer,$contact,$datarray,$item) {
+
+ if(($contact['rel'] == REL_BUD) || ($contact['rel'] == REL_FAN)) {
+ q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
+ intval(REL_FAN),
+ intval($contact['id'])
+ );
+ }
+ else {
+ contact_remove($contact['id']);
+ }
+}
+
+
+function subscribe_to_hub($url,$importer,$contact) {
+
+ if(is_array($importer)) {
+ $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
+ intval($importer['uid'])
+ );
+ }
+ if(! count($r))
+ return;
+
+ $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
+
+ // Use a single verify token, even if multiple hubs
+
+ $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
+
+ $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
+
+ logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
+
+ if(! strlen($contact['hub-verify'])) {
+ $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc($verify_token),
+ intval($contact['id'])
+ );
+ }
+
+ post_url($url,$params);
+ return;
+
+}
+
+
+function atom_author($tag,$name,$uri,$h,$w,$photo) {
+ $o = '';
+ if(! $tag)
+ return $o;
+ $name = xmlify($name);
+ $uri = xmlify($uri);
+ $h = intval($h);
+ $w = intval($w);
+ $photo = xmlify($photo);
+
+
+ $o .= "<$tag>\r\n";
+ $o .= "<name>$name</name>\r\n";
+ $o .= "<uri>$uri</uri>\r\n";
+ $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
+ $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
+
+ call_hooks('atom_author', $o);
+
+ $o .= "</$tag>\r\n";
+ return $o;
+}
+
+function atom_entry($item,$type,$author,$owner,$comment = false) {
+
+ if($item['deleted'])
+ return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
+
+ $a = get_app();
+
+ $o = "\r\n\r\n<entry>\r\n";
+
+ if(is_array($author))
+ $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
+ else
+ $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
+ if(strlen($item['owner-name']))
+ $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
+
+ if($item['parent'] != $item['id'])
+ $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";
+
+ $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
+ $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
+ $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
+ $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
+ $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
+ $o .= '<link rel="alternate" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
+ if($comment)
+ $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
+
+ if($item['location']) {
+ $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
+ $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
+ }
+
+ if($item['coord'])
+ $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
+
+ if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
+ $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
+
+ $verb = construct_verb($item);
+ $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
+ $actobj = construct_activity_object($item);
+ if(strlen($actobj))
+ $o .= $actobj;
+ $actarg = construct_activity_target($item);
+ if(strlen($actarg))
+ $o .= $actarg;
+
+ $mentioned = get_mentions($item);
+ if($mentioned)
+ $o .= $mentioned;
+
+ call_hooks('atom_entry', $o);
+
+ $o .= '</entry>' . "\r\n";
+
+ return $o;
+}
+