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