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';
94 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
96 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
97 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
98 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
99 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
100 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
101 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
102 WHERE `item`.`uid` = %d AND `item`.`visible` = 1
103 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
104 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
106 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
117 $feed_template = file_get_contents('view/atom_feed.tpl');
118 $tomb_template = file_get_contents('view/atom_tomb.tpl');
119 $item_template = file_get_contents('view/atom_item.tpl');
120 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
125 $atom .= replace_macros($feed_template, array(
126 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
127 '$feed_title' => xmlify($owner['name']),
128 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
129 '$name' => xmlify($owner['name']),
130 '$profile_page' => xmlify($owner['url']),
131 '$photo' => xmlify($owner['photo']),
132 '$thumb' => xmlify($owner['thumb']),
133 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
134 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
135 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME))
139 foreach($items as $item) {
141 // public feeds get html, our own nodes use bbcode
143 if($dfrn_id == '*') {
144 $item['body'] = bbcode($item['body']);
151 if($item['deleted']) {
152 $atom .= replace_macros($tomb_template, array(
153 '$id' => xmlify($item['uri']),
154 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
158 $verb = construct_verb($item);
159 $actobj = construct_activity($item);
161 if($item['parent'] == $item['id']) {
162 $atom .= replace_macros($item_template, array(
163 '$name' => xmlify($item['name']),
164 '$profile_page' => xmlify($item['url']),
165 '$thumb' => xmlify($item['thumb']),
166 '$owner_name' => xmlify($item['owner-name']),
167 '$owner_profile_page' => xmlify($item['owner-link']),
168 '$owner_thumb' => xmlify($item['owner-avatar']),
169 '$item_id' => xmlify($item['uri']),
170 '$title' => xmlify($item['title']),
171 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
172 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
173 '$location' => xmlify($item['location']),
175 '$content' => xmlify($item['body']),
176 '$verb' => xmlify($verb),
177 '$actobj' => $actobj, // do not xmlify
178 '$comment_allow' => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
182 $atom .= replace_macros($cmnt_template, array(
183 '$name' => xmlify($item['name']),
184 '$profile_page' => xmlify($item['url']),
185 '$thumb' => xmlify($item['thumb']),
186 '$item_id' => xmlify($item['uri']),
187 '$title' => xmlify($item['title']),
188 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
189 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
191 '$content' => xmlify($item['body']),
192 '$verb' => xmlify($verb),
193 '$actobj' => $actobj, // do not xmlify
194 '$parent_id' => xmlify($item['parent-uri']),
195 '$comment_allow' => (($item['last-child']) ? 1 : 0)
201 $atom .= '</feed>' . "\r\n";
206 function construct_verb($item) {
208 return $item['verb'];
209 return ACTIVITY_POST;
212 function construct_activity($item) {
214 if($item['object']) {
215 $o = '<as:object>' . "\r\n";
216 $r = @simplexml_load_string($item['object']);
218 $o .= '<as:object-type>' . $r->type . '</as:object-type>' . "\r\n";
220 $o .= '<id>' . $r->id . '</id>' . "\r\n";
222 $o .= '<link rel="alternate" type="text/html" href="' . $r->link . '" />' . "\r\n";
224 $o .= '<title>' . $r->title . '</title>' . "\r\n";
226 $o .= '<content type="html" >' . bbcode($r->content) . '</content>' . "\r\n";
227 $o .= '</as:object>' . "\r\n";
237 function get_atom_elements($item) {
239 require_once('library/HTMLPurifier.auto.php');
240 require_once('include/html2bbcode.php');
244 $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
246 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
247 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
250 $author = $item->get_author();
251 $res['author-name'] = unxmlify($author->get_name());
252 $res['author-link'] = unxmlify($author->get_link());
253 if(! $res['author-avatar'])
254 $res['author-avatar'] = unxmlify($author->get_avatar());
255 $res['uri'] = unxmlify($item->get_id());
256 $res['title'] = unxmlify($item->get_title());
257 $res['body'] = unxmlify($item->get_content());
259 $maxlen = get_max_import_size();
260 if($maxlen && (strlen($res['body']) > $maxlen))
261 $res['body'] = substr($res['body'],0, $maxlen);
263 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
264 // the content type. Our own network only emits text normally, though it might have been converted to
265 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
266 // have to assume it is all html and needs to be purified.
268 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
269 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
270 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
274 if(strpos($res['body'],'<')) {
276 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
277 '[youtube]$1[/youtube]', $res['body']);
279 $config = HTMLPurifier_Config::createDefault();
280 $config->set('Core.DefinitionCache', null);
282 // we shouldn't need a whitelist, because the bbcode converter
283 // will strip out any unsupported tags.
284 // $config->set('HTML.Allowed', 'p,b,a[href],i');
286 $purifier = new HTMLPurifier($config);
287 $res['body'] = $purifier->purify($res['body']);
291 $res['body'] = html2bbcode($res['body']);
294 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
295 if($allow && $allow[0]['data'] == 1)
296 $res['last-child'] = 1;
298 $res['last-child'] = 0;
300 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
302 $res['created'] = unxmlify($rawcreated[0]['data']);
304 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
306 $res['location'] = unxmlify($rawlocation[0]['data']);
309 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
311 $res['edited'] = unxmlify($rawcreated[0]['data']);
313 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
314 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
315 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
316 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
317 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
318 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
319 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
320 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
321 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
323 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
324 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
325 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
326 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
328 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
329 // select between supported verbs
331 $res['verb'] = unxmlify($rawverb[0]['data']);
333 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
337 $res['object'] = '<object>' . "\n";
338 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
339 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
340 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
342 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
343 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
345 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'alternate')
346 $res['object'] .= '<link>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href'] . '</link>' . "\n";
347 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
348 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
349 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
350 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
352 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
353 if(strpos($body,'<')) {
355 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
356 '[youtube]$1[/youtube]', $body);
358 $config = HTMLPurifier_Config::createDefault();
359 $config->set('Core.DefinitionCache', null);
361 $purifier = new HTMLPurifier($config);
362 $body = $purifier->purify($body);
365 $body = html2bbcode($body);
366 $res['object'] .= '<content>' . $body . '</content>' . "\n";
369 $res['object'] .= '</object>' . "\n";
375 function item_store($arr) {
381 $arr['gravity'] = intval($arr['gravity']);
382 elseif($arr['parent-uri'] == $arr['uri'])
384 elseif($arr['verb'] == ACTIVITY_POST)
388 $arr['type'] = 'remote';
389 $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
390 $arr['uri'] = notags(trim($arr['uri']));
391 $arr['author-name'] = notags(trim($arr['author-name']));
392 $arr['author-link'] = notags(trim($arr['author-link']));
393 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
394 $arr['owner-name'] = notags(trim($arr['owner-name']));
395 $arr['owner-link'] = notags(trim($arr['owner-link']));
396 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
397 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
398 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
399 $arr['changed'] = datetime_convert();
400 $arr['title'] = notags(trim($arr['title']));
401 $arr['location'] = notags(trim($arr['location']));
402 $arr['body'] = escape_tags(trim($arr['body']));
403 $arr['last-child'] = intval($arr['last-child']);
404 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1);
406 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
407 $arr['verb'] = notags(trim($arr['verb']));
408 $arr['object-type'] = notags(trim($arr['object-type']));
409 $arr['object'] = trim($arr['object']);
412 $parent_missing = false;
416 $r = q("INSERT INTO `item` (`"
417 . implode("`, `", array_keys($arr))
419 . implode("', '", array_values($arr))
422 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
423 dbesc($arr['parent-uri']),
428 $parent_id = $r[0]['id'];
430 $parent_missing = true;
433 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
434 $arr['uri'], // already dbesc'd
438 $current_post = $r[0]['id'];
442 if($parent_missing) {
444 // perhaps the parent was deleted, but in any case, this thread is dead
445 // and unfortunately our brand new item now has to be destroyed
447 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
448 intval($current_post)
453 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
455 intval($current_post)
458 return $current_post;