3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 class ApiAction extends Action
33 function handle($args)
35 parent::handle($args);
37 $this->api_action = $this->arg('apiaction');
38 $method = $this->arg('method');
39 $argument = $this->arg('argument');
40 $this->basic_auth_process_header();
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]);
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]);
55 if ($this->requires_auth()) {
56 if (!isset($this->auth_user)) {
58 # This header makes basic auth go
59 header('WWW-Authenticate: Basic realm="StatusNet API"');
61 # If the user hits cancel -- bam!
62 $this->show_basic_auth_error();
64 $nickname = $this->auth_user;
65 $password = $this->auth_pw;
66 $user = common_check_user($nickname, $password);
70 $this->process_command();
72 # basic authentication failed
73 list($proxy, $ip) = common_client_ip();
75 common_log(LOG_WARNING, "Failed API auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
76 $this->show_basic_auth_error();
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);
87 # Twitter doesn't throw an error if the user isn't found
90 $this->process_command();
94 function process_command()
96 $action = "twitapi$this->api_action";
97 $actionfile = INSTALLDIR."/actions/$action.php";
99 if (file_exists($actionfile)) {
100 require_once($actionfile);
101 $action_class = ucfirst($action)."Action";
102 $action_obj = new $action_class();
104 if (!$action_obj->prepare($this->args)) {
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);
114 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
116 $this->clientError("API method not found!", $code=404);
119 $this->clientError("API method not found!", $code=404);
123 // Whitelist of API methods that don't need authentication
124 function requires_auth()
126 static $noauth = array( 'statuses/public_timeline',
130 'help/downtime_schedule',
143 static $bareauth = array('statuses/user_timeline',
144 'statuses/friends_timeline',
145 'statuses/home_timeline',
149 'statuses/followers',
150 'favorites/favorites',
152 'groups/list_groups');
154 $fullname = "$this->api_action/$this->api_method";
156 // If the site is "private", all API methods except statusnet/config
157 // need authentication
159 if (common_config('site', 'private')) {
160 return $fullname != 'statusnet/config' || false;
163 // bareauth: only needs auth if without an argument or query param specifying user
165 if (in_array($fullname, $bareauth)) {
167 // Special case: friendships/show only needs auth if source_id or
168 // source_screen_name is not specified as a param
170 if ($fullname == 'friendships/show') {
172 $source_id = $this->arg('source_id');
173 $source_screen_name = $this->arg('source_screen_name');
175 if (empty($source_id) && empty($source_screen_name)) {
182 // if all of these are empty, auth is required
184 $id = $this->arg('id');
185 $user_id = $this->arg('user_id');
186 $screen_name = $this->arg('screen_name');
188 if (empty($this->api_arg) &&
191 empty($screen_name)) {
197 } else if (in_array($fullname, $noauth)) {
199 // noauth: never needs auth
204 // everybody else needs auth
210 function basic_auth_process_header()
212 if(isset($_SERVER['AUTHORIZATION']) || isset($_SERVER['HTTP_AUTHORIZATION']))
214 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])?$_SERVER['HTTP_AUTHORIZATION']:$_SERVER['AUTHORIZATION'];
217 if(isset($_SERVER['PHP_AUTH_USER']))
219 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
220 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
222 elseif ( isset($authorization_header) && strstr(substr($authorization_header, 0,5),'Basic') )
224 // decode the HTTP_AUTHORIZATION header on php-cgi server self
225 // on fcgid server the header name is AUTHORIZATION
227 $auth_hash = base64_decode( substr($authorization_header, 6) );
228 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
230 // set all to NULL on a empty basic auth request
231 if($this->auth_user == "") {
232 $this->auth_user = NULL;
233 $this->auth_pw = NULL;
238 $this->auth_user = NULL;
239 $this->auth_pw = NULL;
243 function show_basic_auth_error()
245 header('HTTP/1.1 401 Unauthorized');
246 $msg = 'Could not authenticate you.';
248 if ($this->content_type == 'xml') {
249 header('Content-Type: application/xml; charset=utf-8');
251 $this->elementStart('hash');
252 $this->element('error', null, $msg);
253 $this->element('request', null, $_SERVER['REQUEST_URI']);
254 $this->elementEnd('hash');
256 } else if ($this->content_type == 'json') {
257 header('Content-Type: application/json; charset=utf-8');
258 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
259 print(json_encode($error_array));
261 header('Content-type: text/plain');
266 function isReadOnly($args)
268 $apiaction = $args['apiaction'];
269 $method = $args['method'];
271 list($cmdtext, $fmt) = explode('.', $method);
273 static $write_methods = array(
274 'account' => array('update_location', 'update_delivery_device', 'end_session'),
275 'blocks' => array('create', 'destroy'),
276 'direct_messages' => array('create', 'destroy'),
277 'favorites' => array('create', 'destroy'),
278 'friendships' => array('create', 'destroy'),
280 'notifications' => array('follow', 'leave'),
281 'statuses' => array('update', 'destroy'),
285 if (array_key_exists($apiaction, $write_methods)) {
286 if (!in_array($cmdtext, $write_methods[$apiaction])) {