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