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.
359 'name' => $item['author-name'],
360 'screen_name' => $item['author_name'],
361 'location' => '', //$uinfo[0]['default-location'],
362 'profile_image_url' => $item['author-avatar'],
363 'url' => $item['author-link'],
365 'protected' => false, #
366 'friends_count' => 0,
368 'utc_offset' => 0, #XXX: fix me
369 'time_zone' => '', //$uinfo[0]['timezone'],
370 'geo_enabled' => false,
371 'statuses_count' => 0,
372 'lang' => 'en', #XXX: fix me
374 'followers_count' => 0,
375 'favourites_count' => 0,
376 'contributors_enabled' => false,
377 'follow_request_sent' => false,
378 'profile_background_color' => 'cfe8f6',
379 'profile_text_color' => '000000',
380 'profile_link_color' => 'FF8500',
381 'profile_sidebar_fill_color' =>'AD0066',
382 'profile_sidebar_border_color' => 'AD0066',
383 'profile_background_image_url' => '',
384 'profile_background_tile' => false,
385 'profile_use_background_image' => false,
386 'notifications' => false,
387 'verified' => true, #XXX: fix me
388 'followers' => '', #XXX: fix me
396 * apply xmlify() to all values of array $val, recursively
398 function api_xmlify($val){
399 if (is_bool($val)) return $val?"true":"false";
400 if (is_array($val)) return array_map('api_xmlify', $val);
401 return xmlify((string) $val);
405 * load api $templatename for $type and replace $data array
407 function api_apply_template($templatename, $type, $data){
415 $data = api_xmlify($data);
416 $tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
417 $ret = replace_macros($tpl, $data);
431 * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful;
432 * returns a 401 status code and an error message if not.
433 * http://developer.twitter.com/doc/get/account/verify_credentials
435 function api_account_verify_credentials(&$a, $type){
436 if (local_user()===false) return false;
437 $user_info = api_get_user($a);
439 return api_apply_template("user", $type, array('$user' => $user_info));
442 api_register_func('api/account/verify_credentials','api_account_verify_credentials', true);
446 * get data from $_POST or $_GET
448 function requestdata($k){
449 if (isset($_POST[$k])){
452 if (isset($_GET[$k])){
457 // TODO - media uploads
458 function api_statuses_update(&$a, $type) {
459 if (local_user()===false) return false;
460 $user_info = api_get_user($a);
462 // convert $_POST array items to the form we use for web posts.
464 // logger('api_post: ' . print_r($_POST,true));
466 $_POST['body'] = urldecode(requestdata('status'));
468 $parent = requestdata('in_reply_to_status_id');
469 if(ctype_digit($parent))
470 $_POST['parent'] = $parent;
472 $_POST['parent_uri'] = $parent;
474 if(requestdata('lat') && requestdata('long'))
475 $_POST['coord'] = sprintf("%s %s",requestdata('lat'),requestdata('long'));
476 $_POST['profile_uid'] = local_user();
477 if(requestdata('parent'))
478 $_POST['type'] = 'net-comment';
480 $_POST['type'] = 'wall';
482 // set this so that the item_post() function is quiet and doesn't redirect or emit json
484 $_POST['api_source'] = true;
486 // call out normal post function
488 require_once('mod/item.php');
491 // this should output the last post (the one we just posted).
492 return api_status_show($a,$type);
494 api_register_func('api/statuses/update','api_statuses_update', true);
497 function api_status_show(&$a, $type){
498 $user_info = api_get_user($a);
499 // get last public wall message
500 $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`nick` as `reply_author`
501 FROM `item`, `contact`,
502 (SELECT `item`.`id`, `item`.`contact-id`, `contact`.`nick` FROM `item`,`contact` WHERE `contact`.`id`=`item`.`contact-id`) as `i`
503 WHERE `item`.`contact-id` = %d
504 AND `i`.`id` = `item`.`parent`
505 AND `contact`.`id`=`item`.`contact-id` AND `contact`.`self`=1
506 AND `type`!='activity'
507 AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`=''
508 ORDER BY `created` DESC
510 intval($user_info['id'])
513 if (count($lastwall)>0){
514 $lastwall = $lastwall[0];
516 $in_reply_to_status_id = '';
517 $in_reply_to_user_id = '';
518 $in_reply_to_screen_name = '';
519 if ($lastwall['parent']!=$lastwall['id']) {
520 $in_reply_to_status_id=$lastwall['parent'];
521 $in_reply_to_user_id = $lastwall['reply_uid'];
522 $in_reply_to_screen_name = $lastwall['reply_author'];
524 $status_info = array(
525 'created_at' => api_date($lastwall['created']),
526 'id' => $lastwall['contact-id'],
527 'text' => strip_tags(bbcode($lastwall['body'])),
528 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'),
529 'truncated' => false,
530 'in_reply_to_status_id' => $in_reply_to_status_id,
531 'in_reply_to_user_id' => $in_reply_to_user_id,
532 'favorited' => false,
533 'in_reply_to_screen_name' => $in_reply_to_screen_name,
535 'coordinates' => $lastwall['coord'],
536 'place' => $lastwall['location'],
539 $status_info['user'] = $user_info;
541 return api_apply_template("status", $type, array('$status' => $status_info));
550 * Returns extended information of a given user, specified by ID or screen name as per the required id parameter.
551 * The author's most recent status will be returned inline.
552 * http://developer.twitter.com/doc/get/users/show
554 function api_users_show(&$a, $type){
555 $user_info = api_get_user($a);
556 // get last public wall message
557 $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`nick` as `reply_author`
558 FROM `item`, `contact`,
559 (SELECT `item`.`id`, `item`.`contact-id`, `contact`.`nick` FROM `item`,`contact` WHERE `contact`.`id`=`item`.`contact-id`) as `i`
560 WHERE `item`.`contact-id` = %d
561 AND `i`.`id` = `item`.`parent`
562 AND `contact`.`id`=`item`.`contact-id` AND `contact`.`self`=1
563 AND `type`!='activity'
564 AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`=''
565 ORDER BY `created` DESC
567 intval($user_info['id'])
570 if (count($lastwall)>0){
571 $lastwall = $lastwall[0];
573 $in_reply_to_status_id = '';
574 $in_reply_to_user_id = '';
575 $in_reply_to_screen_name = '';
576 if ($lastwall['parent']!=$lastwall['id']) {
577 $in_reply_to_status_id=$lastwall['parent'];
578 $in_reply_to_user_id = $lastwall['reply_uid'];
579 $in_reply_to_screen_name = $lastwall['reply_author'];
581 $user_info['status'] = array(
582 'created_at' => api_date($lastwall['created']),
583 'id' => $lastwall['contact-id'],
584 'text' => strip_tags(bbcode($lastwall['body'])),
585 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'),
586 'truncated' => false,
587 'in_reply_to_status_id' => $in_reply_to_status_id,
588 'in_reply_to_user_id' => $in_reply_to_user_id,
589 'favorited' => false,
590 'in_reply_to_screen_name' => $in_reply_to_screen_name,
592 'coordinates' => $lastwall['coord'],
593 'place' => $lastwall['location'],
597 return api_apply_template("user", $type, array('$user' => $user_info));
600 api_register_func('api/users/show','api_users_show');
604 * http://developer.twitter.com/doc/get/statuses/home_timeline
606 * TODO: Optional parameters
607 * TODO: Add reply info
609 function api_statuses_home_timeline(&$a, $type){
610 if (local_user()===false) return false;
612 $user_info = api_get_user($a);
613 // get last newtork messages
614 // $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
617 $count = (x($_GET,'count')?$_GET['count']:20);
618 $page = (x($_GET,'page')?$_GET['page']:0);
620 $start = $page*$count;
623 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
624 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
625 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
626 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
627 FROM `item`, `contact`
628 WHERE `item`.`uid` = %d
629 AND `item`.`visible` = 1 AND `item`.`deleted` = 0
630 AND `contact`.`id` = `item`.`contact-id`
631 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
633 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
634 intval($user_info['uid']),
635 intval($start), intval($count)
638 $ret = api_format_items($r,$user_info);
641 $data = array('$statuses' => $ret);
645 $data = api_rss_extra($a, $data, $user_info);
648 return api_apply_template("timeline", $type, $data);
650 api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true);
651 api_register_func('api/statuses/friends_timeline','api_statuses_home_timeline', true);
655 function api_statuses_user_timeline(&$a, $type){
656 if (local_user()===false) return false;
658 $user_info = api_get_user($a);
659 // get last newtork messages
660 // $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
663 $count = (x($_GET,'count')?$_GET['count']:20);
664 $page = (x($_GET,'page')?$_GET['page']:0);
666 $start = $page*$count;
669 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
670 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
671 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
672 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
673 FROM `item`, `contact`
674 WHERE `item`.`uid` = %d
675 AND `item`.`visible` = 1 AND `item`.`deleted` = 0
676 AND `item`.`wall` = 1
677 AND `contact`.`id` = `item`.`contact-id`
678 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
680 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
681 intval($user_info['uid']),
682 intval($start), intval($count)
685 $ret = api_format_items($r,$user_info);
688 $data = array('$statuses' => $ret);
692 $data = api_rss_extra($a, $data, $user_info);
695 return api_apply_template("timeline", $type, $data);
698 api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
701 function api_favorites(&$a, $type){
702 if (local_user()===false) return false;
704 $user_info = api_get_user($a);
705 // get last newtork messages
706 // $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
708 $count = (x($_GET,'count')?$_GET['count']:20);
709 $page = (x($_GET,'page')?$_GET['page']:0);
711 $start = $page*$count;
713 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
714 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
715 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
716 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
717 FROM `item`, `contact`
718 WHERE `item`.`uid` = %d
719 AND `item`.`visible` = 1 AND `item`.`deleted` = 0
720 AND `item`.`starred` = 1
721 AND `contact`.`id` = `item`.`contact-id`
722 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
724 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
725 intval($user_info['uid']),
726 intval($start), intval($count)
729 $ret = api_format_items($r,$user_info);
732 $data = array('$statuses' => $ret);
736 $data = api_rss_extra($a, $data, $user_info);
739 return api_apply_template("timeline", $type, $data);
742 api_register_func('api/favorites','api_favorites', true);
745 function api_format_items($r,$user_info) {
747 //logger('api_format_items: ' . print_r($r,true));
749 //logger('api_format_items: ' . print_r($user_info,true));
754 foreach($r as $item) {
755 $status_user = (($item['cid']==$user_info['id'])?$user_info: api_item_get_user($a,$item));
757 'created_at'=> api_date($item['created']),
758 'published' => datetime_convert('UTC','UTC',$item['created'],ATOM_TIME),
759 'updated' => datetime_convert('UTC','UTC',$item['edited'],ATOM_TIME),
760 'id' => intval($item['id']),
761 'message_id' => $item['uri'],
762 'text' => strip_tags(bbcode($item['body'])),
763 'statusnet_html' => bbcode($item['body']),
764 'source' => (($item['app']) ? $item['app'] : 'web'),
765 'url' => ($item['plink']!=''?$item['plink']:$item['author-link']),
766 'truncated' => False,
767 'in_reply_to_status_id' => ($item['parent']!=$item['id']? intval($item['parent']):''),
768 'in_reply_to_user_id' => '',
769 'favorited' => $item['starred'] ? true : false,
770 'in_reply_to_screen_name' => '',
772 'coordinates' => $item['coord'],
773 'place' => $item['location'],
774 'contributors' => '',
777 'user' => $status_user ,
778 'objecttype' => (($item['object-type']) ? $item['object-type'] : ACTIVITY_OBJ_NOTE),
779 'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST),
780 'self' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
781 'edit' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
789 function api_account_rate_limit_status(&$a,$type) {
792 'remaining_hits' => (string) 150,
793 'hourly_limit' => (string) 150,
794 'reset_time' => datetime_convert('UTC','UTC','now + 1 hour',ATOM_TIME),
795 'reset_time_in_seconds' => strtotime('now + 1 hour')
798 return api_apply_template('ratelimit', $type, array('$hash' => $hash));
801 api_register_func('api/account/rate_limit_status','api_account_rate_limit_status',true);
804 * https://dev.twitter.com/docs/api/1/get/statuses/friends
805 * This function is deprecated by Twitter
808 function api_statuses_f(&$a, $type, $qtype) {
809 if (local_user()===false) return false;
810 $user_info = api_get_user($a);
812 if (x($_GET,'cursor') && $_GET['cursor']=='undefined'){
813 /* this is to stop Hotot to load friends multiple times
814 * I'm not sure if I'm missing return something or
815 * is a bug in hotot. Workaround, meantime
819 $data = array('$users' => $ret);
820 return api_apply_template("friends", $type, $data);
823 if($qtype == 'friends')
824 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
825 if($qtype == 'followers')
826 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
828 $r = q("SELECT id FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra",
834 $ret[] = api_get_user($a, $cid['id']);
838 $data = array('$users' => $ret);
839 return api_apply_template("friends", $type, $data);
842 function api_statuses_friends(&$a, $type){
843 return api_statuses_f($a,$type,"friends");
845 function api_statuses_followers(&$a, $type){
846 return api_statuses_f($a,$type,"followers");
848 api_register_func('api/statuses/friends','api_statuses_friends',true);
849 api_register_func('api/statuses/followers','api_statuses_followers',true);
856 function api_statusnet_config(&$a,$type) {
857 $name = $a->config['sitename'];
858 $server = $a->get_hostname();
859 $logo = $a->get_baseurl() . '/images/friendika-64.png';
860 $email = $a->config['admin_email'];
861 $closed = (($a->config['register_policy'] == REGISTER_CLOSED) ? 'true' : 'false');
862 $private = (($a->config['system']['block_public']) ? 'true' : 'false');
863 $textlimit = (string) (($a->config['max_import_size']) ? $a->config['max_import_size'] : 200000);
864 if($a->config['api_import_size'])
865 $texlimit = string($a->config['api_import_size']);
866 $ssl = (($a->config['system']['have_ssl']) ? 'true' : 'false');
867 $sslserver = (($ssl === 'true') ? str_replace('http:','https:',$a->get_baseurl()) : '');
870 'site' => array('name' => $name,'server' => $server, 'theme' => 'default', 'path' => '',
871 'logo' => $logo, 'fancy' => 'true', 'language' => 'en', 'email' => $email, 'broughtby' => '',
872 'broughtbyurl' => '', 'timezone' => 'UTC', 'closed' => $closed, 'inviteonly' => 'false',
873 'private' => $private, 'textlimit' => $textlimit, 'sslserver' => $sslserver, 'ssl' => $ssl,
874 'shorturllength' => '30'
878 return api_apply_template('config', $type, array('$config' => $config));
881 api_register_func('api/statusnet/config','api_statusnet_config',false);
883 function api_statusnet_version(&$a,$type) {
887 if($type === 'xml') {
888 header("Content-type: application/xml");
889 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>0.9.7</version>' . "\r\n";
892 elseif($type === 'json') {
893 header("Content-type: application/json");
898 api_register_func('api/statusnet/version','api_statusnet_version',false);
901 function api_ff_ids(&$a,$type,$qtype) {
905 if($qtype == 'friends')
906 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
907 if($qtype == 'followers')
908 $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
911 $r = q("SELECT id FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra",
916 if($type === 'xml') {
917 header("Content-type: application/xml");
918 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<ids>' . "\r\n";
920 echo '<id>' . $rr['id'] . '</id>' . "\r\n";
921 echo '</ids>' . "\r\n";
924 elseif($type === 'json') {
926 header("Content-type: application/json");
927 foreach($r as $rr) $ret[] = $rr['id'];
928 echo json_encode($ret);
934 function api_friends_ids(&$a,$type) {
935 api_ff_ids($a,$type,'friends');
937 function api_followers_ids(&$a,$type) {
938 api_ff_ids($a,$type,'followers');
940 api_register_func('api/friends/ids','api_friends_ids',true);
941 api_register_func('api/followers/ids','api_followers_ids',true);
944 function api_direct_messages_new(&$a, $type) {
945 if (local_user()===false) return false;
947 if (!x($_POST, "text") || !x($_POST,"screen_name")) return;
949 $sender = api_get_user($a);
951 $r = q("SELECT `id` FROM `contact` WHERE `uid`=%d AND `nick`='%s'",
952 intval(local_user()),
953 dbesc($_POST['screen_name']));
955 $recipient = api_get_user($a, $r[0]['id']);
958 require_once("include/message.php");
959 $sub = ( (strlen($_POST['text'])>10)?substr($_POST['text'],0,10)."...":$_POST['text']);
960 $id = send_message($recipient['id'], $_POST['text'], $sub);
964 $r = q("SELECT * FROM `mail` WHERE id=%d", intval($id));
968 'created_at'=> datetime_convert('UTC','UTC',$item['created'],ATOM_TIME),
969 'sender_id'=> $sender['id'] ,
970 'sender_screen_name'=> $sender['screen_name'],
972 'recipient_id'=> $recipient['id'],
973 'recipient_screen_name'=> $recipient['screen_name'],
974 'recipient'=> $recipient,
976 'text'=> $item['title']."\n".strip_tags(bbcode($item['body'])) ,
981 $ret = array("error"=>$id);
984 $data = Array('$messages'=>$ret);
989 $data = api_rss_extra($a, $data, $user_info);
992 return api_apply_template("direct_messages", $type, $data);
995 api_register_func('api/direct_messages/new','api_direct_messages_new',true);
997 function api_direct_messages_box(&$a, $type, $box) {
998 if (local_user()===false) return false;
1000 $user_info = api_get_user($a);
1003 $count = (x($_GET,'count')?$_GET['count']:20);
1004 $page = (x($_GET,'page')?$_GET['page']:0);
1006 $start = $page*$count;
1009 if ($box=="sentbox") {
1010 $sql_extra = "`from-url`='%s'";
1012 $sql_extra = "`from-url`!='%s'";
1015 $r = q("SELECT * FROM `mail` WHERE uid=%d AND $sql_extra ORDER BY created DESC LIMIT %d,%d",
1016 intval(local_user()),
1017 dbesc( $a->get_baseurl() . '/profile/' . $a->user['nickname'] ),
1018 intval($start), intval($count)
1022 foreach($r as $item){
1025 $recipient = $user_info;
1026 $sender = api_get_user($a,$item['contact-id']);
1029 $recipient = api_get_user($a,$item['contact-id']);
1030 $sender = $user_info;
1035 'id' => $item['id'],
1036 'created_at'=> datetime_convert('UTC','UTC',$item['created'],ATOM_TIME),
1037 'sender_id'=> $sender['id'] ,
1038 'sender_screen_name'=> $sender['screen_name'],
1040 'recipient_id'=> $recipient['id'],
1041 'recipient_screen_name'=> $recipient['screen_name'],
1042 'recipient'=> $recipient,
1044 'text'=> $item['title']."\n".strip_tags(bbcode($item['body'])) ,
1051 $data = array('$messages' => $ret);
1055 $data = api_rss_extra($a, $data, $user_info);
1058 return api_apply_template("direct_messages", $type, $data);
1062 function api_direct_messages_sentbox(&$a, $type){
1063 return api_direct_messages_box($a, $type, "sentbox");
1065 function api_direct_messages_inbox(&$a, $type){
1066 return api_direct_messages_box($a, $type, "inbox");
1068 api_register_func('api/direct_messages/sent','api_direct_messages_sentbox',true);
1069 api_register_func('api/direct_messages','api_direct_messages_inbox',true);