]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/api.php
6859cd68cbb827d24b538a4078f8390063527e14
[quix0rs-gnu-social.git] / actions / api.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Control Yourself, 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="Laconica 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                     common_log(LOG_WARNING, "Failed API auth attempt, nickname: $nickname.");
71                     $this->show_basic_auth_error();
72                 }
73             }
74         } else {
75
76                         # Caller might give us a username even if not required
77                         if (isset($_SERVER['PHP_AUTH_USER'])) {
78                                 $user = User::staticGet('nickname', $_SERVER['PHP_AUTH_USER']);
79                                 if ($user) {
80                                         $this->user = $user;
81                                 }
82                                 # Twitter doesn't throw an error if the user isn't found
83                         }
84
85             $this->process_command();
86         }
87     }
88
89     function process_command()
90     {
91         $action = "twitapi$this->api_action";
92         $actionfile = INSTALLDIR."/actions/$action.php";
93
94         if (file_exists($actionfile)) {
95             require_once($actionfile);
96             $action_class = ucfirst($action)."Action";
97             $action_obj = new $action_class();
98
99             if (!$action_obj->prepare($this->args)) {
100                 return;
101             }
102
103             if (method_exists($action_obj, $this->api_method)) {
104                 $apidata = array(    'content-type' => $this->content_type,
105                                     'api_method' => $this->api_method,
106                                     'api_arg' => $this->api_arg,
107                                     'user' => $this->user);
108
109                 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
110             } else {
111                 $this->clientError("API method not found!", $code=404);
112             }
113         } else {
114             $this->clientError("API method not found!", $code=404);
115         }
116     }
117
118     # Whitelist of API methods that don't need authentication
119     function requires_auth()
120     {
121         static $noauth = array( 'statuses/public_timeline',
122                                 'statuses/show',
123                                 'users/show',
124                                 'help/test',
125                                 'help/downtime_schedule',
126                                 'laconica/version',
127                                 'laconica/config',
128                                 'laconica/wadl');
129
130         static $bareauth = array('statuses/user_timeline',
131                                  'statuses/friends_timeline',
132                                  'statuses/friends',
133                                  'statuses/replies',
134                                  'statuses/mentions',
135                                  'statuses/followers',
136                                  'favorites/favorites');
137
138         $fullname = "$this->api_action/$this->api_method";
139
140         // If the site is "private", all API methods except laconica/config
141         // need authentication
142         if (common_config('site', 'private')) {
143             return $fullname != 'laconica/config' || false;
144         }
145
146         if (in_array($fullname, $bareauth)) {
147             # bareauth: only needs auth if without an argument or query param specifying user
148             if ($this->api_arg || $this->arg('id') || is_numeric($this->arg('user_id')) || $this->arg('screen_name')) {
149                 return false;
150             } else {
151                 return true;
152             }
153         } else if (in_array($fullname, $noauth)) {
154             # noauth: never needs auth
155             return false;
156         } else {
157             # everybody else needs auth
158             return true;
159         }
160     }
161
162     function show_basic_auth_error()
163     {
164         header('HTTP/1.1 401 Unauthorized');
165         $msg = 'Could not authenticate you.';
166
167         if ($this->content_type == 'xml') {
168             header('Content-Type: application/xml; charset=utf-8');
169             $this->startXML();
170             $this->elementStart('hash');
171             $this->element('error', null, $msg);
172             $this->element('request', null, $_SERVER['REQUEST_URI']);
173             $this->elementEnd('hash');
174             $this->endXML();
175         } else if ($this->content_type == 'json')  {
176             header('Content-Type: application/json; charset=utf-8');
177             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
178             print(json_encode($error_array));
179         } else {
180             header('Content-type: text/plain');
181             print "$msg\n";
182         }
183     }
184
185     function isReadOnly($args)
186     {
187         $apiaction = $args['apiaction'];
188         $method = $args['method'];
189
190         list($cmdtext, $fmt) = explode('.', $method);
191
192         static $write_methods = array(
193             'account' => array('update_location', 'update_delivery_device', 'end_session'),
194             'blocks' => array('create', 'destroy'),
195             'direct_messages' => array('create', 'destroy'),
196             'favorites' => array('create', 'destroy'),
197             'friendships' => array('create', 'destroy'),
198             'help' => array(),
199             'notifications' => array('follow', 'leave'),
200             'statuses' => array('update', 'destroy'),
201             'users' => array()
202         );
203
204         if (array_key_exists($apiaction, $write_methods)) {
205             if (!in_array($cmdtext, $write_methods[$apiaction])) {
206                 return true;
207             }
208         }
209
210         return false;
211     }
212 }