4 function get_feed_for(&$a, $dfrn_id, $owner_id, $last_update) {
6 require_once('bbcode.php');
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",
37 $r = q("SELECT * FROM `contact` WHERE ( `issued-id` = '%s' OR ( `duplex` = 1 AND `dfrn-id` = '%s' )) AND `uid` = %d LIMIT 1",
46 $groups = init_groups_visitor($contact['id']);
49 for($x = 0; $x < count($groups); $x ++)
50 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
51 $gs = implode('|', $groups);
54 $gs = '<<>>' ; // Impossible to match
56 $sql_extra = sprintf("
57 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
58 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
59 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
60 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
62 intval($contact['id']),
63 intval($contact['id']),
69 if($dfrn_id == '' || $dfrn_id == '*')
74 if(! strlen($last_update))
75 $last_update = 'now - 30 days';
76 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
78 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
79 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
80 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
81 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
82 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
83 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
84 WHERE `item`.`uid` = %d AND `item`.`visible` = 1
85 AND NOT `item`.`type` IN ( 'remote', 'net-comment' ) AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
86 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
88 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
99 $feed_template = file_get_contents('view/atom_feed.tpl');
100 $tomb_template = file_get_contents('view/atom_tomb.tpl');
101 $item_template = file_get_contents('view/atom_item.tpl');
102 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
107 $atom .= replace_macros($feed_template, array(
108 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
109 '$feed_title' => xmlify($owner['name']),
110 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
111 '$name' => xmlify($owner['name']),
112 '$profile_page' => xmlify($owner['url']),
113 '$photo' => xmlify($owner['photo']),
114 '$thumb' => xmlify($owner['thumb']),
115 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
116 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
117 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME))
121 foreach($items as $item) {
123 // public feeds get html, our own nodes use bbcode
125 if($dfrn_id == '*') {
126 $item['body'] = bbcode($item['body']);
133 if($item['deleted']) {
134 $atom .= replace_macros($tomb_template, array(
135 '$id' => xmlify($item['uri']),
136 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
140 $verb = construct_verb($item);
141 $actobj = construct_activity($item);
143 if($item['parent'] == $item['id']) {
144 $atom .= replace_macros($item_template, array(
145 '$name' => xmlify($item['name']),
146 '$profile_page' => xmlify($item['url']),
147 '$thumb' => xmlify($item['thumb']),
148 '$owner_name' => xmlify($item['owner-name']),
149 '$owner_profile_page' => xmlify($item['owner-link']),
150 '$owner_thumb' => xmlify($item['owner-avatar']),
151 '$item_id' => xmlify($item['uri']),
152 '$title' => xmlify($item['title']),
153 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
154 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
155 '$location' => xmlify($item['location']),
157 '$content' => xmlify($item['body']),
158 '$verb' => xmlify($verb),
159 '$actobj' => $actobj, // do not xmlify
160 '$comment_allow' => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
164 $atom .= replace_macros($cmnt_template, array(
165 '$name' => xmlify($item['name']),
166 '$profile_page' => xmlify($item['url']),
167 '$thumb' => xmlify($item['thumb']),
168 '$item_id' => xmlify($item['uri']),
169 '$title' => xmlify($item['title']),
170 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
171 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
173 '$content' => xmlify($item['body']),
174 '$verb' => xmlify($verb),
175 '$actobj' => $actobj, // do not xmlify
176 '$parent_id' => xmlify($item['parent-uri']),
177 '$comment_allow' => (($item['last-child']) ? 1 : 0)
183 $atom .= '</feed>' . "\r\n";
188 function construct_verb($item) {
190 return $item['verb'];
191 return ACTIVITY_POST;
194 function construct_activity($item) {
196 if($item['type'] == 'activity') {
206 function get_atom_elements($item) {
208 require_once('library/HTMLPurifier.auto.php');
209 require_once('include/html2bbcode.php');
213 $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
215 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
216 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
219 $author = $item->get_author();
220 $res['author-name'] = unxmlify($author->get_name());
221 $res['author-link'] = unxmlify($author->get_link());
222 if(! $res['author-avatar'])
223 $res['author-avatar'] = unxmlify($author->get_avatar());
224 $res['uri'] = unxmlify($item->get_id());
225 $res['title'] = unxmlify($item->get_title());
226 $res['body'] = unxmlify($item->get_content());
228 $maxlen = get_max_import_size();
229 if($maxlen && (strlen($res['body']) > $maxlen))
230 $res['body'] = substr($res['body'],0, $maxlen);
232 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
233 // the content type. Our own network only emits text normally, though it might have been converted to
234 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
235 // have to assume it is all html and needs to be purified.
237 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
238 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
239 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
243 if(strpos($res['body'],'<')) {
245 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
246 '[youtube]$1[/youtube]', $res['body']);
248 $config = HTMLPurifier_Config::createDefault();
249 $config->set('Core.DefinitionCache', null);
251 // we shouldn't need a whitelist, because the bbcode converter
252 // will strip out any unsupported tags.
253 // $config->set('HTML.Allowed', 'p,b,a[href],i');
255 $purifier = new HTMLPurifier($config);
256 $res['body'] = $purifier->purify($res['body']);
260 $res['body'] = html2bbcode($res['body']);
263 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
264 if($allow && $allow[0]['data'] == 1)
265 $res['last-child'] = 1;
267 $res['last-child'] = 0;
269 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
271 $res['created'] = unxmlify($rawcreated[0]['data']);
273 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
275 $res['location'] = unxmlify($rawlocation[0]['data']);
278 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
280 $res['edited'] = unxmlify($rawcreated[0]['data']);
282 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
283 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
284 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
285 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
286 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
287 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
288 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
289 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
290 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
292 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
293 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
294 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
295 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
297 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
298 // select between supported verbs
300 $res['verb'] = unxmlify($rawverb[0]['data']);
302 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
304 $res['object-type'] = $rawobj[0]['object-type'][0]['data'];
305 $res['object'] = $rawobj[0];
311 function post_remote($a,$arr) {
317 $arr['gravity'] = intval($arr['gravity']);
318 elseif($arr['parent-uri'] == $arr['uri'])
320 elseif($arr['verb'] == ACTIVITY_POST)
324 $arr['type'] = 'remote';
325 $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
326 $arr['uri'] = notags(trim($arr['uri']));
327 $arr['author-name'] = notags(trim($arr['author-name']));
328 $arr['author-link'] = notags(trim($arr['author-link']));
329 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
330 $arr['owner-name'] = notags(trim($arr['owner-name']));
331 $arr['owner-link'] = notags(trim($arr['owner-link']));
332 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
333 $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
334 $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
335 $arr['changed'] = datetime_convert();
336 $arr['title'] = notags(trim($arr['title']));
337 $arr['location'] = notags(trim($arr['location']));
338 $arr['body'] = escape_tags(trim($arr['body']));
339 $arr['last-child'] = intval($arr['last-child']);
342 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
343 $arr['verb'] = notags(trim($arr['verb']));
344 $arr['object-type'] = notags(trim($arr['object-type']));
345 $arr['object'] = trim($arr['object']);
348 $parent_missing = false;
352 $r = q("INSERT INTO `item` (`"
353 . implode("`, `", array_keys($arr))
355 . implode("', '", array_values($arr))
358 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
359 dbesc($arr['parent-uri']),
364 $parent_id = $r[0]['id'];
366 $parent_missing = true;
369 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
370 $arr['uri'], // already dbesc'd
374 $current_post = $r[0]['id'];
378 if($parent_missing) {
380 // perhaps the parent was deleted, but in any case, this thread is dead
381 // and unfortunately our brand new item now has to be destroyed
383 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
384 intval($current_post)
389 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
391 intval($current_post)
394 return $current_post;