]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/api.php
8ccd86f0bb1c9fa49c0503f959f3963a78b95cd8
[quix0rs-gnu-social.git] / actions / api.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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     var $user;
25     var $content_type;
26     var $api_arg;
27     var $api_method;
28     var $api_action;
29
30     function handle($args)
31     {
32         parent::handle($args);
33
34         $this->api_action = $this->arg('apiaction');
35         $method = $this->arg('method');
36         $argument = $this->arg('argument');
37
38         if (isset($argument)) {
39             $cmdext = explode('.', $argument);
40             $this->api_arg =  $cmdext[0];
41             $this->api_method = $method;
42             $this->content_type = strtolower($cmdext[1]);
43         } else {
44
45             # Requested format / content-type will be an extension on the method
46             $cmdext = explode('.', $method);
47             $this->api_method = $cmdext[0];
48             $this->content_type = strtolower($cmdext[1]);
49         }
50
51         if ($this->requires_auth()) {
52             if (!isset($_SERVER['PHP_AUTH_USER'])) {
53
54                 # This header makes basic auth go
55                 header('WWW-Authenticate: Basic realm="Laconica API"');
56
57                 # If the user hits cancel -- bam!
58                 $this->show_basic_auth_error();
59             } else {
60                 $nickname = $_SERVER['PHP_AUTH_USER'];
61                 $password = $_SERVER['PHP_AUTH_PW'];
62                 $user = common_check_user($nickname, $password);
63
64                 if ($user) {
65                     $this->user = $user;
66                     $this->process_command();
67                 } else {
68                     # basic authentication failed
69                     $this->show_basic_auth_error();
70                 }
71             }
72         } else {
73
74             # Look for the user in the session
75             if (common_logged_in()) {
76                  $this->user = common_current_user();
77             }
78
79             $this->process_command();
80         }
81     }
82
83     function process_command()
84     {
85         $action = "twitapi$this->api_action";
86         $actionfile = INSTALLDIR."/actions/$action.php";
87
88         if (file_exists($actionfile)) {
89             require_once($actionfile);
90             $action_class = ucfirst($action)."Action";
91             $action_obj = new $action_class();
92
93             if (!$action_obj->prepare($this->args)) {
94                 return;
95             }
96
97             if (method_exists($action_obj, $this->api_method)) {
98                 $apidata = array(    'content-type' => $this->content_type,
99                                     'api_method' => $this->api_method,
100                                     'api_arg' => $this->api_arg,
101                                     'user' => $this->user);
102
103                 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
104             } else {
105                 common_user_error("API method not found!", $code=404);
106             }
107         } else {
108             common_user_error("API method not found!", $code=404);
109         }
110     }
111
112     # Whitelist of API methods that don't need authentication
113     function requires_auth()
114     {
115         static $noauth = array( 'statuses/public_timeline',
116                                 'statuses/show',
117                                 'users/show',
118                                 'help/test',
119                                 'help/downtime_schedule');
120
121         static $bareauth = array('statuses/user_timeline',
122                                  'statuses/friends',
123                                  'statuses/followers',
124                                  'favorites/favorites');
125
126         # If the site is "private", all API methods need authentication
127
128         if (common_config('site', 'private')) {
129             return true;
130         }
131
132         $fullname = "$this->api_action/$this->api_method";
133
134         if (in_array($fullname, $bareauth)) {
135             # bareauth: only needs auth if without an argument
136             if ($this->api_arg) {
137                 return false;
138             } else {
139                 return true;
140             }
141         } else if (in_array($fullname, $noauth)) {
142             # noauth: never needs auth
143             return false;
144         } else {
145             # everybody else needs auth
146             return true;
147         }
148     }
149
150     function show_basic_auth_error()
151     {
152         header('HTTP/1.1 401 Unauthorized');
153         $msg = 'Could not authenticate you.';
154
155         if ($this->content_type == 'xml') {
156             header('Content-Type: application/xml; charset=utf-8');
157             common_start_xml();
158             common_element_start('hash');
159             common_element('error', null, $msg);
160             common_element('request', null, $_SERVER['REQUEST_URI']);
161             common_element_end('hash');
162             common_end_xml();
163         } else if ($this->content_type == 'json')  {
164             header('Content-Type: application/json; charset=utf-8');
165             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
166             print(json_encode($error_array));
167         } else {
168             header('Content-type: text/plain');
169             print "$msg\n";
170         }
171     }
172
173     function is_readonly()
174     {
175         # NOTE: before handle(), can't use $this->arg
176         $apiaction = $_REQUEST['apiaction'];
177         $method = $_REQUEST['method'];
178         list($cmdtext, $fmt) = explode('.', $method);
179
180         static $write_methods = array(
181             'account' => array('update_location', 'update_delivery_device', 'end_session'),
182             'blocks' => array('create', 'destroy'),
183             'direct_messages' => array('create', 'destroy'),
184             'favorites' => array('create', 'destroy'),
185             'friendships' => array('create', 'destroy'),
186             'help' => array(),
187             'notifications' => array('follow', 'leave'),
188             'statuses' => array('update', 'destroy'),
189             'users' => array()
190         );
191
192         if (array_key_exists($apiaction, $write_methods)) {
193             if (!in_array($cmdtext, $write_methods[$apiaction])) {
194                 return true;
195             }
196         }
197
198         return false;
199     }
200
201 }