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' LIMIT 1",
44 $groups = init_groups_visitor($contact['id']);
47 for($x = 0; $x < count($groups); $x ++)
48 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
49 $gs = implode('|', $groups);
52 $gs = '<<>>' ; // Impossible to match
54 $sql_extra = sprintf("
55 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
56 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
57 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
58 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
60 intval($contact['id']),
61 intval($contact['id']),
67 if(! strlen($last_update))
68 $last_update = 'now - 30 days';
69 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
71 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
72 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
73 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
74 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
75 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
76 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
77 WHERE `item`.`uid` = %d AND `item`.`visible` = 1
78 AND NOT `item`.`type` IN ( 'remote', 'net-comment' ) AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
79 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
81 ORDER BY `parent` ASC, `created` ASC LIMIT 0, 300",
91 $feed_template = file_get_contents('view/atom_feed.tpl');
92 $tomb_template = file_get_contents('view/atom_tomb.tpl');
93 $item_template = file_get_contents('view/atom_item.tpl');
94 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
99 $atom .= replace_macros($feed_template, array(
100 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
101 '$feed_title' => xmlify($owner['name']),
102 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
103 '$name' => xmlify($owner['name']),
104 '$profile_page' => xmlify($owner['url']),
105 '$photo' => xmlify($owner['photo']),
106 '$thumb' => xmlify($owner['thumb']),
107 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
108 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
109 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME))
113 foreach($items as $item) {
115 // public feeds get html, our own nodes use bbcode
117 if($dfrn_id == '*') {
118 $item['body'] = bbcode($item['body']);
125 if($item['deleted']) {
126 $atom .= replace_macros($tomb_template, array(
127 '$id' => xmlify($item['uri']),
128 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
132 $verb = construct_verb($item);
133 $actobj = construct_activity($item);
135 if($item['parent'] == $item['id']) {
136 $atom .= replace_macros($item_template, array(
137 '$name' => xmlify($item['name']),
138 '$profile_page' => xmlify($item['url']),
139 '$thumb' => xmlify($item['thumb']),
140 '$owner_name' => xmlify($item['owner-name']),
141 '$owner_profile_page' => xmlify($item['owner-link']),
142 '$owner_thumb' => xmlify($item['owner-avatar']),
143 '$item_id' => xmlify($item['uri']),
144 '$title' => xmlify($item['title']),
145 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
146 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
147 '$location' => xmlify($item['location']),
149 '$content' => xmlify($item['body']),
150 '$verb' => xmlify($verb),
151 '$actobj' => $actobj, // do not xmlify
152 '$comment_allow' => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
156 $atom .= replace_macros($cmnt_template, array(
157 '$name' => xmlify($item['name']),
158 '$profile_page' => xmlify($item['url']),
159 '$thumb' => xmlify($item['thumb']),
160 '$item_id' => xmlify($item['uri']),
161 '$title' => xmlify($item['title']),
162 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
163 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
165 '$content' =>xmlify($item['body']),
166 '$verb' => xmlify($verb),
167 '$actobj' => $actobj, // do not xmlify
168 '$parent_id' => xmlify($item['parent-uri']),
169 '$comment_allow' => (($item['last-child']) ? 1 : 0)
175 $atom .= '</feed>' . "\r\n";
180 function construct_verb($item) {
182 return $item['verb'];
183 return ACTIVITY_POST;
186 function construct_activity($item) {
188 if($item['type'] == 'activity') {
198 function get_atom_elements($item) {
200 require_once('library/HTMLPurifier.auto.php');
201 require_once('include/html2bbcode.php');
205 $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
207 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
208 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
211 $author = $item->get_author();
212 $res['author-name'] = unxmlify($author->get_name());
213 $res['author-link'] = unxmlify($author->get_link());
214 if(! $res['author-avatar'])
215 $res['author-avatar'] = unxmlify($author->get_avatar());
216 $res['uri'] = unxmlify($item->get_id());
217 $res['title'] = unxmlify($item->get_title());
218 $res['body'] = unxmlify($item->get_content());
220 $maxlen = get_max_import_size();
221 if($maxlen && (strlen($res['body']) > $maxlen))
222 $res['body'] = substr($res['body'],0, $maxlen);
224 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
225 // the content type. Our own network only emits text normally, though it might have been converted to
226 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
227 // have to assume it is all html and needs to be purified.
229 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
230 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
231 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
235 if(strpos($res['body'],'<')) {
237 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
238 '[youtube]$1[/youtube]', $res['body']);
240 $config = HTMLPurifier_Config::createDefault();
241 $config->set('Core.DefinitionCache', null);
243 // we shouldn't need a whitelist, because the bbcode converter
244 // will strip out any unsupported tags.
245 // $config->set('HTML.Allowed', 'p,b,a[href],i');
247 $purifier = new HTMLPurifier($config);
248 $res['body'] = $purifier->purify($res['body']);
252 $res['body'] = html2bbcode($res['body']);
255 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
256 if($allow && $allow[0]['data'] == 1)
257 $res['last-child'] = 1;
259 $res['last-child'] = 0;
261 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
263 $res['created'] = unxmlify($rawcreated[0]['data']);
265 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
267 $res['location'] = unxmlify($rawlocation[0]['data']);
270 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
272 $res['edited'] = unxmlify($rawcreated[0]['data']);
274 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
275 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
276 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
277 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
278 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
279 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
280 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
281 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
282 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
284 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
285 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
286 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
287 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
289 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
290 // select between supported verbs
292 $res['verb'] = unxmlify($rawverb[0]['data']);
294 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
296 $res['object-type'] = $rawobj[0]['object-type'][0]['data'];
297 $res['object'] = $rawobj[0];
303 function post_remote($a,$arr) {
308 $arr['type'] = 'remote';
309 $arr['uri'] = notags(trim($arr['uri']));
310 $arr['author-name'] = notags(trim($arr['author-name']));
311 $arr['author-link'] = notags(trim($arr['author-link']));
312 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
313 $arr['owner-name'] = notags(trim($arr['owner-name']));
314 $arr['owner-link'] = notags(trim($arr['owner-link']));
315 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
316 $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
317 $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
318 $arr['changed'] = datetime_convert();
319 $arr['title'] = notags(trim($arr['title']));
320 $arr['location'] = notags(trim($arr['location']));
321 $arr['body'] = escape_tags(trim($arr['body']));
322 $arr['last-child'] = intval($arr['last-child']);
325 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
326 $arr['verb'] = notags(trim($arr['verb']));
327 $arr['object-type'] = notags(trim($arr['object-type']));
328 $arr['object'] = trim($arr['object']);
331 $parent_missing = false;
335 $r = q("INSERT INTO `item` (`"
336 . implode("`, `", array_keys($arr))
338 . implode("', '", array_values($arr))
341 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
342 dbesc($arr['parent-uri']),
347 $parent_id = $r[0]['id'];
349 $parent_missing = true;
352 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
353 $arr['uri'], // already dbesc'd
357 $current_post = $r[0]['id'];
361 if($parent_missing) {
363 // perhaps the parent was deleted, but in any case, this thread is dead
364 // and unfortunately our brand new item now has to be destroyed
366 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
367 intval($current_post)
372 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
374 intval($current_post)
377 return $current_post;