]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/actions/api.php
move opening brace of class declaration to next line
[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
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             # Look for the user in the session
76             if (common_logged_in()) {
77                  $this->user = common_current_user();
78             }
79
80             $this->process_command();
81         }
82     }
83
84     function process_command()
85     {
86         $action = "twitapi$this->api_action";
87         $actionfile = INSTALLDIR."/actions/$action.php";
88
89         if (file_exists($actionfile)) {
90             require_once($actionfile);
91             $action_class = ucfirst($action)."Action";
92             $action_obj = new $action_class();
93
94             if (!$action_obj->prepare($this->args)) {
95                 return;
96             }
97
98             if (method_exists($action_obj, $this->api_method)) {
99                 $apidata = array(    'content-type' => $this->content_type,
100                                     'api_method' => $this->api_method,
101                                     'api_arg' => $this->api_arg,
102                                     'user' => $this->user);
103
104                 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
105             } else {
106                 common_user_error("API method not found!", $code=404);
107             }
108         } else {
109             common_user_error("API method not found!", $code=404);
110         }
111     }
112
113     # Whitelist of API methods that don't need authentication
114     function requires_auth()
115     {
116         static $noauth = array( 'statuses/public_timeline',
117                                 'statuses/show',
118                                 'users/show',
119                                 'help/test',
120                                 'help/downtime_schedule');
121
122         static $bareauth = array('statuses/user_timeline',
123                                  'statuses/friends',
124                                  'statuses/followers',
125                                  'favorites/favorites');
126
127         # If the site is "private", all API methods need authentication
128
129         if (common_config('site', 'private')) {
130             return true;
131         }
132
133         $fullname = "$this->api_action/$this->api_method";
134
135         if (in_array($fullname, $bareauth)) {
136             # bareauth: only needs auth if without an argument
137             if ($this->api_arg) {
138                 return false;
139             } else {
140                 return true;
141             }
142         } else if (in_array($fullname, $noauth)) {
143             # noauth: never needs auth
144             return false;
145         } else {
146             # everybody else needs auth
147             return true;
148         }
149     }
150
151     function show_basic_auth_error()
152     {
153         header('HTTP/1.1 401 Unauthorized');
154         $msg = 'Could not authenticate you.';
155
156         if ($this->content_type == 'xml') {
157             header('Content-Type: application/xml; charset=utf-8');
158             common_start_xml();
159             common_element_start('hash');
160             common_element('error', null, $msg);
161             common_element('request', null, $_SERVER['REQUEST_URI']);
162             common_element_end('hash');
163             common_end_xml();
164         } else if ($this->content_type == 'json')  {
165             header('Content-Type: application/json; charset=utf-8');
166             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
167             print(json_encode($error_array));
168         } else {
169             header('Content-type: text/plain');
170             print "$msg\n";
171         }
172     }
173
174     function is_readonly()
175     {
176         # NOTE: before handle(), can't use $this->arg
177         $apiaction = $_REQUEST['apiaction'];
178         $method = $_REQUEST['method'];
179         list($cmdtext, $fmt) = explode('.', $method);
180
181         static $write_methods = array(
182             'account' => array('update_location', 'update_delivery_device', 'end_session'),
183             'blocks' => array('create', 'destroy'),
184             'direct_messages' => array('create', 'destroy'),
185             'favorites' => array('create', 'destroy'),
186             'friendships' => array('create', 'destroy'),
187             'help' => array(),
188             'notifications' => array('follow', 'leave'),
189             'statuses' => array('update', 'destroy'),
190             'users' => array()
191         );
192
193         if (array_key_exists($apiaction, $write_methods)) {
194             if (!in_array($cmdtext, $write_methods[$apiaction])) {
195                 return true;
196             }
197         }
198
199         return false;
200     }
201
202 }