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 = load_view_file('view/atom_feed.tpl');
120 $tomb_template = load_view_file('view/atom_tomb.tpl');
121 $item_template = load_view_file('view/atom_item.tpl');
122 $cmnt_template = load_view_file('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 '$alt' => xmlify($a->get_baseurl() . '/display/' . $owner_nick . '/' . $item['id']),
183 '$content' => xmlify($item['body']),
184 '$verb' => xmlify($verb),
185 '$actobj' => $actobj, // do not xmlify
186 '$comment_allow' => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
190 $atom .= replace_macros($cmnt_template, array(
191 '$name' => xmlify($item['name']),
192 '$profile_page' => xmlify($item['url']),
193 '$thumb' => xmlify($item['thumb']),
194 '$item_id' => xmlify($item['uri']),
195 '$title' => xmlify($item['title']),
196 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
197 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
199 '$content' => xmlify($item['body']),
200 '$alt' => xmlify($a->get_baseurl() . '/display/' . $owner_nick . '/' . $item['id']),
201 '$verb' => xmlify($verb),
202 '$actobj' => $actobj, // do not xmlify
203 '$parent_id' => xmlify($item['parent-uri']),
204 '$comment_allow' => (($item['last-child']) ? 1 : 0)
210 $atom .= '</feed>' . "\r\n";
215 function construct_verb($item) {
217 return $item['verb'];
218 return ACTIVITY_POST;
221 function construct_activity($item) {
223 if($item['object']) {
224 $o = '<as:object>' . "\r\n";
225 $r = @simplexml_load_string($item['object']);
227 $o .= '<as:object-type>' . $r->type . '</as:object-type>' . "\r\n";
229 $o .= '<id>' . $r->id . '</id>' . "\r\n";
231 $o .= '<link rel="alternate" type="text/html" href="' . $r->link . '" />' . "\r\n";
233 $o .= '<title>' . $r->title . '</title>' . "\r\n";
235 $o .= '<content type="html" >' . bbcode($r->content) . '</content>' . "\r\n";
236 $o .= '</as:object>' . "\r\n";
246 function get_atom_elements($item) {
248 require_once('library/HTMLPurifier.auto.php');
249 require_once('include/html2bbcode.php');
253 $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
255 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] === 'photo')
256 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
259 $author = $item->get_author();
260 $res['author-name'] = unxmlify($author->get_name());
261 $res['author-link'] = unxmlify($author->get_link());
262 if(! $res['author-avatar'])
263 $res['author-avatar'] = unxmlify($author->get_avatar());
264 $res['uri'] = unxmlify($item->get_id());
265 $res['title'] = unxmlify($item->get_title());
266 $res['body'] = unxmlify($item->get_content());
268 $maxlen = get_max_import_size();
269 if($maxlen && (strlen($res['body']) > $maxlen))
270 $res['body'] = substr($res['body'],0, $maxlen);
272 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
273 // the content type. Our own network only emits text normally, though it might have been converted to
274 // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
275 // have to assume it is all html and needs to be purified.
277 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
278 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
279 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
283 if(strpos($res['body'],'<')) {
285 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
286 '[youtube]$1[/youtube]', $res['body']);
288 $config = HTMLPurifier_Config::createDefault();
289 $config->set('Core.DefinitionCache', null);
291 // we shouldn't need a whitelist, because the bbcode converter
292 // will strip out any unsupported tags.
293 // $config->set('HTML.Allowed', 'p,b,a[href],i');
295 $purifier = new HTMLPurifier($config);
296 $res['body'] = $purifier->purify($res['body']);
300 $res['body'] = html2bbcode($res['body']);
303 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
304 if($allow && $allow[0]['data'] == 1)
305 $res['last-child'] = 1;
307 $res['last-child'] = 0;
309 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
311 $res['created'] = unxmlify($rawcreated[0]['data']);
313 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
315 $res['location'] = unxmlify($rawlocation[0]['data']);
318 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
320 $res['edited'] = unxmlify($rawcreated[0]['data']);
322 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
323 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
324 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
325 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
326 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
327 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
328 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
329 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
330 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
332 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] === 'photo')
333 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
334 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
335 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
337 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
338 // select between supported verbs
340 $res['verb'] = unxmlify($rawverb[0]['data']);
342 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
346 $res['object'] = '<object>' . "\n";
347 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
348 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
349 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
351 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
352 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
354 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] === 'alternate')
355 $res['object'] .= '<link>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href'] . '</link>' . "\n";
356 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
357 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
358 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
359 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
361 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
362 if(strpos($body,'<')) {
364 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
365 '[youtube]$1[/youtube]', $body);
367 $config = HTMLPurifier_Config::createDefault();
368 $config->set('Core.DefinitionCache', null);
370 $purifier = new HTMLPurifier($config);
371 $body = $purifier->purify($body);
374 $body = html2bbcode($body);
375 $res['object'] .= '<content>' . $body . '</content>' . "\n";
378 $res['object'] .= '</object>' . "\n";
384 function item_store($arr) {
390 $arr['gravity'] = intval($arr['gravity']);
391 elseif($arr['parent-uri'] == $arr['uri'])
393 elseif($arr['verb'] == ACTIVITY_POST)
397 $arr['type'] = 'remote';
398 $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
399 $arr['uri'] = notags(trim($arr['uri']));
400 $arr['author-name'] = notags(trim($arr['author-name']));
401 $arr['author-link'] = notags(trim($arr['author-link']));
402 $arr['author-avatar'] = notags(trim($arr['author-avatar']));
403 $arr['owner-name'] = notags(trim($arr['owner-name']));
404 $arr['owner-link'] = notags(trim($arr['owner-link']));
405 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
406 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
407 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
408 $arr['changed'] = datetime_convert();
409 $arr['title'] = notags(trim($arr['title']));
410 $arr['location'] = notags(trim($arr['location']));
411 $arr['body'] = escape_tags(trim($arr['body']));
412 $arr['last-child'] = intval($arr['last-child']);
413 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1);
415 $arr['parent-uri'] = notags(trim($arr['parent-uri']));
416 $arr['verb'] = notags(trim($arr['verb']));
417 $arr['object-type'] = notags(trim($arr['object-type']));
418 $arr['object'] = trim($arr['object']);
421 $parent_missing = false;
425 $r = q("INSERT INTO `item` (`"
426 . implode("`, `", array_keys($arr))
428 . implode("', '", array_values($arr))
431 // find the parent and snarf the item id and ACL's
433 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
434 dbesc($arr['parent-uri']),
439 $parent_id = $r[0]['id'];
440 $allow_cid = $r[0]['allow_cid'];
441 $allow_gid = $r[0]['allow_gid'];
442 $deny_cid = $r[0]['deny_cid'];
443 $deny_gid = $r[0]['deny_gid'];
446 $parent_missing = true;
449 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
450 $arr['uri'], // already dbesc'd
454 $current_post = $r[0]['id'];
458 if($parent_missing) {
460 // perhaps the parent was deleted, but in any case, this thread is dead
461 // and unfortunately our brand new item now has to be destroyed
463 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
464 intval($current_post)
469 // Set parent id - all of the parent's ACL's are also inherited by this post
471 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
472 `deny_cid` = '%s', `deny_gid` = '%s' WHERE `id` = %d LIMIT 1",
478 intval($current_post)
481 return $current_post;
484 function get_item_contact($item,$contacts) {
485 if(! count($contacts) || (! is_array($item)))
487 foreach($contacts as $contact) {
488 if($contact['id'] == $item['contact-id']) {
497 function dfrn_deliver($contact,$atom,$debugging = false) {
500 if((! strlen($contact['dfrn-id'])) && (! $contact['duplex']))
503 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
505 if($contact['duplex'] && $contact['dfrn-id'])
506 $idtosend = '0:' . $orig_id;
507 if($contact['duplex'] && $contact['issued-id'])
508 $idtosend = '1:' . $orig_id;
510 $url = $contact['notify'] . '?dfrn_id=' . $idtosend;
515 $xml = fetch_url($url);
523 $res = simplexml_load_string($xml);
525 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
526 return (($res->status) ? $res->status : 3);
529 $sent_dfrn_id = hex2bin($res->dfrn_id);
530 $challenge = hex2bin($res->challenge);
534 if($contact['duplex'] && strlen($contact['prvkey'])) {
535 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
536 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
539 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
540 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
543 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
545 if(strpos($final_dfrn_id,':') == 1)
546 $final_dfrn_id = substr($final_dfrn_id,2);
548 if($final_dfrn_id != $orig_id) {
549 // did not decode properly - cannot trust this site
553 $postvars['dfrn_id'] = $idtosend;
556 if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
557 $postvars['data'] = $atom;
560 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
563 $xml = post_url($contact['notify'],$postvars);
568 $res = simplexml_load_string($xml);