]> git.mxchange.org Git - friendica.git/blob - include/api.php
Merge pull request #621 from annando/master
[friendica.git] / include / api.php
1 <?php
2         require_once("include/bbcode.php");
3         require_once("include/datetime.php");
4         require_once("include/conversation.php");
5         require_once("include/oauth.php");
6         require_once("include/html2plain.php");
7         /*
8          * Twitter-Like API
9          *
10          */
11
12         $API = Array();
13         $called_api = Null;
14
15         function api_user() {
16           // It is not sufficient to use local_user() to check whether someone is allowed to use the API,
17           // because this will open CSRF holes (just embed an image with src=friendicasite.com/api/statuses/update?status=CSRF
18           // into a page, and visitors will post something without noticing it).
19           // Instead, use this function.
20           if ($_SESSION["allow_api"])
21             return local_user();
22
23           return false;
24         }
25
26         function api_date($str){
27                 //Wed May 23 06:01:13 +0000 2007
28                 return datetime_convert('UTC', 'UTC', $str, "D M d H:i:s +0000 Y" );
29         }
30
31
32         function api_register_func($path, $func, $auth=false){
33                 global $API;
34                 $API[$path] = array('func'=>$func,
35                                                         'auth'=>$auth);
36         }
37
38         /**
39          * Simple HTTP Login
40          */
41
42         function api_login(&$a){
43                 // login with oauth
44                 try{
45                         $oauth = new FKOAuth1();
46                         list($consumer,$token) = $oauth->verify_request(OAuthRequest::from_request());
47                         if (!is_null($token)){
48                                 $oauth->loginUser($token->uid);
49                                 call_hooks('logged_in', $a->user);
50                                 return;
51                         }
52                         echo __file__.__line__.__function__."<pre>"; var_dump($consumer, $token); die();
53                 }catch(Exception $e){
54                         logger(__file__.__line__.__function__."\n".$e);
55                         //die(__file__.__line__.__function__."<pre>".$e); die();
56                 }
57
58                 
59                 
60                 // workaround for HTTP-auth in CGI mode
61                 if(x($_SERVER,'REDIRECT_REMOTE_USER')) {
62                         $userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"],6)) ;
63                         if(strlen($userpass)) {
64                                 list($name, $password) = explode(':', $userpass);
65                                 $_SERVER['PHP_AUTH_USER'] = $name;
66                                 $_SERVER['PHP_AUTH_PW'] = $password;
67                         }
68                 }
69
70                 if (!isset($_SERVER['PHP_AUTH_USER'])) {
71                    logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG);
72                     header('WWW-Authenticate: Basic realm="Friendica"');
73                     header('HTTP/1.0 401 Unauthorized');
74                     die('This api requires login');
75                 }
76                 
77                 $user = $_SERVER['PHP_AUTH_USER'];
78                 $encrypted = hash('whirlpool',trim($_SERVER['PHP_AUTH_PW']));
79                 
80                 
81                         /**
82                          *  next code from mod/auth.php. needs better solution
83                          */
84                         
85                 // process normal login request
86
87                 $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) 
88                         AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
89                         dbesc(trim($user)),
90                         dbesc(trim($user)),
91                         dbesc($encrypted)
92                 );
93                 if(count($r)){
94                         $record = $r[0];
95                 } else {
96                    logger('API_login failure: ' . print_r($_SERVER,true), LOGGER_DEBUG);
97                     header('WWW-Authenticate: Basic realm="Friendica"');
98                     header('HTTP/1.0 401 Unauthorized');
99                     die('This api requires login');
100                 }
101
102                 require_once('include/security.php');
103                 authenticate_success($record); $_SESSION["allow_api"] = true;
104
105                 call_hooks('logged_in', $a->user);
106
107         }
108         
109         /**************************
110          *  MAIN API ENTRY POINT  *
111          **************************/
112         function api_call(&$a){
113                 GLOBAL $API, $called_api;
114
115                 // preset
116                 $type="json";
117
118                 foreach ($API as $p=>$info){
119                         if (strpos($a->query_string, $p)===0){
120                                 $called_api= explode("/",$p);
121                                 //unset($_SERVER['PHP_AUTH_USER']);
122                                 if ($info['auth']===true && api_user()===false) {
123                                                 api_login($a);
124                                 }
125
126                                 load_contact_links(api_user());
127
128                                 logger('API call for ' . $a->user['username'] . ': ' . $a->query_string);
129                                 logger('API parameters: ' . print_r($_REQUEST,true));
130                                 $type="json";
131                                 if (strpos($a->query_string, ".xml")>0) $type="xml";
132                                 if (strpos($a->query_string, ".json")>0) $type="json";
133                                 if (strpos($a->query_string, ".rss")>0) $type="rss";
134                                 if (strpos($a->query_string, ".atom")>0) $type="atom";
135                                 if (strpos($a->query_string, ".as")>0) $type="as";
136
137                                 $r = call_user_func($info['func'], $a, $type);
138                                 if ($r===false) return;
139
140                                 switch($type){
141                                         case "xml":
142                                                 $r = mb_convert_encoding($r, "UTF-8",mb_detect_encoding($r));
143                                                 header ("Content-Type: text/xml");
144                                                 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
145                                                 break;
146                                         case "json":
147                                                 //header ("Content-Type: application/json");
148                                                 foreach($r as $rr)
149                                                     return json_encode($rr);
150                                                 break;
151                                         case "rss":
152                                                 header ("Content-Type: application/rss+xml");
153                                                 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
154                                                 break;
155                                         case "atom":
156                                                 header ("Content-Type: application/atom+xml");
157                                                 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
158                                                 break;
159                                         case "as":
160                                                 //header ("Content-Type: application/json");
161                                                 //foreach($r as $rr)
162                                                 //    return json_encode($rr);
163                                                 return json_encode($r);
164                                                 break;
165
166                                 }
167                                 //echo "<pre>"; var_dump($r); die();
168                         }
169                 }
170                 header("HTTP/1.1 404 Not Found");
171                 logger('API call not implemented: '.$a->query_string." - ".print_r($_REQUEST,true));
172                 $r = '<status><error>not implemented</error></status>';
173                 switch($type){
174                         case "xml":
175                                 header ("Content-Type: text/xml");
176                                 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
177                                 break;
178                         case "json":
179                                 header ("Content-Type: application/json");
180                             return json_encode(array('error' => 'not implemented'));
181                                 break;
182                         case "rss":
183                                 header ("Content-Type: application/rss+xml");
184                                 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
185                                 break;
186                         case "atom":
187                                 header ("Content-Type: application/atom+xml");
188                                 return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
189                                 break;
190                 }
191         }
192
193         /**
194          * RSS extra info
195          */
196         function api_rss_extra(&$a, $arr, $user_info){
197                 if (is_null($user_info)) $user_info = api_get_user($a);
198                 $arr['$user'] = $user_info;
199                 $arr['$rss'] = array(
200                         'alternate' => $user_info['url'],
201                         'self' => $a->get_baseurl(). "/". $a->query_string,
202                         'base' => $a->get_baseurl(),
203                         'updated' => api_date(null),
204                         'atom_updated' => datetime_convert('UTC','UTC','now',ATOM_TIME),
205                         'language' => $user_info['language'],
206                         'logo'  => $a->get_baseurl()."/images/friendica-32.png",
207                 );
208                 
209                 return $arr;
210         }
211          
212         /**
213          * Returns user info array.
214          */
215         function api_get_user(&$a, $contact_id = Null){
216                 global $called_api;
217                 $user = null;
218                 $extra_query = "";
219
220
221                 if(!is_null($contact_id)){
222                         $user=$contact_id;
223                         $extra_query = "AND `contact`.`id` = %d ";
224                 }
225                 
226                 if(is_null($user) && x($_GET, 'user_id')) {
227                         $user = intval($_GET['user_id']);       
228                         $extra_query = "AND `contact`.`id` = %d ";
229                 }
230                 if(is_null($user) && x($_GET, 'screen_name')) {
231                         $user = dbesc($_GET['screen_name']);    
232                         $extra_query = "AND `contact`.`nick` = '%s' ";
233                         if (api_user()!==false)  $extra_query .= "AND `contact`.`uid`=".intval(api_user());
234                         
235                 }
236                 
237                 if (is_null($user) && $a->argc > (count($called_api)-1)){
238                         $argid = count($called_api);
239                         list($user, $null) = explode(".",$a->argv[$argid]);
240                         if(is_numeric($user)){
241                                 $user = intval($user);
242                                 $extra_query = "AND `contact`.`id` = %d ";
243                         } else {
244                                 $user = dbesc($user);
245                                 $extra_query = "AND `contact`.`nick` = '%s' ";
246                                 if (api_user()!==false)  $extra_query .= "AND `contact`.`uid`=".intval(api_user());
247                         }
248                 }
249                 
250                 if (! $user) {
251                         if (api_user()===false) {
252                                 api_login($a); return False;
253                         } else {
254                                 $user = $_SESSION['uid'];
255                                 $extra_query = "AND `contact`.`uid` = %d AND `contact`.`self` = 1 ";
256                         }
257                         
258                 }
259                 
260                 logger('api_user: ' . $extra_query . ', user: ' . $user);
261                 // user info            
262                 $uinfo = q("SELECT *, `contact`.`id` as `cid` FROM `contact`
263                                 WHERE 1
264                                 $extra_query",
265                                 $user
266                 );
267                 if (count($uinfo)==0) {
268                         return False;
269                 }
270                 
271                 if($uinfo[0]['self']) {
272                         $usr = q("select * from user where uid = %d limit 1",
273                                 intval(api_user())
274                         );
275                         $profile = q("select * from profile where uid = %d and `is-default` = 1 limit 1",
276                                 intval(api_user())
277                         );
278
279                         // count public wall messages
280                         $r = q("SELECT COUNT(`id`) as `count` FROM `item`
281                                         WHERE  `uid` = %d
282                                         AND `type`='wall' 
283                                         AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''",
284                                         intval($uinfo[0]['uid'])
285                         );
286                         $countitms = $r[0]['count'];
287                 }
288                 else {
289                         $r = q("SELECT COUNT(`id`) as `count` FROM `item`
290                                         WHERE  `contact-id` = %d
291                                         AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''",
292                                         intval($uinfo[0]['id'])
293                         );
294                         $countitms = $r[0]['count'];
295                 }
296
297                 // count friends
298                 $r = q("SELECT COUNT(`id`) as `count` FROM `contact`
299                                 WHERE  `uid` = %d AND `rel` IN ( %d, %d )
300                                 AND `self`=0 AND `blocked`=0 AND `pending`=0 AND `hidden`=0", 
301                                 intval($uinfo[0]['uid']),
302                                 intval(CONTACT_IS_SHARING),
303                                 intval(CONTACT_IS_FRIEND)
304                 );
305                 $countfriends = $r[0]['count'];
306
307                 $r = q("SELECT COUNT(`id`) as `count` FROM `contact`
308                                 WHERE  `uid` = %d AND `rel` IN ( %d, %d )
309                                 AND `self`=0 AND `blocked`=0 AND `pending`=0 AND `hidden`=0", 
310                                 intval($uinfo[0]['uid']),
311                                 intval(CONTACT_IS_FOLLOWER),
312                                 intval(CONTACT_IS_FRIEND)
313                 );
314                 $countfollowers = $r[0]['count'];
315
316                 $r = q("SELECT count(`id`) as `count` FROM item where starred = 1 and uid = %d and deleted = 0",
317                         intval($uinfo[0]['uid'])
318                 );
319                 $starred = $r[0]['count'];
320         
321
322                 if(! $uinfo[0]['self']) {
323                         $countfriends = 0;
324                         $countfollowers = 0;
325                         $starred = 0;
326                 }
327
328                 $ret = Array(
329                         'id' => intval($uinfo[0]['cid']),
330                         'self' => intval($uinfo[0]['self']),
331                         'uid' => intval($uinfo[0]['uid']),
332                         'name' => (($uinfo[0]['name']) ? $uinfo[0]['name'] : $uinfo[0]['nick']),
333                         'screen_name' => (($uinfo[0]['nick']) ? $uinfo[0]['nick'] : $uinfo[0]['name']),
334                         'location' => ($usr) ? $usr[0]['default-location'] : '',
335                         'profile_image_url' => $uinfo[0]['micro'],
336                         'url' => $uinfo[0]['url'],
337                         'contact_url' => $a->get_baseurl()."/contacts/".$uinfo[0]['cid'],
338                         'protected' => false,   
339                         'friends_count' => intval($countfriends),
340                         'created_at' => api_date($uinfo[0]['name-date']),
341                         'utc_offset' => "+00:00",
342                         'time_zone' => 'UTC', //$uinfo[0]['timezone'],
343                         'geo_enabled' => false,
344                         'statuses_count' => intval($countitms), #XXX: fix me 
345                         'lang' => 'en', #XXX: fix me
346                         'description' => (($profile) ? $profile[0]['pdesc'] : ''),
347                         'followers_count' => intval($countfollowers),
348                         'favourites_count' => intval($starred),
349                         'contributors_enabled' => false,
350                         'follow_request_sent' => true,
351                         'profile_background_color' => 'cfe8f6',
352                         'profile_text_color' => '000000',
353                         'profile_link_color' => 'FF8500',
354                         'profile_sidebar_fill_color' =>'AD0066',
355                         'profile_sidebar_border_color' => 'AD0066',
356                         'profile_background_image_url' => '',
357                         'profile_background_tile' => false,
358                         'profile_use_background_image' => false,
359                         'notifications' => false,
360                         'following' => '', #XXX: fix me
361                         'verified' => true, #XXX: fix me
362                         'status' => array()
363                 );
364         
365                 return $ret;
366                 
367         }
368
369         function api_item_get_user(&$a, $item) {
370                 global $usercache;
371
372                 // The author is our direct contact, in a conversation with us.
373                 if(link_compare($item['url'],$item['author-link'])) {
374                         return api_get_user($a,$item['cid']);
375                 }
376                 else {
377                         // The author may be a contact of ours, but is replying to somebody else. 
378                         // Figure out if we know him/her.
379                         $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
380             if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
381                                 return api_get_user($a,$a->contacts[$normalised]['id']);
382                 }
383                 // We don't know this person directly.
384                 
385                 list($nick, $name) = array_map("trim",explode("(",$item['author-name']));
386                 $name=str_replace(")","",$name);
387
388                 if ($name == '')
389                         $name = $nick;
390
391                 if ($nick == '')
392                         $nick = $name;
393
394                 // Generating a random ID
395                 if (is_null($usercache[$nick]) or !array_key_exists($nick, $usercache))
396                         $usercache[$nick] = mt_rand(2000000, 2100000);
397
398                 $ret = array(
399                         'id' => $usercache[$nick],
400                         'name' => $name,
401                         'screen_name' => $nick,
402                         'location' => '', //$uinfo[0]['default-location'],
403                         'description' => '',
404                         'profile_image_url' => $item['author-avatar'],
405                         'url' => $item['author-link'],
406                         'protected' => false,   #
407                         'followers_count' => 0,
408                         'friends_count' => 0,
409                         'created_at' => '',
410                         'favourites_count' => 0,
411                         'utc_offset' => 0, #XXX: fix me
412                         'time_zone' => '', //$uinfo[0]['timezone'],
413                         'statuses_count' => 0,
414                         'following' => 1,
415                         'statusnet_blocking' => false,
416                         'notifications' => false,
417                         'uid' => 0,
418                         'contact_url' => 0,
419                         'geo_enabled' => false,
420                         'lang' => 'en', #XXX: fix me
421                         'contributors_enabled' => false,
422                         'follow_request_sent' => false,
423                         'profile_background_color' => 'cfe8f6',
424                         'profile_text_color' => '000000',
425                         'profile_link_color' => 'FF8500',
426                         'profile_sidebar_fill_color' =>'AD0066',
427                         'profile_sidebar_border_color' => 'AD0066',
428                         'profile_background_image_url' => '',
429                         'profile_background_tile' => false,
430                         'profile_use_background_image' => false,
431                         'verified' => true, #XXX: fix me
432                         'followers' => '', #XXX: fix me
433                         'status' => array()
434                 );
435
436                 return $ret; 
437         }
438
439
440         /**
441          *  load api $templatename for $type and replace $data array
442          */
443         function api_apply_template($templatename, $type, $data){
444
445                 $a = get_app();
446
447                 switch($type){
448                         case "atom":
449                         case "rss":
450                         case "xml":
451                                 $data = array_xmlify($data);
452                                 $tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
453                                 if(! $tpl) {
454                                         header ("Content-Type: text/xml");
455                                         echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<status><error>not implemented</error></status>';
456                                         killme();
457                                 }
458                                 $ret = replace_macros($tpl, $data);
459                                 break;
460                         case "json":
461                                 $ret = $data;
462                                 break;
463                 }
464                 return $ret;
465         }
466         
467         /**
468          ** TWITTER API
469          */
470         
471         /**
472          * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; 
473          * returns a 401 status code and an error message if not. 
474          * http://developer.twitter.com/doc/get/account/verify_credentials
475          */
476         function api_account_verify_credentials(&$a, $type){
477                 if (api_user()===false) return false;
478                 $user_info = api_get_user($a);
479                 
480                 return api_apply_template("user", $type, array('$user' => $user_info));
481
482         }
483         api_register_func('api/account/verify_credentials','api_account_verify_credentials', true);
484                 
485
486         /**
487          * get data from $_POST or $_GET
488          */
489         function requestdata($k){
490                 if (isset($_POST[$k])){
491                         return $_POST[$k];
492                 }
493                 if (isset($_GET[$k])){
494                         return $_GET[$k];
495                 }
496                 return null;
497         }
498
499 /*Waitman Gobble Mod*/
500         function api_statuses_mediap(&$a, $type) {
501                 if (api_user()===false) {
502                         logger('api_statuses_update: no user');
503                         return false;
504                 }
505                 $user_info = api_get_user($a);
506
507                 $_REQUEST['type'] = 'wall';
508                 $_REQUEST['profile_uid'] = api_user();
509                 $_REQUEST['api_source'] = true;
510                 $txt = requestdata('status');
511                 //$txt = urldecode(requestdata('status'));
512
513                 require_once('library/HTMLPurifier.auto.php');
514                 require_once('include/html2bbcode.php');
515
516                 if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) {
517                         $txt = html2bb_video($txt);
518                         $config = HTMLPurifier_Config::createDefault();
519                         $config->set('Cache.DefinitionImpl', null);
520                         $purifier = new HTMLPurifier($config);
521                         $txt = $purifier->purify($txt);
522                 }
523                 $txt = html2bbcode($txt);
524
525                 $a->argv[1]=$user_info['screen_name']; //should be set to username?
526
527                 $_REQUEST['hush']='yeah'; //tell wall_upload function to return img info instead of echo
528                 require_once('mod/wall_upload.php');
529                 $bebop = wall_upload_post($a);
530
531                 //now that we have the img url in bbcode we can add it to the status and insert the wall item.
532                 $_REQUEST['body']=$txt."\n\n".$bebop;
533                 require_once('mod/item.php');
534                 item_post($a);
535
536                 // this should output the last post (the one we just posted).
537                 return api_status_show($a,$type);
538         }
539         api_register_func('api/statuses/mediap','api_statuses_mediap', true);
540 /*Waitman Gobble Mod*/
541
542
543         function api_statuses_update(&$a, $type) {
544                 if (api_user()===false) {
545                         logger('api_statuses_update: no user');
546                         return false;
547                 }
548                 $user_info = api_get_user($a);
549
550                 // convert $_POST array items to the form we use for web posts.
551
552                 // logger('api_post: ' . print_r($_POST,true));
553
554                 if(requestdata('htmlstatus')) {
555                         require_once('library/HTMLPurifier.auto.php');
556                         require_once('include/html2bbcode.php');
557
558                         $txt = requestdata('htmlstatus');
559                         if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) {
560
561                                 $txt = html2bb_video($txt);
562
563                                 $config = HTMLPurifier_Config::createDefault();
564                                 $config->set('Cache.DefinitionImpl', null);
565
566
567                                 $purifier = new HTMLPurifier($config);
568                                 $txt = $purifier->purify($txt);
569
570                                 $_REQUEST['body'] = html2bbcode($txt);
571                         }
572
573                 }
574                 else
575                         $_REQUEST['body'] = requestdata('status');
576                         //$_REQUEST['body'] = urldecode(requestdata('status'));
577
578                 $_REQUEST['title'] = requestdata('title');
579
580                 $parent = requestdata('in_reply_to_status_id');
581                 if(ctype_digit($parent))
582                         $_REQUEST['parent'] = $parent;
583                 else
584                         $_REQUEST['parent_uri'] = $parent;
585
586                 if(requestdata('lat') && requestdata('long'))
587                         $_REQUEST['coord'] = sprintf("%s %s",requestdata('lat'),requestdata('long'));
588                 $_REQUEST['profile_uid'] = api_user();
589
590                 if($parent)
591                         $_REQUEST['type'] = 'net-comment';
592                 else {
593                         $_REQUEST['type'] = 'wall';
594                         if(x($_FILES,'media')) {
595                                 // upload the image if we have one
596                                 $_REQUEST['hush']='yeah'; //tell wall_upload function to return img info instead of echo
597                                 require_once('mod/wall_upload.php');
598                                 $media = wall_upload_post($a);
599                                 if(strlen($media)>0)
600                                         $_REQUEST['body'] .= "\n\n".$media;
601                         }
602                 }
603
604                 // set this so that the item_post() function is quiet and doesn't redirect or emit json
605
606                 $_REQUEST['api_source'] = true;
607
608                 // call out normal post function
609
610                 require_once('mod/item.php');
611                 item_post($a);  
612
613                 // this should output the last post (the one we just posted).
614                 return api_status_show($a,$type);
615         }
616         api_register_func('api/statuses/update','api_statuses_update', true);
617
618
619         function api_status_show(&$a, $type){
620                 $user_info = api_get_user($a);
621                 // get last public wall message
622                 $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`nick` as `reply_author`
623                                 FROM `item`, `contact`,
624                                         (SELECT `item`.`id`, `item`.`contact-id`, `contact`.`nick` FROM `item`,`contact` WHERE `contact`.`id`=`item`.`contact-id`) as `i` 
625                                 WHERE `item`.`contact-id` = %d
626                                         AND `i`.`id` = `item`.`parent`
627                                         AND `contact`.`id`=`item`.`contact-id` AND `contact`.`self`=1
628                                         AND `type`!='activity'
629                                         AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`=''
630                                 ORDER BY `created` DESC 
631                                 LIMIT 1",
632                                 intval($user_info['id'])
633                 );
634
635                 if (count($lastwall)>0){
636                         $lastwall = $lastwall[0];
637
638                         $in_reply_to_status_id = '';
639                         $in_reply_to_user_id = '';
640                         $in_reply_to_screen_name = '';
641                         if ($lastwall['parent']!=$lastwall['id']) {
642                                 $in_reply_to_status_id=$lastwall['parent'];
643                                 $in_reply_to_user_id = $lastwall['reply_uid'];
644                                 $in_reply_to_screen_name = $lastwall['reply_author'];
645                         }
646                         $status_info = array(
647                                 'text' => html2plain(bbcode($lastwall['body'], false, false, true), 0),
648                                 'truncated' => false,
649                                 'created_at' => api_date($lastwall['created']),
650                                 'in_reply_to_status_id' => $in_reply_to_status_id,
651                                 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'),
652                                 'id' => $lastwall['contact-id'],
653                                 'in_reply_to_user_id' => $in_reply_to_user_id,
654                                 'in_reply_to_screen_name' => $in_reply_to_screen_name,
655                                 'geo' => '',
656                                 'favorited' => false,
657                                 'coordinates' => $lastwall['coord'],
658                                 'place' => $lastwall['location'],
659                                 'contributors' => ''                                    
660                         );
661                         $status_info['user'] = $user_info;
662                 }
663                 return  api_apply_template("status", $type, array('$status' => $status_info));
664                 
665         }
666
667
668
669
670                 
671         /**
672          * Returns extended information of a given user, specified by ID or screen name as per the required id parameter.
673          * The author's most recent status will be returned inline.
674          * http://developer.twitter.com/doc/get/users/show
675          */
676         function api_users_show(&$a, $type){
677                 $user_info = api_get_user($a);
678                 // get last public wall message
679                 $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`nick` as `reply_author`
680                                 FROM `item`, `contact`,
681                                         (SELECT `item`.`id`, `item`.`contact-id`, `contact`.`nick` FROM `item`,`contact` WHERE `contact`.`id`=`item`.`contact-id`) as `i` 
682                                 WHERE `item`.`contact-id` = %d
683                                         AND `i`.`id` = `item`.`parent`
684                                         AND `contact`.`id`=`item`.`contact-id` AND `contact`.`self`=1
685                                         AND `type`!='activity'
686                                         AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`=''
687                                 ORDER BY `created` DESC 
688                                 LIMIT 1",
689                                 intval($user_info['id'])
690                 );
691
692                 if (count($lastwall)>0){
693                         $lastwall = $lastwall[0];
694                         
695                         $in_reply_to_status_id = '';
696                         $in_reply_to_user_id = '';
697                         $in_reply_to_screen_name = '';
698                         if ($lastwall['parent']!=$lastwall['id']) {
699                                 $in_reply_to_status_id=$lastwall['parent'];
700                                 $in_reply_to_user_id = $lastwall['reply_uid'];
701                                 $in_reply_to_screen_name = $lastwall['reply_author'];
702                         }
703                         $user_info['status'] = array(
704                                 'created_at' => api_date($lastwall['created']),
705                                 'id' => $lastwall['contact-id'],
706                                 'text' => html2plain(bbcode($lastwall['body'], false, false, true), 0),
707                                 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'),
708                                 'truncated' => false,
709                                 'in_reply_to_status_id' => $in_reply_to_status_id,
710                                 'in_reply_to_user_id' => $in_reply_to_user_id,
711                                 'favorited' => false,
712                                 'in_reply_to_screen_name' => $in_reply_to_screen_name,
713                                 'geo' => '',
714                                 'coordinates' => $lastwall['coord'],
715                                 'place' => $lastwall['location'],
716                                 'contributors' => ''
717                         );
718                 }
719                 return  api_apply_template("user", $type, array('$user' => $user_info));
720
721         }
722         api_register_func('api/users/show','api_users_show');
723
724         /**
725          *
726          * http://developer.twitter.com/doc/get/statuses/home_timeline
727          *
728          * TODO: Optional parameters
729          * TODO: Add reply info
730          */
731         function api_statuses_home_timeline(&$a, $type){
732                 if (api_user()===false) return false;
733
734                 $user_info = api_get_user($a);
735                 // get last newtork messages
736
737
738                 // params
739                 $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
740                 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
741                 if ($page<0) $page=0;
742                 $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
743                 $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0);
744                 $exclude_replies = (x($_REQUEST,'exclude_replies')?1:0);
745                 //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
746
747                 $start = $page*$count;
748
749                 //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false);
750
751                 $sql_extra = '';
752                 if ($max_id > 0)
753                         $sql_extra .= ' AND `item`.`id` <= '.intval($max_id);
754                 if ($exclude_replies > 0)
755                         $sql_extra .= ' AND `item`.`parent` = `item`.`id`';
756
757                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
758                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
759                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
760                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
761                         FROM `item`, `contact`
762                         WHERE `item`.`uid` = %d
763                         AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
764                         AND `contact`.`id` = `item`.`contact-id`
765                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
766                         $sql_extra
767                         AND `item`.`id`>%d
768                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
769                         intval($user_info['uid']),
770                         intval($since_id),
771                         intval($start), intval($count)
772                 );
773
774                 $ret = api_format_items($r,$user_info);
775
776                 // We aren't going to try to figure out at the item, group, and page
777                 // level which items you've seen and which you haven't. If you're looking
778                 // at the network timeline just mark everything seen. 
779         
780                 $r = q("UPDATE `item` SET `unseen` = 0 
781                         WHERE `unseen` = 1 AND `uid` = %d",
782                         intval($user_info['uid'])
783                 );
784
785
786                 $data = array('$statuses' => $ret);
787                 switch($type){
788                         case "atom":
789                         case "rss":
790                                 $data = api_rss_extra($a, $data, $user_info);
791                                 break;
792                         case "as":
793                                 $as = api_format_as($a, $ret, $user_info);
794                                 $as['title'] = $a->config['sitename']." Home Timeline";
795                                 $as['link']['url'] = $a->get_baseurl()."/".$user_info["screen_name"]."/all";
796                                 return($as);
797                                 break;
798                 }
799
800                 return  api_apply_template("timeline", $type, $data);
801         }
802         api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true);
803         api_register_func('api/statuses/friends_timeline','api_statuses_home_timeline', true);
804
805         function api_statuses_public_timeline(&$a, $type){
806                 if (api_user()===false) return false;
807
808                 $user_info = api_get_user($a);
809                 // get last newtork messages
810
811
812                 // params
813                 $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
814                 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
815                 if ($page<0) $page=0;
816                 $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
817                 $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0);
818                 //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
819
820                 $start = $page*$count;
821
822                 //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false);
823
824                 if ($max_id > 0)
825                         $sql_extra = 'AND `item`.`id` <= '.intval($max_id);
826
827                 /*$r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
828                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
829                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
830                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
831                         FROM `item`, `contact`
832                         WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
833                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' 
834                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' 
835                         AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `user`.`hidewall` = 0
836                         AND `contact`.`id` = `item`.`contact-id`
837                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
838                         $sql_extra
839                         AND `item`.`id`>%d
840                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
841                         intval($since_id),
842                         intval($start), intval($count)
843                 );*/
844                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
845                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
846                         `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
847                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
848                         `user`.`nickname`, `user`.`hidewall`
849                         FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
850                         LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
851                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
852                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
853                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
854                         AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `user`.`hidewall` = 0
855                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
856                         $sql_extra
857                         AND `item`.`id`>%d
858                         ORDER BY `received` DESC LIMIT %d, %d ",
859                         intval($since_id),
860                         intval($start),
861                         intval($count));
862
863                 $ret = api_format_items($r,$user_info);
864
865
866                 $data = array('$statuses' => $ret);
867                 switch($type){
868                         case "atom":
869                         case "rss":
870                                 $data = api_rss_extra($a, $data, $user_info);
871                                 break;
872                         case "as":
873                                 $as = api_format_as($a, $ret, $user_info);
874                                 $as['title'] = $a->config['sitename']." Public Timeline";
875                                 $as['link']['url'] = $a->get_baseurl()."/";
876                                 return($as);
877                                 break;
878                 }
879
880                 return  api_apply_template("timeline", $type, $data);
881         }
882         api_register_func('api/statuses/public_timeline','api_statuses_public_timeline', true);
883
884         /**
885          * 
886          */
887         function api_statuses_show(&$a, $type){
888                 if (api_user()===false) return false;
889
890                 $user_info = api_get_user($a);
891
892                 // params
893                 $id = intval($a->argv[3]);
894
895                 logger('API: api_statuses_show: '.$id);
896
897                 //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false);
898                 $conversation = (x($_REQUEST,'conversation')?1:0);
899
900                 $sql_extra = '';
901                 if ($conversation)
902                         $sql_extra .= " AND `item`.`parent` = %d  ORDER BY `received` ASC ";
903                 else
904                         $sql_extra .= " AND `item`.`id` = %d";
905
906                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
907                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
908                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
909                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
910                         FROM `item`, `contact`
911                         WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
912                         AND `contact`.`id` = `item`.`contact-id`
913                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
914                         $sql_extra",
915                         intval($id)
916                 );
917
918                 $ret = api_format_items($r,$user_info);
919
920                 if ($conversation) {
921                         $data = array('$statuses' => $ret);
922                         return api_apply_template("timeline", $type, $data);
923                 } else {
924                         $data = array('$status' => $ret[0]);
925                         /*switch($type){
926                                 case "atom":
927                                 case "rss":
928                                         $data = api_rss_extra($a, $data, $user_info);
929                         }*/
930                         return  api_apply_template("status", $type, $data);
931                 }
932         }
933         api_register_func('api/statuses/show','api_statuses_show', true);
934
935
936         /**
937          * 
938          */
939         function api_statuses_repeat(&$a, $type){
940                 if (api_user()===false) return false;
941
942                 $user_info = api_get_user($a);
943
944                 // params
945                 $id = intval($a->argv[3]);
946
947                 logger('API: api_statuses_repeat: '.$id);
948
949                 //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false);
950
951                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `contact`.`nick` as `reply_author`,
952                         `contact`.`name`, `contact`.`photo` as `reply_photo`, `contact`.`url` as `reply_url`, `contact`.`rel`,
953                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
954                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
955                         FROM `item`, `contact`
956                         WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
957                         AND `contact`.`id` = `item`.`contact-id`
958                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
959                         $sql_extra
960                         AND `item`.`id`=%d",
961                         intval($id)
962                 );
963
964                 if ($r[0]['body'] != "") {
965                         if (intval(get_config('system','new_share'))) {
966                                 $post = "[share author='".str_replace("'", "&#039;", $r[0]['reply_author']).
967                                                 "' profile='".$r[0]['reply_url'].
968                                                 "' avatar='".$r[0]['reply_photo'].
969                                                 "' link='".$r[0]['plink']."']";
970
971                                 $post .= $r[0]['body'];
972                                 $post .= "[/share]";
973                                 $_REQUEST['body'] = $post;
974                         } else
975                                 $_REQUEST['body'] = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8')."[url=".$r[0]['reply_url']."]".$r[0]['reply_author']."[/url] \n".$r[0]['body'];
976
977                         $_REQUEST['profile_uid'] = api_user();
978                         $_REQUEST['type'] = 'wall';
979                         $_REQUEST['api_source'] = true;
980
981                         require_once('mod/item.php');
982                         item_post($a);
983                 }
984
985                 if ($type == 'xml')
986                         $ok = "true";
987                 else
988                         $ok = "ok";
989
990                 return api_apply_template('test', $type, array('$ok' => $ok));
991         }
992         api_register_func('api/statuses/retweet','api_statuses_repeat', true);
993
994         /**
995          * 
996          */
997         function api_statuses_destroy(&$a, $type){
998                 if (api_user()===false) return false;
999
1000                 $user_info = api_get_user($a);
1001
1002                 // params
1003                 $id = intval($a->argv[3]);
1004
1005                 logger('API: api_statuses_destroy: '.$id);
1006
1007                 require_once('include/items.php');
1008                 drop_item($id, false);
1009
1010                 if ($type == 'xml')
1011                         $ok = "true";
1012                 else
1013                         $ok = "ok";
1014
1015                 return api_apply_template('test', $type, array('$ok' => $ok));
1016         }
1017         api_register_func('api/statuses/destroy','api_statuses_destroy', true);
1018
1019         /**
1020          * 
1021          * http://developer.twitter.com/doc/get/statuses/mentions
1022          * 
1023          */
1024         function api_statuses_mentions(&$a, $type){
1025                 if (api_user()===false) return false;
1026                                 
1027                 $user_info = api_get_user($a);
1028                 // get last newtork messages
1029
1030
1031                 // params
1032                 $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
1033                 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
1034                 if ($page<0) $page=0;
1035                 $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
1036                 $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0);
1037                 //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
1038
1039                 $start = $page*$count;
1040
1041                 //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false);
1042
1043                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
1044                 $myurl = substr($myurl,strpos($myurl,'://')+3);
1045                 //$myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
1046                 $myurl = str_replace('www.','',$myurl);
1047                 $diasp_url = str_replace('/profile/','/u/',$myurl);
1048
1049 /*              if (get_config('system','use_fulltext_engine'))
1050                         $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where (MATCH(`author-link`) AGAINST ('".'"%s"'."' in boolean mode) or MATCH(`tag`) AGAINST ('".'"%s"'."' in boolean mode) or MATCH(tag) AGAINST ('".'"%s"'."' in boolean mode))) ",
1051                                 dbesc(protect_sprintf($myurl)),
1052                                 dbesc(protect_sprintf($myurl)),
1053                                 dbesc(protect_sprintf($diasp_url))
1054                         );
1055                 else
1056                         $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where ( `author-link` like '%s' or `tag` like '%s' or tag like '%s' )) ",
1057                                 dbesc(protect_sprintf('%' . $myurl)),
1058                                 dbesc(protect_sprintf('%' . $myurl . ']%')),
1059                                 dbesc(protect_sprintf('%' . $diasp_url . ']%'))
1060                         );
1061 */
1062                 $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where `author-link` IN ('https://%s', 'http://%s') OR `mention`)",
1063                         dbesc(protect_sprintf($myurl)),
1064                         dbesc(protect_sprintf($myurl))
1065                 );
1066
1067                 if ($max_id > 0)
1068                         $sql_extra .= ' AND `item`.`id` <= '.intval($max_id);
1069
1070                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
1071                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
1072                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
1073                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1074                         FROM `item`, `contact`
1075                         WHERE `item`.`uid` = %d
1076                         AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
1077                         AND `contact`.`id` = `item`.`contact-id`
1078                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1079                         $sql_extra
1080                         AND `item`.`id`>%d
1081                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
1082                         intval($user_info['uid']),
1083                         intval($since_id),
1084                         intval($start), intval($count)
1085                 );
1086
1087                 $ret = api_format_items($r,$user_info);
1088
1089
1090                 $data = array('$statuses' => $ret);
1091                 switch($type){
1092                         case "atom":
1093                         case "rss":
1094                                 $data = api_rss_extra($a, $data, $user_info);
1095                                 break;
1096                         case "as":
1097                                 $as = api_format_as($a, $ret, $user_info);
1098                                 $as["title"] = $a->config['sitename']." Mentions";
1099                                 $as['link']['url'] = $a->get_baseurl()."/";
1100                                 return($as);
1101                                 break;
1102                 }
1103
1104                 return  api_apply_template("timeline", $type, $data);
1105         }
1106         api_register_func('api/statuses/mentions','api_statuses_mentions', true);
1107         api_register_func('api/statuses/replies','api_statuses_mentions', true);
1108
1109
1110         function api_statuses_user_timeline(&$a, $type){
1111                 if (api_user()===false) return false;
1112                 
1113                 $user_info = api_get_user($a);
1114                 // get last newtork messages
1115
1116
1117                 logger("api_statuses_user_timeline: api_user: ". api_user() .
1118                            "\nuser_info: ".print_r($user_info, true) .
1119                            "\n_REQUEST:  ".print_r($_REQUEST, true),
1120                            LOGGER_DEBUG);
1121
1122                 // params
1123                 $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
1124                 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
1125                 if ($page<0) $page=0;
1126                 $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
1127                 $exclude_replies = (x($_REQUEST,'exclude_replies')?1:0);
1128                 //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
1129                 
1130                 $start = $page*$count;
1131
1132                 $sql_extra = '';
1133                 if ($user_info['self']==1) $sql_extra .= " AND `item`.`wall` = 1 ";
1134                 if ($exclude_replies > 0)  $sql_extra .= ' AND `item`.`parent` = `item`.`id`';
1135
1136                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
1137                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
1138                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
1139                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1140                         FROM `item`, `contact`
1141                         WHERE `item`.`uid` = %d
1142                         AND `item`.`contact-id` = %d
1143                         AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
1144                         AND `contact`.`id` = `item`.`contact-id`
1145                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1146                         $sql_extra
1147                         AND `item`.`id`>%d
1148                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
1149                         intval(api_user()),
1150                         intval($user_info['id']),
1151                         intval($since_id),
1152                         intval($start), intval($count)
1153                 );
1154
1155                 $ret = api_format_items($r,$user_info);
1156
1157
1158                 $data = array('$statuses' => $ret);
1159                 switch($type){
1160                         case "atom":
1161                         case "rss":
1162                                 $data = api_rss_extra($a, $data, $user_info);
1163                 }
1164
1165                 return  api_apply_template("timeline", $type, $data);
1166         }
1167
1168         api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
1169
1170
1171         function api_favorites(&$a, $type){
1172                 if (api_user()===false) return false;
1173
1174                 $user_info = api_get_user($a);
1175                 // in friendica starred item are private
1176                 // return favorites only for self
1177                 logger('api_favorites: self:' . $user_info['self']);
1178
1179                 if ($user_info['self']==0) {
1180                         $ret = array();
1181                 } else {
1182
1183
1184                         // params
1185                         $count = (x($_GET,'count')?$_GET['count']:20);
1186                         $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
1187                         if ($page<0) $page=0;
1188
1189                         $start = $page*$count;
1190
1191                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
1192                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
1193                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
1194                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1195                                 FROM `item`, `contact`
1196                                 WHERE `item`.`uid` = %d
1197                                 AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
1198                                 AND `item`.`starred` = 1
1199                                 AND `contact`.`id` = `item`.`contact-id`
1200                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1201                                 $sql_extra
1202                                 ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
1203                                 intval($user_info['uid']),
1204                                 intval($start), intval($count)
1205                         );
1206
1207                         $ret = api_format_items($r,$user_info);
1208
1209                 }
1210
1211                 $data = array('$statuses' => $ret);
1212                 switch($type){
1213                         case "atom":
1214                         case "rss":
1215                                 $data = api_rss_extra($a, $data, $user_info);
1216                 }
1217
1218                 return  api_apply_template("timeline", $type, $data);
1219         }
1220
1221         api_register_func('api/favorites','api_favorites', true);
1222
1223         function api_format_as($a, $ret, $user_info) {
1224
1225                 $as = array();
1226                 $as['title'] = $a->config['sitename']." Public Timeline";
1227                 $items = array();
1228                 foreach ($ret as $item) {
1229                         $singleitem["actor"]["displayName"] = $item["user"]["name"];
1230                         $singleitem["actor"]["id"] = $item["user"]["contact_url"];
1231                         $avatar[0]["url"] = $item["user"]["profile_image_url"];
1232                         $avatar[0]["rel"] = "avatar";
1233                         $avatar[0]["type"] = "";
1234                         $avatar[0]["width"] = 96;
1235                         $avatar[0]["height"] = 96;
1236                         $avatar[1]["url"] = $item["user"]["profile_image_url"];
1237                         $avatar[1]["rel"] = "avatar";
1238                         $avatar[1]["type"] = "";
1239                         $avatar[1]["width"] = 48;
1240                         $avatar[1]["height"] = 48;
1241                         $avatar[2]["url"] = $item["user"]["profile_image_url"];
1242                         $avatar[2]["rel"] = "avatar";
1243                         $avatar[2]["type"] = "";
1244                         $avatar[2]["width"] = 24;
1245                         $avatar[2]["height"] = 24;
1246                         $singleitem["actor"]["avatarLinks"] = $avatar;
1247
1248                         $singleitem["actor"]["image"]["url"] = $item["user"]["profile_image_url"];
1249                         $singleitem["actor"]["image"]["rel"] = "avatar";
1250                         $singleitem["actor"]["image"]["type"] = "";
1251                         $singleitem["actor"]["image"]["width"] = 96;
1252                         $singleitem["actor"]["image"]["height"] = 96;
1253                         $singleitem["actor"]["type"] = "person";
1254                         $singleitem["actor"]["url"] = $item["person"]["contact_url"];
1255                         $singleitem["actor"]["statusnet:profile_info"]["local_id"] = $item["user"]["id"];
1256                         $singleitem["actor"]["statusnet:profile_info"]["following"] = $item["user"]["following"] ? "true" : "false";
1257                         $singleitem["actor"]["statusnet:profile_info"]["blocking"] = "false";
1258                         $singleitem["actor"]["contact"]["preferredUsername"] = $item["user"]["screen_name"];
1259                         $singleitem["actor"]["contact"]["displayName"] = $item["user"]["name"];
1260                         $singleitem["actor"]["contact"]["addresses"] = "";
1261
1262                         $singleitem["body"] = $item["text"];
1263                         $singleitem["object"]["displayName"] = $item["text"];
1264                         $singleitem["object"]["id"] = $item["url"];
1265                         $singleitem["object"]["type"] = "note";
1266                         $singleitem["object"]["url"] = $item["url"];
1267                         //$singleitem["context"] =;
1268                         $singleitem["postedTime"] = date("c", strtotime($item["published"]));
1269                         $singleitem["provider"]["objectType"] = "service";
1270                         $singleitem["provider"]["displayName"] = "Test";
1271                         $singleitem["provider"]["url"] = "http://test.tld";
1272                         $singleitem["title"] = $item["text"];
1273                         $singleitem["verb"] = "post";
1274                         $singleitem["statusnet:notice_info"]["local_id"] = $item["id"];
1275                                 $singleitem["statusnet:notice_info"]["source"] = $item["source"];
1276                                 $singleitem["statusnet:notice_info"]["favorite"] = "false";
1277                                 $singleitem["statusnet:notice_info"]["repeated"] = "false";
1278                                 //$singleitem["original"] = $item;
1279                                 $items[] = $singleitem;
1280                 }
1281                 $as['items'] = $items;
1282                 $as['link']['url'] = $a->get_baseurl()."/".$user_info["screen_name"]."/all";
1283                 $as['link']['rel'] = "alternate";
1284                 $as['link']['type'] = "text/html";
1285                 return($as);
1286         }
1287
1288         function api_format_messages($item, $recipient, $sender) {
1289                 // standard meta information
1290                 $ret=Array(
1291                                 'id'                    => $item['id'],
1292                                 'created_at'            => api_date($item['created']),
1293                                 'sender_id'             => $sender['id'] ,
1294                                 'sender_screen_name'    => $sender['screen_name'],
1295                                 'sender'                => $sender,
1296                                 'recipient_id'          => $recipient['id'],
1297                                 'recipient_screen_name' => $recipient['screen_name'],
1298                                 'recipient'             => $recipient,
1299                 );
1300
1301                 //don't send title to regular StatusNET requests to avoid confusing these apps
1302                 if (x($_GET, 'getText')) {
1303                         $ret['title'] = $item['title'] ;
1304                         if ($_GET["getText"] == "html") {
1305                                 $ret['text'] = bbcode($item['body']);
1306                         }
1307                         elseif ($_GET["getText"] == "plain") {
1308                                 $ret['text'] = html2plain(bbcode($item['body'], false, false, true), 0);
1309                         }
1310                 }
1311                 else {
1312                         $ret['text'] = $item['title']."\n".html2plain(bbcode($item['body'], false, false, true), 0);
1313                 }
1314                 if (isset($_GET["getUserObjects"]) && $_GET["getUserObjects"] == "false") {
1315                         unset($ret['sender']);
1316                         unset($ret['recipient']);
1317                 }
1318
1319                 return $ret;
1320         }
1321
1322         function api_format_items($r,$user_info) {
1323
1324                 //logger('api_format_items: ' . print_r($r,true));
1325
1326                 //logger('api_format_items: ' . print_r($user_info,true));
1327
1328                 $a = get_app();
1329                 $ret = Array();
1330
1331                 foreach($r as $item) {
1332                         localize_item($item);
1333                         $status_user = (($item['cid']==$user_info['id'])?$user_info: api_item_get_user($a,$item));
1334
1335                         if ($item['parent']!=$item['id']) {
1336                                 $r = q("select id from item where parent=%s and id<%s order by id desc limit 1",
1337                                         intval($item['parent']), intval($item['id']));
1338                                 if ($r)
1339                                         $in_reply_to_status_id = $r[0]['id'];
1340                                 else
1341                                         $in_reply_to_status_id = $item['parent'];
1342
1343                                 $r = q("select `item`.`contact-id`, `contact`.nick, `item`.`author-name` from item, contact
1344                                         where `contact`.`id` = `item`.`contact-id` and `item`.id=%d", intval($in_reply_to_status_id));
1345
1346                                 $in_reply_to_screen_name = $r[0]['author-name'];
1347                                 $in_reply_to_user_id = $r[0]['contact-id'];
1348
1349                         } else {
1350                                 $in_reply_to_screen_name = '';
1351                                 $in_reply_to_user_id = 0;
1352                                 $in_reply_to_status_id = 0;
1353                         }
1354
1355                         // Workaround for ostatus messages where the title is identically to the body
1356                         $statusbody = trim(html2plain(bbcode($item['body'], false, false, true), 0));
1357                         $statustitle = trim($item['title']);
1358
1359                         if (($statustitle != '') and (strpos($statusbody, $statustitle) !== false))
1360                                 $statustext = trim($statusbody);
1361                         else
1362                                 $statustext = trim($statustitle."\n\n".$statusbody);
1363
1364                         if (($item["network"] == NETWORK_FEED) and (strlen($statustext)> 1000))
1365                                 $statustext = substr($statustext, 0, 1000)."... \n".$item["plink"];
1366
1367                         $status = array(
1368                                 'text'          => $statustext,
1369                                 'truncated' => False,
1370                                 'created_at'=> api_date($item['created']),
1371                                 'in_reply_to_status_id' => $in_reply_to_status_id,
1372                                 'source'    => (($item['app']) ? $item['app'] : 'web'),
1373                                 'id'            => intval($item['id']),
1374                                 'in_reply_to_user_id' => $in_reply_to_user_id,
1375                                 'in_reply_to_screen_name' => $in_reply_to_screen_name,
1376                                 'geo' => '',
1377                                 'favorited' => $item['starred'] ? true : false,
1378                                 'user' =>  $status_user ,
1379                                 'statusnet_html'                => trim(bbcode($item['body'])),
1380                                 'statusnet_conversation_id'     => $item['parent'],
1381                         );
1382
1383                         // Seesmic doesn't like the following content
1384                         if ($_SERVER['HTTP_USER_AGENT'] != 'Seesmic') {
1385                                 $status2 = array(
1386                                         'updated'   => api_date($item['edited']),
1387                                         'published' => api_date($item['created']),
1388                                         'message_id' => $item['uri'],
1389                                         'url'           => ($item['plink']!=''?$item['plink']:$item['author-link']),
1390                                         'coordinates' => $item['coord'],
1391                                         'place' => $item['location'],
1392                                         'contributors' => '',
1393                                         'annotations'  => '',
1394                                         'entities'  => '',
1395                                         'objecttype' => (($item['object-type']) ? $item['object-type'] : ACTIVITY_OBJ_NOTE),
1396                                         'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST),
1397                                         'self' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
1398                                         'edit' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
1399                                 );
1400
1401                                 $status = array_merge($status, $status2);
1402                         }
1403
1404                         $ret[]=$status;
1405                 };
1406                 return $ret;
1407         }
1408
1409
1410         function api_account_rate_limit_status(&$a,$type) {
1411
1412                 $hash = array(
1413                           'reset_time_in_seconds' => strtotime('now + 1 hour'),
1414                           'remaining_hits' => (string) 150,
1415                           'hourly_limit' => (string) 150,
1416                           'reset_time' => datetime_convert('UTC','UTC','now + 1 hour',ATOM_TIME),
1417                 );
1418                 if ($type == "xml")
1419                         $hash['resettime_in_seconds'] = $hash['reset_time_in_seconds'];
1420
1421                 return api_apply_template('ratelimit', $type, array('$hash' => $hash));
1422
1423         }
1424         api_register_func('api/account/rate_limit_status','api_account_rate_limit_status',true);
1425
1426         function api_help_test(&$a,$type) {
1427
1428                 if ($type == 'xml')
1429                         $ok = "true";
1430                 else
1431                         $ok = "ok";
1432
1433                 return api_apply_template('test', $type, array('$ok' => $ok));
1434
1435         }
1436         api_register_func('api/help/test','api_help_test',true);
1437
1438         /**
1439          *  https://dev.twitter.com/docs/api/1/get/statuses/friends 
1440          *  This function is deprecated by Twitter
1441          *  returns: json, xml 
1442          **/
1443         function api_statuses_f(&$a, $type, $qtype) {
1444                 if (api_user()===false) return false;
1445                 $user_info = api_get_user($a);
1446                 
1447                 
1448                 // friends and followers only for self
1449                 if ($user_info['self']==0){
1450                         return false;
1451                 }
1452                 
1453                 if (x($_GET,'cursor') && $_GET['cursor']=='undefined'){
1454                         /* this is to stop Hotot to load friends multiple times
1455                         *  I'm not sure if I'm missing return something or
1456                         *  is a bug in hotot. Workaround, meantime
1457                         */
1458                         
1459                         /*$ret=Array();
1460                         return array('$users' => $ret);*/
1461                         return false;
1462                 }
1463                 
1464                 if($qtype == 'friends')
1465                         $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
1466                 if($qtype == 'followers')
1467                         $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
1468  
1469                 $r = q("SELECT id FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra",
1470                         intval(api_user())
1471                 );
1472
1473                 $ret = array();
1474                 foreach($r as $cid){
1475                         $ret[] = api_get_user($a, $cid['id']);
1476                 }
1477
1478                 
1479                 return array('$users' => $ret);
1480
1481         }
1482         function api_statuses_friends(&$a, $type){
1483                 $data =  api_statuses_f($a,$type,"friends");
1484                 if ($data===false) return false;
1485                 return  api_apply_template("friends", $type, $data);
1486         }
1487         function api_statuses_followers(&$a, $type){
1488                 $data = api_statuses_f($a,$type,"followers");
1489                 if ($data===false) return false;
1490                 return  api_apply_template("friends", $type, $data);
1491         }
1492         api_register_func('api/statuses/friends','api_statuses_friends',true);
1493         api_register_func('api/statuses/followers','api_statuses_followers',true);
1494
1495
1496
1497
1498
1499
1500         function api_statusnet_config(&$a,$type) {
1501                 $name = $a->config['sitename'];
1502                 $server = $a->get_hostname();
1503                 $logo = $a->get_baseurl() . '/images/friendica-64.png';
1504                 $email = $a->config['admin_email'];
1505                 $closed = (($a->config['register_policy'] == REGISTER_CLOSED) ? 'true' : 'false');
1506                 $private = (($a->config['system']['block_public']) ? 'true' : 'false');
1507                 $textlimit = (string) (($a->config['max_import_size']) ? $a->config['max_import_size'] : 200000);
1508                 if($a->config['api_import_size'])
1509                         $texlimit = string($a->config['api_import_size']);
1510                 $ssl = (($a->config['system']['have_ssl']) ? 'true' : 'false');
1511                 $sslserver = (($ssl === 'true') ? str_replace('http:','https:',$a->get_baseurl()) : '');
1512
1513                 $config = array(
1514                         'site' => array('name' => $name,'server' => $server, 'theme' => 'default', 'path' => '',
1515                                 'logo' => $logo, 'fancy' => 'true', 'language' => 'en', 'email' => $email, 'broughtby' => '',
1516                                 'broughtbyurl' => '', 'timezone' => 'UTC', 'closed' => $closed, 'inviteonly' => 'false',
1517                                 'private' => $private, 'textlimit' => $textlimit, 'sslserver' => $sslserver, 'ssl' => $ssl,
1518                                 'shorturllength' => '30',
1519         'friendica' => array(
1520                              'FRIENDICA_PLATFORM' => FRIENDICA_PLATFORM,
1521                              'FRIENDICA_VERSION' => FRIENDICA_VERSION,
1522                              'DFRN_PROTOCOL_VERSION' => DFRN_PROTOCOL_VERSION,
1523                              'DB_UPDATE_VERSION' => DB_UPDATE_VERSION
1524                              )
1525                         ),
1526                 );  
1527
1528                 return api_apply_template('config', $type, array('$config' => $config));
1529
1530         }
1531         api_register_func('api/statusnet/config','api_statusnet_config',false);
1532
1533         function api_statusnet_version(&$a,$type) {
1534
1535                 // liar
1536
1537                 if($type === 'xml') {
1538                         header("Content-type: application/xml");
1539                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>0.9.7</version>' . "\r\n";
1540                         killme();
1541                 }
1542                 elseif($type === 'json') {
1543                         header("Content-type: application/json");
1544                         echo '"0.9.7"';
1545                         killme();
1546                 }
1547         }
1548         api_register_func('api/statusnet/version','api_statusnet_version',false);
1549
1550
1551         function api_ff_ids(&$a,$type,$qtype) {
1552                 if(! api_user())
1553                         return false;
1554
1555                 if($qtype == 'friends')
1556                         $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
1557                 if($qtype == 'followers')
1558                         $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
1559  
1560
1561                 $r = q("SELECT id FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra",
1562                         intval(api_user())
1563                 );
1564
1565                 if(is_array($r)) {
1566                         if($type === 'xml') {
1567                                 header("Content-type: application/xml");
1568                                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<ids>' . "\r\n";
1569                                 foreach($r as $rr)
1570                                         echo '<id>' . $rr['id'] . '</id>' . "\r\n";
1571                                 echo '</ids>' . "\r\n";
1572                                 killme();
1573                         }
1574                         elseif($type === 'json') {
1575                                 $ret = array();
1576                                 header("Content-type: application/json");
1577                                 foreach($r as $rr) $ret[] = $rr['id'];
1578                                 echo json_encode($ret);
1579                                 killme();
1580                         }
1581                 }
1582         }
1583
1584         function api_friends_ids(&$a,$type) {
1585                 api_ff_ids($a,$type,'friends');
1586         }
1587         function api_followers_ids(&$a,$type) {
1588                 api_ff_ids($a,$type,'followers');
1589         }
1590         api_register_func('api/friends/ids','api_friends_ids',true);
1591         api_register_func('api/followers/ids','api_followers_ids',true);
1592
1593
1594         function api_direct_messages_new(&$a, $type) {
1595                 if (api_user()===false) return false;
1596                 
1597                 if (!x($_POST, "text") || !x($_POST,"screen_name")) return;
1598
1599                 $sender = api_get_user($a);
1600                 
1601                 require_once("include/message.php");
1602
1603                 $r = q("SELECT `id` FROM `contact` WHERE `uid`=%d AND `nick`='%s'",
1604                                 intval(api_user()),
1605                                 dbesc($_POST['screen_name']));
1606
1607                 $recipient = api_get_user($a, $r[0]['id']);                     
1608                 $replyto = '';
1609                 $sub     = '';
1610                 if (x($_REQUEST,'replyto')) {
1611                         $r = q('SELECT `parent-uri`, `title` FROM `mail` WHERE `uid`=%d AND `id`=%d',
1612                                         intval(api_user()),
1613                                         intval($_REQUEST['replyto']));
1614                         $replyto = $r[0]['parent-uri'];
1615                         $sub     = $r[0]['title'];
1616                 }
1617                 else {
1618                         if (x($_REQUEST,'title')) {
1619                                 $sub = $_REQUEST['title'];
1620                         }
1621                         else {
1622                                 $sub = ((strlen($_POST['text'])>10)?substr($_POST['text'],0,10)."...":$_POST['text']);
1623                         }
1624                 }
1625
1626                 $id = send_message($recipient['id'], $_POST['text'], $sub, $replyto);
1627
1628                 if ($id>-1) {
1629                         $r = q("SELECT * FROM `mail` WHERE id=%d", intval($id));
1630                         $ret = api_format_messages($r[0], $recipient, $sender);
1631                 
1632                 } else {
1633                         $ret = array("error"=>$id);     
1634                 }
1635                 
1636                 $data = Array('$messages'=>$ret);
1637                 
1638                 switch($type){
1639                         case "atom":
1640                         case "rss":
1641                                 $data = api_rss_extra($a, $data, $user_info);
1642                 }
1643                                 
1644                 return  api_apply_template("direct_messages", $type, $data);
1645                                 
1646         }
1647         api_register_func('api/direct_messages/new','api_direct_messages_new',true);
1648
1649         function api_direct_messages_box(&$a, $type, $box) {
1650                 if (api_user()===false) return false;
1651                 
1652                 $user_info = api_get_user($a);
1653                 
1654                 // params
1655                 $count = (x($_GET,'count')?$_GET['count']:20);
1656                 $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
1657                 if ($page<0) $page=0;
1658                 
1659                 $start = $page*$count;
1660                 
1661                 $profile_url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
1662                 if ($box=="sentbox") {
1663                         $sql_extra = "`from-url`='".dbesc( $profile_url )."'";
1664                 }
1665                 elseif ($box=="conversation") {
1666                         $sql_extra = "`parent-uri`='".dbesc( $_GET["uri"] )  ."'";
1667                 }
1668                 elseif ($box=="all") {
1669                         $sql_extra = "true";
1670                 }
1671                 elseif ($box=="inbox") {
1672                         $sql_extra = "`from-url`!='".dbesc( $profile_url )."'";
1673                 }
1674                 
1675                 $r = q("SELECT * FROM `mail` WHERE uid=%d AND $sql_extra ORDER BY created DESC LIMIT %d,%d",
1676                                 intval(api_user()),
1677                                 intval($start), intval($count)
1678                 );
1679                 
1680                 $ret = Array();
1681                 foreach($r as $item) {
1682                         if ($box == "inbox" || $item['from-url'] != $profile_url){
1683                                 $recipient = $user_info;
1684                                 $sender = api_get_user($a,$item['contact-id']);
1685                         }
1686                         elseif ($box == "sentbox" || $item['from-url'] != $profile_url){
1687                                 $recipient = api_get_user($a,$item['contact-id']);
1688                                 $sender = $user_info;
1689                         }
1690
1691                         $ret[]=api_format_messages($item, $recipient, $sender);
1692                 }
1693                 
1694
1695                 $data = array('$messages' => $ret);
1696                 switch($type){
1697                         case "atom":
1698                         case "rss":
1699                                 $data = api_rss_extra($a, $data, $user_info);
1700                 }
1701                                 
1702                 return  api_apply_template("direct_messages", $type, $data);
1703                 
1704         }
1705
1706         function api_direct_messages_sentbox(&$a, $type){
1707                 return api_direct_messages_box($a, $type, "sentbox");
1708         }
1709         function api_direct_messages_inbox(&$a, $type){
1710                 return api_direct_messages_box($a, $type, "inbox");
1711         }
1712         function api_direct_messages_all(&$a, $type){
1713                 return api_direct_messages_box($a, $type, "all");
1714         }
1715         function api_direct_messages_conversation(&$a, $type){
1716                 return api_direct_messages_box($a, $type, "conversation");
1717         }
1718         api_register_func('api/direct_messages/conversation','api_direct_messages_conversation',true);
1719         api_register_func('api/direct_messages/all','api_direct_messages_all',true);
1720         api_register_func('api/direct_messages/sent','api_direct_messages_sentbox',true);
1721         api_register_func('api/direct_messages','api_direct_messages_inbox',true);
1722
1723
1724
1725         function api_oauth_request_token(&$a, $type){
1726                 try{
1727                         $oauth = new FKOAuth1();
1728                         $r = $oauth->fetch_request_token(OAuthRequest::from_request());
1729                 }catch(Exception $e){
1730                         echo "error=". OAuthUtil::urlencode_rfc3986($e->getMessage()); killme();
1731                 }
1732                 echo $r;
1733                 killme();       
1734         }
1735         function api_oauth_access_token(&$a, $type){
1736                 try{
1737                         $oauth = new FKOAuth1();
1738                         $r = $oauth->fetch_access_token(OAuthRequest::from_request());
1739                 }catch(Exception $e){
1740                         echo "error=". OAuthUtil::urlencode_rfc3986($e->getMessage()); killme();
1741                 }
1742                 echo $r;
1743                 killme();                       
1744         }
1745
1746         api_register_func('api/oauth/request_token', 'api_oauth_request_token', false);
1747         api_register_func('api/oauth/access_token', 'api_oauth_access_token', false);
1748
1749 /*
1750 Not implemented by now:
1751 favorites
1752 favorites/create
1753 favorites/destroy
1754 statuses/retweets_of_me
1755 friendships/create
1756 friendships/destroy
1757 friendships/exists
1758 friendships/show
1759 account/update_location
1760 account/update_profile_background_image
1761 account/update_profile_image
1762 blocks/create
1763 blocks/destroy
1764
1765 Not implemented in status.net:
1766 statuses/retweeted_to_me
1767 statuses/retweeted_by_me
1768 direct_messages/destroy
1769 account/end_session
1770 account/update_delivery_device
1771 notifications/follow
1772 notifications/leave
1773 blocks/exists
1774 blocks/blocking
1775 lists
1776 */
1777