2 require_once("bbcode.php");
3 require_once("datetime.php");
13 function api_date($str){
14 //Wed May 23 06:01:13 +0000 2007
15 return datetime_convert('UTC', 'UTC', $str, "D M d H:i:s +0000 Y" );
19 function api_register_func($path, $func, $auth=false){
21 $API[$path] = array('func'=>$func,
28 function api_login(&$a){
29 // workaround for HTTP-auth in CGI mode
30 if(x($_SERVER,'REDIRECT_REMOTE_USER')) {
31 $userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"],6)) ;
32 if(strlen($userpass)) {
33 list($name, $password) = explode(':', $userpass);
34 $_SERVER['PHP_AUTH_USER'] = $name;
35 $_SERVER['PHP_AUTH_PW'] = $password;
39 if (!isset($_SERVER['PHP_AUTH_USER'])) {
40 logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG);
41 header('WWW-Authenticate: Basic realm="Friendika"');
42 header('HTTP/1.0 401 Unauthorized');
43 die('This api requires login');
46 $user = $_SERVER['PHP_AUTH_USER'];
47 $encrypted = hash('whirlpool',trim($_SERVER['PHP_AUTH_PW']));
51 * next code from mod/auth.php. needs better solution
54 // process normal login request
56 $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
57 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
65 logger('API_login failure: ' . print_r($_SERVER,true), LOGGER_DEBUG);
66 header('WWW-Authenticate: Basic realm="Friendika"');
67 header('HTTP/1.0 401 Unauthorized');
68 die('This api requires login');
70 $_SESSION['uid'] = $record['uid'];
71 $_SESSION['theme'] = $record['theme'];
72 $_SESSION['authenticated'] = 1;
73 $_SESSION['page_flags'] = $record['page-flags'];
74 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
75 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
77 //notice( t("Welcome back ") . $record['username'] . EOL);
80 if(strlen($a->user['timezone'])) {
81 date_default_timezone_set($a->user['timezone']);
82 $a->timezone = $a->user['timezone'];
85 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
86 intval($_SESSION['uid']));
89 $a->cid = $r[0]['id'];
90 $_SESSION['cid'] = $a->cid;
92 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1",
93 dbesc(datetime_convert()),
94 intval($_SESSION['uid'])
97 call_hooks('logged_in', $a->user);
99 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
102 /**************************
103 * MAIN API ENTRY POINT *
104 **************************/
105 function api_call(&$a){
107 foreach ($API as $p=>$info){
108 if (strpos($a->query_string, $p)===0){
109 #unset($_SERVER['PHP_AUTH_USER']);
110 if ($info['auth']===true && local_user()===false) {
114 load_contact_links(local_user());
116 logger('API call for ' . $a->user['username'] . ': ' . $a->query_string);
117 logger('API parameters: ' . print_r($_REQUEST,true));
119 if (strpos($a->query_string, ".xml")>0) $type="xml";
120 if (strpos($a->query_string, ".json")>0) $type="json";
121 if (strpos($a->query_string, ".rss")>0) $type="rss";
122 if (strpos($a->query_string, ".atom")>0) $type="atom";
124 $r = call_user_func($info['func'], $a, $type);
125 if ($r===false) return;
129 $r = mb_convert_encoding($r, "UTF-8",mb_detect_encoding($r));
130 header ("Content-Type: text/xml");
131 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
134 header ("Content-Type: application/json");
136 return json_encode($rr);
139 header ("Content-Type: application/rss+xml");
140 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
143 header ("Content-Type: application/atom+xml");
144 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
148 //echo "<pre>"; var_dump($r); die();
151 $r = '<status><error>not implemented</error></status>';
154 header ("Content-Type: text/xml");
155 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
158 header ("Content-Type: application/json");
159 return json_encode(array('error' => 'not implemented'));
162 header ("Content-Type: application/rss+xml");
163 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
166 header ("Content-Type: application/atom+xml");
167 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
176 function api_rss_extra(&$a, $arr, $user_info){
177 if (is_null($user_info)) $user_info = api_get_user($a);
178 $arr['$user'] = $user_info;
179 $arr['$rss'] = array(
180 'alternate' => $user_info['url'],
181 'self' => $a->get_baseurl(). "/". $a->query_string,
182 'base' => $a->get_baseurl(),
183 'updated' => api_date(null),
184 'atom_updated' => datetime_convert('UTC','UTC','now',ATOM_TIME),
185 'language' => $user_info['language'],
186 'logo' => $a->get_baseurl()."/images/friendika-32.png",
193 * Returns user info array.
195 function api_get_user(&$a, $contact_id = Null){
200 if(!is_null($contact_id)){
202 $extra_query = "AND `contact`.`id` = %d ";
205 if(is_null($user) && x($_GET, 'user_id')) {
206 $user = intval($_GET['user_id']);
207 $extra_query = "AND `contact`.`id` = %d ";
209 if(is_null($user) && x($_GET, 'screen_name')) {
210 $user = dbesc($_GET['screen_name']);
211 $extra_query = "AND `contact`.`nick` = '%s' ";
214 if (is_null($user) && $a->argc > 3){
215 list($user, $null) = explode(".",$a->argv[3]);
216 if(is_numeric($user)){
217 $user = intval($user);
218 $extra_query = "AND `contact`.`id` = %d ";
220 $user = dbesc($user);
221 $extra_query = "AND `contact`.`nick` = '%s' ";
226 if (local_user()===false) {
227 api_login($a); return False;
229 $user = $_SESSION['uid'];
230 $extra_query = "AND `contact`.`uid` = %d AND `contact`.`self` = 1 ";
235 logger('api_user: ' . $extra_query . ' ' , $user);
237 $uinfo = q("SELECT *, `contact`.`id` as `cid` FROM `contact`
242 if (count($uinfo)==0) {
246 if($uinfo[0]['self']) {
247 $usr = q("select * from user where uid = %d limit 1",
250 $profile = q("select * from profile where uid = %d and `is-default` = 1 limit 1",
254 // count public wall messages
255 $r = q("SELECT COUNT(`id`) as `count` FROM `item`
258 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''",
259 intval($uinfo[0]['uid'])
261 $countitms = $r[0]['count'];
264 $r = q("SELECT COUNT(`id`) as `count` FROM `item`
265 WHERE `contact-id` = %d
266 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''",
267 intval($uinfo[0]['id'])
269 $countitms = $r[0]['count'];
273 $r = q("SELECT COUNT(`id`) as `count` FROM `contact`
274 WHERE `uid` = %d AND `rel` IN ( %d, %d )
275 AND `self`=0 AND `blocked`=0",
276 intval($uinfo[0]['uid']),
277 intval(CONTACT_IS_SHARING),
278 intval(CONTACT_IS_FRIEND)
280 $countfriends = $r[0]['count'];
282 $r = q("SELECT COUNT(`id`) as `count` FROM `contact`
283 WHERE `uid` = %d AND `rel` IN ( %d, %d )
284 AND `self`=0 AND `blocked`=0",
285 intval($uinfo[0]['uid']),
286 intval(CONTACT_IS_FOLLOWER),
287 intval(CONTACT_IS_FRIEND)
289 $countfollowers = $r[0]['count'];
291 $r = q("SELECT count(`id`) as `count` FROM item where starred = 1 and uid = %d and deleted = 0",
292 intval($uinfo[0]['uid'])
294 $starred = $r[0]['count'];
297 if(! $uinfo[0]['self']) {
304 'uid' => intval($uinfo[0]['uid']),
305 'id' => intval($uinfo[0]['cid']),
306 'name' => $uinfo[0]['name'],
307 'screen_name' => (($uinfo[0]['nick']) ? $uinfo[0]['nick'] : $uinfo[0]['name']),
308 'location' => ($usr) ? $usr[0]['default-location'] : '',
309 'profile_image_url' => $uinfo[0]['micro'],
310 'url' => $uinfo[0]['url'],
311 'contact_url' => $a->get_baseurl()."/contacts/".$uinfo[0]['cid'],
312 'protected' => false,
313 'friends_count' => intval($countfriends),
314 'created_at' => api_date($uinfo[0]['name-date']),
315 'utc_offset' => "+00:00",
316 'time_zone' => 'UTC', //$uinfo[0]['timezone'],
317 'geo_enabled' => false,
318 'statuses_count' => intval($countitms), #XXX: fix me
319 'lang' => 'en', #XXX: fix me
320 'description' => (($profile) ? $profile[0]['pdesc'] : ''),
321 'followers_count' => intval($countfollowers),
322 'favourites_count' => intval($starred),
323 'contributors_enabled' => false,
324 'follow_request_sent' => false,
325 'profile_background_color' => 'cfe8f6',
326 'profile_text_color' => '000000',
327 'profile_link_color' => 'FF8500',
328 'profile_sidebar_fill_color' =>'AD0066',
329 'profile_sidebar_border_color' => 'AD0066',
330 'profile_background_image_url' => '',
331 'profile_background_tile' => false,
332 'profile_use_background_image' => false,
333 'notifications' => false,
334 'following' => '', #XXX: fix me
335 'verified' => true, #XXX: fix me
343 function api_item_get_user(&$a, $item) {
344 // The author is our direct contact, in a conversation with us.
345 if(link_compare($item['url'],$item['author-link'])) {
346 return api_get_user($a,$item['cid']);
349 // The author may be a contact of ours, but is replying to somebody else.
350 // Figure out if we know him/her.
351 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
352 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
353 return api_get_user($a,$a->contacts[$normalised]['id']);
355 // We don't know this person directly.
357 list($nick, $name) = array_map("trim",explode("(",$item['author-name']));
358 $name=str_replace(")","",$name);
364 'screen_name' => $nick,
365 'location' => '', //$uinfo[0]['default-location'],
366 'profile_image_url' => $item['author-avatar'],
367 'url' => $item['author-link'],
369 'protected' => false, #
370 'friends_count' => 0,
372 'utc_offset' => 0, #XXX: fix me
373 'time_zone' => '', //$uinfo[0]['timezone'],
374 'geo_enabled' => false,
375 'statuses_count' => 0,
376 'lang' => 'en', #XXX: fix me
378 'followers_count' => 0,
379 'favourites_count' => 0,
380 'contributors_enabled' => false,
381 'follow_request_sent' => false,
382 'profile_background_color' => 'cfe8f6',
383 'profile_text_color' => '000000',
384 'profile_link_color' => 'FF8500',
385 'profile_sidebar_fill_color' =>'AD0066',
386 'profile_sidebar_border_color' => 'AD0066',
387 'profile_background_image_url' => '',
388 'profile_background_tile' => false,
389 'profile_use_background_image' => false,
390 'notifications' => false,
391 'verified' => true, #XXX: fix me
392 'followers' => '', #XXX: fix me
400 * apply xmlify() to all values of array $val, recursively
402 function api_xmlify($val){
403 if (is_bool($val)) return $val?"true":"false";
404 if (is_array($val)) return array_map('api_xmlify', $val);
405 return xmlify((string) $val);
409 * load api $templatename for $type and replace $data array
411 function api_apply_template($templatename, $type, $data){
419 $data = api_xmlify($data);
420 $tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
421 $ret = replace_macros($tpl, $data);
435 * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful;
436 * returns a 401 status code and an error message if not.
437 * http://developer.twitter.com/doc/get/account/verify_credentials
439 function api_account_verify_credentials(&$a, $type){
440 if (local_user()===false) return false;
441 $user_info = api_get_user($a);
443 return api_apply_template("user", $type, array('$user' => $user_info));
446 api_register_func('api/account/verify_credentials','api_account_verify_credentials', true);
450 * get data from $_POST or $_GET
452 function requestdata($k){
453 if (isset($_POST[$k])){
456 if (isset($_GET[$k])){
461 // TODO - media uploads
462 function api_statuses_update(&$a, $type) {
463 if (local_user()===false) return false;
464 $user_info = api_get_user($a);
466 // convert $_POST array items to the form we use for web posts.
468 // logger('api_post: ' . print_r($_POST,true));
470 $_POST['body'] = urldecode(requestdata('status'));
472 $parent = requestdata('in_reply_to_status_id');
473 if(ctype_digit($parent))
474 $_POST['parent'] = $parent;
476 $_POST['parent_uri'] = $parent;
478 if(requestdata('lat') && requestdata('long'))
479 $_POST['coord'] = sprintf("%s %s",requestdata('lat'),requestdata('long'));
480 $_POST['profile_uid'] = local_user();
481 if(requestdata('parent'))
482 $_POST['type'] = 'net-comment';
484 $_POST['type'] = 'wall';
486 // set this so that the item_post() function is quiet and doesn't redirect or emit json
488 $_POST['api_source'] = true;
490 // call out normal post function
492 require_once('mod/item.php');
495 // this should output the last post (the one we just posted).
496 return api_status_show($a,$type);
498 api_register_func('api/statuses/update','api_statuses_update', true);
501 function api_status_show(&$a, $type){
502 $user_info = api_get_user($a);
503 // get last public wall message
504 $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`nick` as `reply_author`
505 FROM `item`, `contact`,
506 (SELECT `item`.`id`, `item`.`contact-id`, `contact`.`nick` FROM `item`,`contact` WHERE `contact`.`id`=`item`.`contact-id`) as `i`
507 WHERE `item`.`contact-id` = %d
508 AND `i`.`id` = `item`.`parent`
509 AND `contact`.`id`=`item`.`contact-id` AND `contact`.`self`=1
510 AND `type`!='activity'
511 AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`=''
512 ORDER BY `created` DESC
514 intval($user_info['id'])
517 if (count($lastwall)>0){
518 $lastwall = $lastwall[0];
520 $in_reply_to_status_id = '';
521 $in_reply_to_user_id = '';
522 $in_reply_to_screen_name = '';
523 if ($lastwall['parent']!=$lastwall['id']) {
524 $in_reply_to_status_id=$lastwall['parent'];
525 $in_reply_to_user_id = $lastwall['reply_uid'];
526 $in_reply_to_screen_name = $lastwall['reply_author'];
528 $status_info = array(
529 'created_at' => api_date($lastwall['created']),
530 'id' => $lastwall['contact-id'],
531 'text' => strip_tags(bbcode($lastwall['body'])),
532 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'),
533 'truncated' => false,
534 'in_reply_to_status_id' => $in_reply_to_status_id,
535 'in_reply_to_user_id' => $in_reply_to_user_id,
536 'favorited' => false,
537 'in_reply_to_screen_name' => $in_reply_to_screen_name,
539 'coordinates' => $lastwall['coord'],
540 'place' => $lastwall['location'],
543 $status_info['user'] = $user_info;
545 return api_apply_template("status", $type, array('$status' => $status_info));
554 * Returns extended information of a given user, specified by ID or screen name as per the required id parameter.
555 * The author's most recent status will be returned inline.
556 * http://developer.twitter.com/doc/get/users/show
558 function api_users_show(&$a, $type){
559 $user_info = api_get_user($a);
560 // get last public wall message
561 $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`nick` as `reply_author`
562 FROM `item`, `contact`,
563 (SELECT `item`.`id`, `item`.`contact-id`, `contact`.`nick` FROM `item`,`contact` WHERE `contact`.`id`=`item`.`contact-id`) as `i`
564 WHERE `item`.`contact-id` = %d
565 AND `i`.`id` = `item`.`parent`
566 AND `contact`.`id`=`item`.`contact-id` AND `contact`.`self`=1
567 AND `type`!='activity'
568 AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`=''
569 ORDER BY `created` DESC
571 intval($user_info['id'])
574 if (count($lastwall)>0){
575 $lastwall = $lastwall[0];
577 $in_reply_to_status_id = '';
578 $in_reply_to_user_id = '';
579 $in_reply_to_screen_name = '';
580 if ($lastwall['parent']!=$lastwall['id']) {
581 $in_reply_to_status_id=$lastwall['parent'];
582 $in_reply_to_user_id = $lastwall['reply_uid'];
583 $in_reply_to_screen_name = $lastwall['reply_author'];
585 $user_info['status'] = array(
586 'created_at' => api_date($lastwall['created']),
587 'id' => $lastwall['contact-id'],
588 'text' => strip_tags(bbcode($lastwall['body'])),
589 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'),
590 'truncated' => false,
591 'in_reply_to_status_id' => $in_reply_to_status_id,
592 'in_reply_to_user_id' => $in_reply_to_user_id,
593 'favorited' => false,
594 'in_reply_to_screen_name' => $in_reply_to_screen_name,
596 'coordinates' => $lastwall['coord'],
597 'place' => $lastwall['location'],
601 return api_apply_template("user", $type, array('$user' => $user_info));
604 api_register_func('api/users/show','api_users_show');
608 * http://developer.twitter.com/doc/get/statuses/home_timeline
610 * TODO: Optional parameters
611 * TODO: Add reply info
613 function api_statuses_home_timeline(&$a, $type){
614 if (local_user()===false) return false;
616 $user_info = api_get_user($a);
617 // get last newtork messages
620 $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
621 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
622 if ($page<0) $page=0;
623 $since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
625 $start = $page*$count;
627 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
628 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
629 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
630 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
631 FROM `item`, `contact`
632 WHERE `item`.`uid` = %d
633 AND `item`.`visible` = 1 AND `item`.`deleted` = 0
634 AND `contact`.`id` = `item`.`contact-id`
635 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
638 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
639 intval($user_info['uid']),
641 intval($start), intval($count)
644 $ret = api_format_items($r,$user_info);
647 $data = array('$statuses' => $ret);
651 $data = api_rss_extra($a, $data, $user_info);
654 return api_apply_template("timeline", $type, $data);
656 api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true);
657 api_register_func('api/statuses/friends_timeline','api_statuses_home_timeline', true);
661 function api_statuses_user_timeline(&$a, $type){
662 if (local_user()===false) return false;
664 $user_info = api_get_user($a);
665 // get last newtork messages
668 $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
669 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
670 if ($page<0) $page=0;
671 $since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
673 $start = $page*$count;
676 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
677 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
678 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
679 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
680 FROM `item`, `contact`
681 WHERE `item`.`uid` = %d
682 AND `item`.`visible` = 1 AND `item`.`deleted` = 0
683 AND `item`.`wall` = 1
684 AND `contact`.`id` = `item`.`contact-id`
685 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
688 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
689 intval($user_info['uid']),
691 intval($start), intval($count)
694 $ret = api_format_items($r,$user_info);
697 $data = array('$statuses' => $ret);
701 $data = api_rss_extra($a, $data, $user_info);
704 return api_apply_template("timeline", $type, $data);
707 api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
710 function api_favorites(&$a, $type){
711 if (local_user()===false) return false;
713 $user_info = api_get_user($a);
714 // get last newtork messages
717 $count = (x($_GET,'count')?$_GET['count']:20);
718 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
719 if ($page<0) $page=0;
721 $start = $page*$count;
723 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
724 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
725 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
726 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
727 FROM `item`, `contact`
728 WHERE `item`.`uid` = %d
729 AND `item`.`visible` = 1 AND `item`.`deleted` = 0
730 AND `item`.`starred` = 1
731 AND `contact`.`id` = `item`.`contact-id`
732 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
734 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
735 intval($user_info['uid']),
736 intval($start), intval($count)
739 $ret = api_format_items($r,$user_info);
742 $data = array('$statuses' => $ret);
746 $data = api_rss_extra($a, $data, $user_info);
749 return api_apply_template("timeline", $type, $data);
752 api_register_func('api/favorites','api_favorites', true);
755 function api_format_items($r,$user_info) {
757 //logger('api_format_items: ' . print_r($r,true));
759 //logger('api_format_items: ' . print_r($user_info,true));
764 foreach($r as $item) {
765 $status_user = (($item['cid']==$user_info['id'])?$user_info: api_item_get_user($a,$item));
767 'created_at'=> api_date($item['created']),
768 'published' => api_date($item['created']),
769 'updated' => api_date($item['edited']),
770 'id' => intval($item['id']),
771 'message_id' => $item['uri'],
772 'text' => strip_tags(bbcode($item['body'])),
773 'statusnet_html' => bbcode($item['body']),
774 'source' => (($item['app']) ? $item['app'] : 'web'),
775 'url' => ($item['plink']!=''?$item['plink']:$item['author-link']),
776 'truncated' => False,
777 'in_reply_to_status_id' => ($item['parent']!=$item['id']? intval($item['parent']):''),
778 'in_reply_to_user_id' => '',
779 'favorited' => $item['starred'] ? true : false,
780 'in_reply_to_screen_name' => '',
782 'coordinates' => $item['coord'],
783 'place' => $item['location'],
784 'contributors' => '',
787 'user' => $status_user ,
788 'objecttype' => (($item['object-type']) ? $item['object-type'] : ACTIVITY_OBJ_NOTE),
789 'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST),
790 'self' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
791 'edit' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
799 function api_account_rate_limit_status(&$a,$type) {
802 'remaining_hits' => (string) 150,
803 'hourly_limit' => (string) 150,
804 'reset_time' => datetime_convert('UTC','UTC','now + 1 hour',ATOM_TIME),
805 'reset_time_in_seconds' => strtotime('now + 1 hour')
808 return api_apply_template('ratelimit', $type, array('$hash' => $hash));
811 api_register_func('api/account/rate_limit_status','api_account_rate_limit_status',true);
814 * https://dev.twitter.com/docs/api/1/get/statuses/friends
815 * This function is deprecated by Twitter
818 function api_statuses_f(&$a, $type, $qtype) {
819 if (local_user()===false) return false;
820 $user_info = api_get_user($a);
822 if (x($_GET,'cursor') && $_GET['cursor']=='undefined'){
823 /* this is to stop Hotot to load friends multiple times
824 * I'm not sure if I'm missing return something or
825 * is a bug in hotot. Workaround, meantime
829 $data = array('$users' => $ret);
830 return api_apply_template("friends", $type, $data);
833 if($qtype == 'friends')
834 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
835 if($qtype == 'followers')
836 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
838 $r = q("SELECT id FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra",
844 $ret[] = api_get_user($a, $cid['id']);
848 $data = array('$users' => $ret);
849 return api_apply_template("friends", $type, $data);
852 function api_statuses_friends(&$a, $type){
853 return api_statuses_f($a,$type,"friends");
855 function api_statuses_followers(&$a, $type){
856 return api_statuses_f($a,$type,"followers");
858 api_register_func('api/statuses/friends','api_statuses_friends',true);
859 api_register_func('api/statuses/followers','api_statuses_followers',true);
866 function api_statusnet_config(&$a,$type) {
867 $name = $a->config['sitename'];
868 $server = $a->get_hostname();
869 $logo = $a->get_baseurl() . '/images/friendika-64.png';
870 $email = $a->config['admin_email'];
871 $closed = (($a->config['register_policy'] == REGISTER_CLOSED) ? 'true' : 'false');
872 $private = (($a->config['system']['block_public']) ? 'true' : 'false');
873 $textlimit = (string) (($a->config['max_import_size']) ? $a->config['max_import_size'] : 200000);
874 if($a->config['api_import_size'])
875 $texlimit = string($a->config['api_import_size']);
876 $ssl = (($a->config['system']['have_ssl']) ? 'true' : 'false');
877 $sslserver = (($ssl === 'true') ? str_replace('http:','https:',$a->get_baseurl()) : '');
880 'site' => array('name' => $name,'server' => $server, 'theme' => 'default', 'path' => '',
881 'logo' => $logo, 'fancy' => 'true', 'language' => 'en', 'email' => $email, 'broughtby' => '',
882 'broughtbyurl' => '', 'timezone' => 'UTC', 'closed' => $closed, 'inviteonly' => 'false',
883 'private' => $private, 'textlimit' => $textlimit, 'sslserver' => $sslserver, 'ssl' => $ssl,
884 'shorturllength' => '30'
888 return api_apply_template('config', $type, array('$config' => $config));
891 api_register_func('api/statusnet/config','api_statusnet_config',false);
893 function api_statusnet_version(&$a,$type) {
897 if($type === 'xml') {
898 header("Content-type: application/xml");
899 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>0.9.7</version>' . "\r\n";
902 elseif($type === 'json') {
903 header("Content-type: application/json");
908 api_register_func('api/statusnet/version','api_statusnet_version',false);
911 function api_ff_ids(&$a,$type,$qtype) {
915 if($qtype == 'friends')
916 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
917 if($qtype == 'followers')
918 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
921 $r = q("SELECT id FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra",
926 if($type === 'xml') {
927 header("Content-type: application/xml");
928 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<ids>' . "\r\n";
930 echo '<id>' . $rr['id'] . '</id>' . "\r\n";
931 echo '</ids>' . "\r\n";
934 elseif($type === 'json') {
936 header("Content-type: application/json");
937 foreach($r as $rr) $ret[] = $rr['id'];
938 echo json_encode($ret);
944 function api_friends_ids(&$a,$type) {
945 api_ff_ids($a,$type,'friends');
947 function api_followers_ids(&$a,$type) {
948 api_ff_ids($a,$type,'followers');
950 api_register_func('api/friends/ids','api_friends_ids',true);
951 api_register_func('api/followers/ids','api_followers_ids',true);
954 function api_direct_messages_new(&$a, $type) {
955 if (local_user()===false) return false;
957 if (!x($_POST, "text") || !x($_POST,"screen_name")) return;
959 $sender = api_get_user($a);
961 $r = q("SELECT `id` FROM `contact` WHERE `uid`=%d AND `nick`='%s'",
962 intval(local_user()),
963 dbesc($_POST['screen_name']));
965 $recipient = api_get_user($a, $r[0]['id']);
968 require_once("include/message.php");
969 $sub = ( (strlen($_POST['text'])>10)?substr($_POST['text'],0,10)."...":$_POST['text']);
970 $id = send_message($recipient['id'], $_POST['text'], $sub);
974 $r = q("SELECT * FROM `mail` WHERE id=%d", intval($id));
978 'created_at'=> api_date($item['created']),
979 'sender_id'=> $sender['id'] ,
980 'sender_screen_name'=> $sender['screen_name'],
982 'recipient_id'=> $recipient['id'],
983 'recipient_screen_name'=> $recipient['screen_name'],
984 'recipient'=> $recipient,
986 'text'=> $item['title']."\n".strip_tags(bbcode($item['body'])) ,
991 $ret = array("error"=>$id);
994 $data = Array('$messages'=>$ret);
999 $data = api_rss_extra($a, $data, $user_info);
1002 return api_apply_template("direct_messages", $type, $data);
1005 api_register_func('api/direct_messages/new','api_direct_messages_new',true);
1007 function api_direct_messages_box(&$a, $type, $box) {
1008 if (local_user()===false) return false;
1010 $user_info = api_get_user($a);
1013 $count = (x($_GET,'count')?$_GET['count']:20);
1014 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
1015 if ($page<0) $page=0;
1017 $start = $page*$count;
1020 if ($box=="sentbox") {
1021 $sql_extra = "`from-url`='%s'";
1023 $sql_extra = "`from-url`!='%s'";
1026 $r = q("SELECT * FROM `mail` WHERE uid=%d AND $sql_extra ORDER BY created DESC LIMIT %d,%d",
1027 intval(local_user()),
1028 dbesc( $a->get_baseurl() . '/profile/' . $a->user['nickname'] ),
1029 intval($start), intval($count)
1033 foreach($r as $item){
1036 $recipient = $user_info;
1037 $sender = api_get_user($a,$item['contact-id']);
1040 $recipient = api_get_user($a,$item['contact-id']);
1041 $sender = $user_info;
1046 'id' => $item['id'],
1047 'created_at'=> api_date($item['created']),
1048 'sender_id'=> $sender['id'] ,
1049 'sender_screen_name'=> $sender['screen_name'],
1051 'recipient_id'=> $recipient['id'],
1052 'recipient_screen_name'=> $recipient['screen_name'],
1053 'recipient'=> $recipient,
1055 'text'=> $item['title']."\n".strip_tags(bbcode($item['body'])) ,
1062 $data = array('$messages' => $ret);
1066 $data = api_rss_extra($a, $data, $user_info);
1069 return api_apply_template("direct_messages", $type, $data);
1073 function api_direct_messages_sentbox(&$a, $type){
1074 return api_direct_messages_box($a, $type, "sentbox");
1076 function api_direct_messages_inbox(&$a, $type){
1077 return api_direct_messages_box($a, $type, "inbox");
1079 api_register_func('api/direct_messages/sent','api_direct_messages_sentbox',true);
1080 api_register_func('api/direct_messages','api_direct_messages_inbox',true);