]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/api.php
2e9cc6618deaec19c6e47ef39f6859decc7d73f7
[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('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
31     function handle($args)
32     {
33         parent::handle($args);
34
35         $this->api_action = $this->arg('apiaction');
36         $method = $this->arg('method');
37         $argument = $this->arg('argument');
38
39         if (isset($argument)) {
40             $cmdext = explode('.', $argument);
41             $this->api_arg =  $cmdext[0];
42             $this->api_method = $method;
43             $this->content_type = strtolower($cmdext[1]);
44         } else {
45
46             # Requested format / content-type will be an extension on the method
47             $cmdext = explode('.', $method);
48             $this->api_method = $cmdext[0];
49             $this->content_type = strtolower($cmdext[1]);
50         }
51
52         if ($this->requires_auth()) {
53             if (!isset($_SERVER['PHP_AUTH_USER'])) {
54
55                 # This header makes basic auth go
56                 header('WWW-Authenticate: Basic realm="StatusNet API"');
57
58                 # If the user hits cancel -- bam!
59                 $this->show_basic_auth_error();
60             } else {
61                 $nickname = $_SERVER['PHP_AUTH_USER'];
62                 $password = $_SERVER['PHP_AUTH_PW'];
63                 $user = common_check_user($nickname, $password);
64
65                 if ($user) {
66                     $this->user = $user;
67                     $this->process_command();
68                 } else {
69                     # basic authentication failed
70                     list($proxy, $ip) = common_client_ip();
71
72                     common_log(LOG_WARNING, "Failed API auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
73                     $this->show_basic_auth_error();
74                 }
75             }
76         } else {
77
78             // Caller might give us a username even if not required
79             if (isset($_SERVER['PHP_AUTH_USER'])) {
80                 $user = User::staticGet('nickname', $_SERVER['PHP_AUTH_USER']);
81                 if ($user) {
82                     $this->user = $user;
83                 }
84                 # Twitter doesn't throw an error if the user isn't found
85             }
86
87             $this->process_command();
88         }
89     }
90
91     function process_command()
92     {
93         $action = "twitapi$this->api_action";
94         $actionfile = INSTALLDIR."/actions/$action.php";
95
96         if (file_exists($actionfile)) {
97             require_once($actionfile);
98             $action_class = ucfirst($action)."Action";
99             $action_obj = new $action_class();
100
101             if (!$action_obj->prepare($this->args)) {
102                 return;
103             }
104
105             if (method_exists($action_obj, $this->api_method)) {
106                 $apidata = array(    'content-type' => $this->content_type,
107                                     'api_method' => $this->api_method,
108                                     'api_arg' => $this->api_arg,
109                                     'user' => $this->user);
110
111                 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
112             } else {
113                 $this->clientError("API method not found!", $code=404);
114             }
115         } else {
116             $this->clientError("API method not found!", $code=404);
117         }
118     }
119
120     // Whitelist of API methods that don't need authentication
121     function requires_auth()
122     {
123         static $noauth = array( 'statuses/public_timeline',
124                                 'statuses/show',
125                                 'users/show',
126                                 'help/test',
127                                 'help/downtime_schedule',
128                                 'laconica/version',
129                                 'laconica/config',
130                                 'laconica/wadl',
131                                 'tags/timeline',
132                                 'oembed/oembed',
133                                 'groups/show',
134                                 'groups/timeline',
135                                 'groups/list_all',
136                                 'groups/timeline');
137
138         static $bareauth = array('statuses/user_timeline',
139                                  'statuses/friends_timeline',
140                                  'statuses/friends',
141                                  'statuses/replies',
142                                  'statuses/mentions',
143                                  'statuses/followers',
144                                  'favorites/favorites',
145                                  'friendships/show',
146                                  'groups/list_groups');
147
148         $fullname = "$this->api_action/$this->api_method";
149
150         // If the site is "private", all API methods except laconica/config
151         // need authentication
152
153         if (common_config('site', 'private')) {
154             return $fullname != 'laconica/config' || false;
155         }
156
157         // bareauth: only needs auth if without an argument or query param specifying user
158
159         if (in_array($fullname, $bareauth)) {
160
161             // Special case: friendships/show only needs auth if source_id or
162             // source_screen_name is not specified as a param
163
164             if ($fullname == 'friendships/show') {
165
166                 $source_id          = $this->arg('source_id');
167                 $source_screen_name = $this->arg('source_screen_name');
168
169                 if (empty($source_id) && empty($source_screen_name)) {
170                     return true;
171                 }
172
173                 return false;
174             }
175
176             // if all of these are empty, auth is required
177
178             $id          = $this->arg('id');
179             $user_id     = $this->arg('user_id');
180             $screen_name = $this->arg('screen_name');
181
182             if (empty($this->api_arg) &&
183                 empty($id)            &&
184                 empty($user_id)       &&
185                 empty($screen_name)) {
186                 return true;
187             } else {
188                 return false;
189             }
190
191         } else if (in_array($fullname, $noauth)) {
192
193             // noauth: never needs auth
194
195             return false;
196         } else {
197
198             // everybody else needs auth
199
200             return true;
201         }
202     }
203
204     function show_basic_auth_error()
205     {
206         header('HTTP/1.1 401 Unauthorized');
207         $msg = 'Could not authenticate you.';
208
209         if ($this->content_type == 'xml') {
210             header('Content-Type: application/xml; charset=utf-8');
211             $this->startXML();
212             $this->elementStart('hash');
213             $this->element('error', null, $msg);
214             $this->element('request', null, $_SERVER['REQUEST_URI']);
215             $this->elementEnd('hash');
216             $this->endXML();
217         } else if ($this->content_type == 'json')  {
218             header('Content-Type: application/json; charset=utf-8');
219             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
220             print(json_encode($error_array));
221         } else {
222             header('Content-type: text/plain');
223             print "$msg\n";
224         }
225     }
226
227     function isReadOnly($args)
228     {
229         $apiaction = $args['apiaction'];
230         $method = $args['method'];
231
232         list($cmdtext, $fmt) = explode('.', $method);
233
234         static $write_methods = array(
235             'account' => array('update_location', 'update_delivery_device', 'end_session'),
236             'blocks' => array('create', 'destroy'),
237             'direct_messages' => array('create', 'destroy'),
238             'favorites' => array('create', 'destroy'),
239             'friendships' => array('create', 'destroy'),
240             'help' => array(),
241             'notifications' => array('follow', 'leave'),
242             'statuses' => array('update', 'destroy'),
243             'users' => array()
244         );
245
246         if (array_key_exists($apiaction, $write_methods)) {
247             if (!in_array($cmdtext, $write_methods[$apiaction])) {
248                 return true;
249             }
250         }
251
252         return false;
253     }
254 }