]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/api.php
Merge branch '0.8.x' of git@gitorious.org:statusnet/mainline into 0.8.x
[quix0rs-gnu-social.git] / actions / api.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 class ApiAction extends Action
23 {
24
25     var $user;
26     var $content_type;
27     var $api_arg;
28     var $api_method;
29     var $api_action;
30     var $auth_user;
31     var $auth_pw;
32
33     function handle($args)
34     {
35         parent::handle($args);
36
37         $this->api_action = $this->arg('apiaction');
38         $method = $this->arg('method');
39         $argument = $this->arg('argument');
40         $this->basic_auth_process_header();
41
42         if (isset($argument)) {
43             $cmdext = explode('.', $argument);
44             $this->api_arg =  $cmdext[0];
45             $this->api_method = $method;
46             $this->content_type = strtolower($cmdext[1]);
47         } else {
48
49             # Requested format / content-type will be an extension on the method
50             $cmdext = explode('.', $method);
51             $this->api_method = $cmdext[0];
52             $this->content_type = strtolower($cmdext[1]);
53         }
54
55         if ($this->requires_auth()) {
56             if (!isset($this->auth_user)) {
57
58                 # This header makes basic auth go
59                 header('WWW-Authenticate: Basic realm="StatusNet API"');
60
61                 # If the user hits cancel -- bam!
62                 $this->show_basic_auth_error();
63             } else {
64                 $nickname = $this->auth_user;
65                 $password = $this->auth_pw;
66                 $user = common_check_user($nickname, $password);
67
68                 if ($user) {
69                     $this->user = $user;
70                     $this->process_command();
71                 } else {
72                     # basic authentication failed
73                     list($proxy, $ip) = common_client_ip();
74
75                     common_log(LOG_WARNING, "Failed API auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
76                     $this->show_basic_auth_error();
77                 }
78             }
79         } else {
80
81             // Caller might give us a username even if not required
82             if (isset($this->auth_user)) {
83                 $user = User::staticGet('nickname', $this->auth_user);
84                 if ($user) {
85                     $this->user = $user;
86                 }
87                 # Twitter doesn't throw an error if the user isn't found
88             }
89
90             $this->process_command();
91         }
92     }
93
94     function process_command()
95     {
96         $action = "twitapi$this->api_action";
97         $actionfile = INSTALLDIR."/actions/$action.php";
98
99         if (file_exists($actionfile)) {
100             require_once($actionfile);
101             $action_class = ucfirst($action)."Action";
102             $action_obj = new $action_class();
103
104             if (!$action_obj->prepare($this->args)) {
105                 return;
106             }
107
108             if (method_exists($action_obj, $this->api_method)) {
109                 $apidata = array(    'content-type' => $this->content_type,
110                                     'api_method' => $this->api_method,
111                                     'api_arg' => $this->api_arg,
112                                     'user' => $this->user);
113
114                 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
115             } else {
116                 $this->clientError("API method not found!", $code=404);
117             }
118         } else {
119             $this->clientError("API method not found!", $code=404);
120         }
121     }
122
123     // Whitelist of API methods that don't need authentication
124     function requires_auth()
125     {
126         static $noauth = array( 'statuses/public_timeline',
127                                 'statuses/show',
128                                 'users/show',
129                                 'help/test',
130                                 'help/downtime_schedule',
131                                 'statusnet/version',
132                                 'statusnet/config',
133                                 'statusnet/wadl',
134                                 'tags/timeline',
135                                 'oembed/oembed',
136                                 'groups/show',
137                                 'groups/timeline',
138                                 'groups/list_all',
139                                 'groups/membership',
140                                 'groups/is_member',
141                                 'groups/timeline');
142
143         static $bareauth = array('statuses/user_timeline',
144                                  'statuses/friends_timeline',
145                                  'statuses/friends',
146                                  'statuses/replies',
147                                  'statuses/mentions',
148                                  'statuses/followers',
149                                  'favorites/favorites',
150                                  'friendships/show',
151                                  'groups/list_groups');
152
153         $fullname = "$this->api_action/$this->api_method";
154
155         // If the site is "private", all API methods except statusnet/config
156         // need authentication
157
158         if (common_config('site', 'private')) {
159             return $fullname != 'statusnet/config' || false;
160         }
161
162         // bareauth: only needs auth if without an argument or query param specifying user
163
164         if (in_array($fullname, $bareauth)) {
165
166             // Special case: friendships/show only needs auth if source_id or
167             // source_screen_name is not specified as a param
168
169             if ($fullname == 'friendships/show') {
170
171                 $source_id          = $this->arg('source_id');
172                 $source_screen_name = $this->arg('source_screen_name');
173
174                 if (empty($source_id) && empty($source_screen_name)) {
175                     return true;
176                 }
177
178                 return false;
179             }
180
181             // if all of these are empty, auth is required
182
183             $id          = $this->arg('id');
184             $user_id     = $this->arg('user_id');
185             $screen_name = $this->arg('screen_name');
186
187             if (empty($this->api_arg) &&
188                 empty($id)            &&
189                 empty($user_id)       &&
190                 empty($screen_name)) {
191                 return true;
192             } else {
193                 return false;
194             }
195
196         } else if (in_array($fullname, $noauth)) {
197
198             // noauth: never needs auth
199
200             return false;
201         } else {
202
203             // everybody else needs auth
204
205             return true;
206         }
207     }
208
209     function basic_auth_process_header()
210     {
211         if(isset($_SERVER['AUTHORIZATION']) || isset($_SERVER['HTTP_AUTHORIZATION']))
212         {
213                 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])?$_SERVER['HTTP_AUTHORIZATION']:$_SERVER['AUTHORIZATION'];
214         }
215
216         if(isset($_SERVER['PHP_AUTH_USER']))
217         {
218                 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
219                 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
220         }
221         elseif ( isset($authorization_header) && strstr(substr($authorization_header, 0,5),'Basic')  )
222         {
223                 // decode the HTTP_AUTHORIZATION header on php-cgi server self
224                 // on fcgid server the header name is AUTHORIZATION
225
226                 $auth_hash = base64_decode( substr($authorization_header, 6) );
227                 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
228
229                 // set all to NULL on a empty basic auth request
230                 if($this->auth_user == "") {
231                         $this->auth_user = NULL;
232                         $this->auth_pw = NULL;
233                 }
234         }
235         else
236         {
237                 $this->auth_user = NULL;
238                 $this->auth_pw = NULL;
239         }
240     }
241
242     function show_basic_auth_error()
243     {
244         header('HTTP/1.1 401 Unauthorized');
245         $msg = 'Could not authenticate you.';
246
247         if ($this->content_type == 'xml') {
248             header('Content-Type: application/xml; charset=utf-8');
249             $this->startXML();
250             $this->elementStart('hash');
251             $this->element('error', null, $msg);
252             $this->element('request', null, $_SERVER['REQUEST_URI']);
253             $this->elementEnd('hash');
254             $this->endXML();
255         } else if ($this->content_type == 'json')  {
256             header('Content-Type: application/json; charset=utf-8');
257             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
258             print(json_encode($error_array));
259         } else {
260             header('Content-type: text/plain');
261             print "$msg\n";
262         }
263     }
264
265     function isReadOnly($args)
266     {
267         $apiaction = $args['apiaction'];
268         $method = $args['method'];
269
270         list($cmdtext, $fmt) = explode('.', $method);
271
272         static $write_methods = array(
273             'account' => array('update_location', 'update_delivery_device', 'end_session'),
274             'blocks' => array('create', 'destroy'),
275             'direct_messages' => array('create', 'destroy'),
276             'favorites' => array('create', 'destroy'),
277             'friendships' => array('create', 'destroy'),
278             'help' => array(),
279             'notifications' => array('follow', 'leave'),
280             'statuses' => array('update', 'destroy'),
281             'users' => array()
282         );
283
284         if (array_key_exists($apiaction, $write_methods)) {
285             if (!in_array($cmdtext, $write_methods[$apiaction])) {
286                 return true;
287             }
288         }
289
290         return false;
291     }
292 }