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' , 'Y-m-d\TH:i:s\Z')) ,
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' , 'Y-m-d\TH:i:s\Z')) ,
108 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
109 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
114 foreach($items as $item) {
116 // public feeds get html, our own nodes use bbcode
118 if($dfrn_id == '*') {
119 $item['body'] = bbcode($item['body']);
126 if($item['deleted']) {
127 $atom .= replace_macros($tomb_template, array(
128 '$id' => xmlify($item['uri']),
129 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
133 $verb = construct_verb($item);
134 $actobj = construct_activity($item);
136 if($item['parent'] == $item['id']) {
137 $atom .= replace_macros($item_template, array(
138 '$name' => xmlify($item['name']),
139 '$profile_page' => xmlify($item['url']),
140 '$thumb' => xmlify($item['thumb']),
141 '$owner_name' => xmlify($item['owner-name']),
142 '$owner_profile_page' => xmlify($item['owner-link']),
143 '$owner_thumb' => xmlify($item['owner-avatar']),
144 '$item_id' => xmlify($item['uri']),
145 '$title' => xmlify($item['title']),
146 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
147 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
148 '$location' => xmlify($item['location']),
150 '$content' => xmlify($item['body']),
151 '$verb' => xmlify($verb),
152 '$actobj' => $actobj, // do not xmlify
153 '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0)
157 $atom .= replace_macros($cmnt_template, array(
158 '$name' => xmlify($item['name']),
159 '$profile_page' => xmlify($item['url']),
160 '$thumb' => xmlify($item['thumb']),
161 '$item_id' => xmlify($item['uri']),
162 '$title' => xmlify($item['title']),
163 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
164 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
166 '$content' =>xmlify($item['body']),
167 '$verb' => xmlify($verb),
168 '$actobj' => $actobj, // do not xmlify
169 '$parent_id' => xmlify($item['parent-uri']),
170 '$comment_allow' => (($item['last-child']) ? 1 : 0)
176 $atom .= '</feed>' . "\r\n";
181 function construct_verb($item) {
183 return $item['verb'];
184 return ACTIVITY_POST;
187 function construct_activity($item) {
189 if($item['type'] == 'activity') {
199 function get_atom_elements($item) {
201 require_once('library/HTMLPurifier.auto.php');
202 require_once('include/html2bbcode.php');
206 $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
208 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
209 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
212 $author = $item->get_author();
213 $res['author-name'] = unxmlify($author->get_name());
214 $res['author-link'] = unxmlify($author->get_link());
215 if(! $res['author-avatar'])
216 $res['author-avatar'] = unxmlify($author->get_avatar());
217 $res['uri'] = unxmlify($item->get_id());
218 $res['title'] = unxmlify($item->get_title());
219 $res['body'] = unxmlify($item->get_content());
221 $maxlen = get_max_import_size();
222 if($maxlen && (strlen($res['body']) > $maxlen))
223 $res['body'] = substr($res['body'],0, $maxlen);
225 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
226 // the content type. Our own network only emits text normally, though it might have been converted to
227 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
228 // have to assume it is all html and needs to be purified.
230 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
231 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
232 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
236 echo "test1" . $res['body'];
237 if(strpos($res['body'],'<')) {
239 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
240 '[youtube]$1[/youtube]', $res['body']);
242 $config = HTMLPurifier_Config::createDefault();
243 $config->set('Core.DefinitionCache', null);
245 // we shouldn't need a whitelist, because the bbcode converter
246 // will strip out any unsupported tags.
247 // $config->set('HTML.Allowed', 'p,b,a[href],i');
249 $purifier = new HTMLPurifier($config);
250 $res['body'] = $purifier->purify($res['body']);
252 echo "test2" . $res['body'];
254 $res['body'] = html2bbcode($res['body']);
255 echo "test3" . $res['body'];
257 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
258 if($allow && $allow[0]['data'] == 1)
259 $res['last-child'] = 1;
261 $res['last-child'] = 0;
263 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
265 $res['created'] = unxmlify($rawcreated[0]['data']);
267 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
269 $res['location'] = unxmlify($rawlocation[0]['data']);
272 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
274 $res['edited'] = unxmlify($rawcreated[0]['data']);
276 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
277 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
278 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
279 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
280 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
281 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
282 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
283 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
284 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
286 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
287 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
288 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
289 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
291 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
292 // select between supported verbs
294 $res['verb'] = unxmlify($rawverb[0]['data']);
296 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
298 $res['object-type'] = $rawobj[0]['object-type'][0]['data'];
299 $res['object'] = $rawobj[0];
305 function post_remote($a,$arr) {
310 $arr['type'] = 'remote';
311 $arr['uri'] = notags(trim($arr['uri']));
312 $arr['author-name'] = notags(trim($arr['author-name']));
313 $arr['author-link'] = notags(trim($arr['author-link']));
314 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
315 $arr['owner-name'] = notags(trim($arr['owner-name']));
316 $arr['owner-link'] = notags(trim($arr['owner-link']));
317 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
318 $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
319 $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
320 $arr['changed'] = datetime_convert();
321 $arr['title'] = notags(trim($arr['title']));
322 $arr['location'] = notags(trim($arr['location']));
323 $arr['body'] = escape_tags(trim($arr['body']));
324 $arr['last-child'] = intval($arr['last-child']);
327 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
328 $arr['verb'] = notags(trim($arr['verb']));
329 $arr['object-type'] = notags(trim($arr['object-type']));
330 $arr['object'] = trim($arr['object']);
333 $parent_missing = false;
337 $r = q("INSERT INTO `item` (`"
338 . implode("`, `", array_keys($arr))
340 . implode("', '", array_values($arr))
343 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
344 dbesc($arr['parent-uri']),
349 $parent_id = $r[0]['id'];
351 $parent_missing = true;
354 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
355 $arr['uri'], // already dbesc'd
359 $current_post = $r[0]['id'];
363 if($parent_missing) {
365 // perhaps the parent was deleted, but in any case, this thread is dead
366 // and unfortunately our brand new item now has to be destroyed
368 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
369 intval($current_post)
374 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
376 intval($current_post)
379 return $current_post;