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",
35 if($dfrn_id && $dfrn_id != '*') {
40 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
44 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45 $my_id = '1:' . $dfrn_id;
48 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
49 $my_id = '0:' . $dfrn_id;
56 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
64 $groups = init_groups_visitor($contact['id']);
67 for($x = 0; $x < count($groups); $x ++)
68 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
69 $gs = implode('|', $groups);
72 $gs = '<<>>' ; // Impossible to match
74 $sql_extra = sprintf("
75 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
76 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
77 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
78 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
80 intval($contact['id']),
81 intval($contact['id']),
87 if($dfrn_id == '' || $dfrn_id == '*')
92 if(! strlen($last_update))
93 $last_update = 'now - 30 days';
95 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
97 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
98 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
99 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
100 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
101 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
102 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
103 WHERE `item`.`uid` = %d AND `item`.`visible` = 1
104 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
105 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
107 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
114 // Will check further below if this actually returned results.
115 // We will provide an empty feed in any case.
119 $feed_template = file_get_contents('view/atom_feed.tpl');
120 $tomb_template = file_get_contents('view/atom_tomb.tpl');
121 $item_template = file_get_contents('view/atom_item.tpl');
122 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
127 $atom .= replace_macros($feed_template, array(
128 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
129 '$feed_title' => xmlify($owner['name']),
130 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
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))
141 if(! count($items)) {
142 $atom .= '</feed>' . "\r\n";
146 foreach($items as $item) {
148 // public feeds get html, our own nodes use bbcode
150 if($dfrn_id == '*') {
151 $item['body'] = bbcode($item['body']);
158 if($item['deleted']) {
159 $atom .= replace_macros($tomb_template, array(
160 '$id' => xmlify($item['uri']),
161 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
165 $verb = construct_verb($item);
166 $actobj = construct_activity($item);
168 if($item['parent'] == $item['id']) {
169 $atom .= replace_macros($item_template, array(
170 '$name' => xmlify($item['name']),
171 '$profile_page' => xmlify($item['url']),
172 '$thumb' => xmlify($item['thumb']),
173 '$owner_name' => xmlify($item['owner-name']),
174 '$owner_profile_page' => xmlify($item['owner-link']),
175 '$owner_thumb' => xmlify($item['owner-avatar']),
176 '$item_id' => xmlify($item['uri']),
177 '$title' => xmlify($item['title']),
178 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
179 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
180 '$location' => xmlify($item['location']),
182 '$content' => xmlify($item['body']),
183 '$verb' => xmlify($verb),
184 '$actobj' => $actobj, // do not xmlify
185 '$comment_allow' => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
189 $atom .= replace_macros($cmnt_template, array(
190 '$name' => xmlify($item['name']),
191 '$profile_page' => xmlify($item['url']),
192 '$thumb' => xmlify($item['thumb']),
193 '$item_id' => xmlify($item['uri']),
194 '$title' => xmlify($item['title']),
195 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
196 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
198 '$content' => xmlify($item['body']),
199 '$verb' => xmlify($verb),
200 '$actobj' => $actobj, // do not xmlify
201 '$parent_id' => xmlify($item['parent-uri']),
202 '$comment_allow' => (($item['last-child']) ? 1 : 0)
208 $atom .= '</feed>' . "\r\n";
213 function construct_verb($item) {
215 return $item['verb'];
216 return ACTIVITY_POST;
219 function construct_activity($item) {
221 if($item['object']) {
222 $o = '<as:object>' . "\r\n";
223 $r = @simplexml_load_string($item['object']);
225 $o .= '<as:object-type>' . $r->type . '</as:object-type>' . "\r\n";
227 $o .= '<id>' . $r->id . '</id>' . "\r\n";
229 $o .= '<link rel="alternate" type="text/html" href="' . $r->link . '" />' . "\r\n";
231 $o .= '<title>' . $r->title . '</title>' . "\r\n";
233 $o .= '<content type="html" >' . bbcode($r->content) . '</content>' . "\r\n";
234 $o .= '</as:object>' . "\r\n";
244 function get_atom_elements($item) {
246 require_once('library/HTMLPurifier.auto.php');
247 require_once('include/html2bbcode.php');
251 $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
253 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
254 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
257 $author = $item->get_author();
258 $res['author-name'] = unxmlify($author->get_name());
259 $res['author-link'] = unxmlify($author->get_link());
260 if(! $res['author-avatar'])
261 $res['author-avatar'] = unxmlify($author->get_avatar());
262 $res['uri'] = unxmlify($item->get_id());
263 $res['title'] = unxmlify($item->get_title());
264 $res['body'] = unxmlify($item->get_content());
266 $maxlen = get_max_import_size();
267 if($maxlen && (strlen($res['body']) > $maxlen))
268 $res['body'] = substr($res['body'],0, $maxlen);
270 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
271 // the content type. Our own network only emits text normally, though it might have been converted to
272 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
273 // have to assume it is all html and needs to be purified.
275 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
276 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
277 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
281 if(strpos($res['body'],'<')) {
283 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
284 '[youtube]$1[/youtube]', $res['body']);
286 $config = HTMLPurifier_Config::createDefault();
287 $config->set('Core.DefinitionCache', null);
289 // we shouldn't need a whitelist, because the bbcode converter
290 // will strip out any unsupported tags.
291 // $config->set('HTML.Allowed', 'p,b,a[href],i');
293 $purifier = new HTMLPurifier($config);
294 $res['body'] = $purifier->purify($res['body']);
298 $res['body'] = html2bbcode($res['body']);
301 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
302 if($allow && $allow[0]['data'] == 1)
303 $res['last-child'] = 1;
305 $res['last-child'] = 0;
307 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
309 $res['created'] = unxmlify($rawcreated[0]['data']);
311 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
313 $res['location'] = unxmlify($rawlocation[0]['data']);
316 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
318 $res['edited'] = unxmlify($rawcreated[0]['data']);
320 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
321 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
322 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
323 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
324 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
325 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
326 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
327 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
328 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
330 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
331 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
332 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
333 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
335 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
336 // select between supported verbs
338 $res['verb'] = unxmlify($rawverb[0]['data']);
340 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
344 $res['object'] = '<object>' . "\n";
345 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
346 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
347 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
349 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
350 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
352 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'alternate')
353 $res['object'] .= '<link>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href'] . '</link>' . "\n";
354 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
355 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
356 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
357 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
359 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
360 if(strpos($body,'<')) {
362 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
363 '[youtube]$1[/youtube]', $body);
365 $config = HTMLPurifier_Config::createDefault();
366 $config->set('Core.DefinitionCache', null);
368 $purifier = new HTMLPurifier($config);
369 $body = $purifier->purify($body);
372 $body = html2bbcode($body);
373 $res['object'] .= '<content>' . $body . '</content>' . "\n";
376 $res['object'] .= '</object>' . "\n";
382 function item_store($arr) {
388 $arr['gravity'] = intval($arr['gravity']);
389 elseif($arr['parent-uri'] == $arr['uri'])
391 elseif($arr['verb'] == ACTIVITY_POST)
395 $arr['type'] = 'remote';
396 $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
397 $arr['uri'] = notags(trim($arr['uri']));
398 $arr['author-name'] = notags(trim($arr['author-name']));
399 $arr['author-link'] = notags(trim($arr['author-link']));
400 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
401 $arr['owner-name'] = notags(trim($arr['owner-name']));
402 $arr['owner-link'] = notags(trim($arr['owner-link']));
403 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
404 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
405 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
406 $arr['changed'] = datetime_convert();
407 $arr['title'] = notags(trim($arr['title']));
408 $arr['location'] = notags(trim($arr['location']));
409 $arr['body'] = escape_tags(trim($arr['body']));
410 $arr['last-child'] = intval($arr['last-child']);
411 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1);
413 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
414 $arr['verb'] = notags(trim($arr['verb']));
415 $arr['object-type'] = notags(trim($arr['object-type']));
416 $arr['object'] = trim($arr['object']);
419 $parent_missing = false;
423 $r = q("INSERT INTO `item` (`"
424 . implode("`, `", array_keys($arr))
426 . implode("', '", array_values($arr))
429 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
430 dbesc($arr['parent-uri']),
435 $parent_id = $r[0]['id'];
437 $parent_missing = true;
440 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
441 $arr['uri'], // already dbesc'd
445 $current_post = $r[0]['id'];
449 if($parent_missing) {
451 // perhaps the parent was deleted, but in any case, this thread is dead
452 // and unfortunately our brand new item now has to be destroyed
454 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
455 intval($current_post)
460 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
462 intval($current_post)
465 return $current_post;