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',
148 'statuses/followers',
149 'favorites/favorites',
151 'groups/list_groups');
153 $fullname = "$this->api_action/$this->api_method";
155 // If the site is "private", all API methods except statusnet/config
156 // need authentication
158 if (common_config('site', 'private')) {
159 return $fullname != 'statusnet/config' || false;
162 // bareauth: only needs auth if without an argument or query param specifying user
164 if (in_array($fullname, $bareauth)) {
166 // Special case: friendships/show only needs auth if source_id or
167 // source_screen_name is not specified as a param
169 if ($fullname == 'friendships/show') {
171 $source_id = $this->arg('source_id');
172 $source_screen_name = $this->arg('source_screen_name');
174 if (empty($source_id) && empty($source_screen_name)) {
181 // if all of these are empty, auth is required
183 $id = $this->arg('id');
184 $user_id = $this->arg('user_id');
185 $screen_name = $this->arg('screen_name');
187 if (empty($this->api_arg) &&
190 empty($screen_name)) {
196 } else if (in_array($fullname, $noauth)) {
198 // noauth: never needs auth
203 // everybody else needs auth
209 function basic_auth_process_header()
211 if(isset($_SERVER['AUTHORIZATION']) || isset($_SERVER['HTTP_AUTHORIZATION']))
213 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])?$_SERVER['HTTP_AUTHORIZATION']:$_SERVER['AUTHORIZATION'];
216 if(isset($_SERVER['PHP_AUTH_USER']))
218 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
219 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
221 elseif ( isset($authorization_header) && strstr(substr($authorization_header, 0,5),'Basic') )
223 // decode the HTTP_AUTHORIZATION header on php-cgi server self
224 // on fcgid server the header name is AUTHORIZATION
226 $auth_hash = base64_decode( substr($authorization_header, 6) );
227 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
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;
237 $this->auth_user = NULL;
238 $this->auth_pw = NULL;
242 function show_basic_auth_error()
244 header('HTTP/1.1 401 Unauthorized');
245 $msg = 'Could not authenticate you.';
247 if ($this->content_type == 'xml') {
248 header('Content-Type: application/xml; charset=utf-8');
250 $this->elementStart('hash');
251 $this->element('error', null, $msg);
252 $this->element('request', null, $_SERVER['REQUEST_URI']);
253 $this->elementEnd('hash');
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));
260 header('Content-type: text/plain');
265 function isReadOnly($args)
267 $apiaction = $args['apiaction'];
268 $method = $args['method'];
270 list($cmdtext, $fmt) = explode('.', $method);
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'),
279 'notifications' => array('follow', 'leave'),
280 'statuses' => array('update', 'destroy'),
284 if (array_key_exists($apiaction, $write_methods)) {
285 if (!in_array($cmdtext, $write_methods[$apiaction])) {