3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class ApiAction extends Action {
30 function handle($args)
32 parent::handle($args);
34 $this->api_action = $this->arg('apiaction');
35 $method = $this->arg('method');
36 $argument = $this->arg('argument');
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]);
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]);
51 if ($this->requires_auth()) {
52 if (!isset($_SERVER['PHP_AUTH_USER'])) {
54 # This header makes basic auth go
55 header('WWW-Authenticate: Basic realm="Laconica API"');
57 # If the user hits cancel -- bam!
58 $this->show_basic_auth_error();
60 $nickname = $_SERVER['PHP_AUTH_USER'];
61 $password = $_SERVER['PHP_AUTH_PW'];
62 $user = common_check_user($nickname, $password);
66 $this->process_command();
68 # basic authentication failed
69 $this->show_basic_auth_error();
74 # Look for the user in the session
75 if (common_logged_in()) {
76 $this->user = common_current_user();
79 $this->process_command();
83 function process_command()
85 $action = "twitapi$this->api_action";
86 $actionfile = INSTALLDIR."/actions/$action.php";
88 if (file_exists($actionfile)) {
89 require_once($actionfile);
90 $action_class = ucfirst($action)."Action";
91 $action_obj = new $action_class();
93 if (!$action_obj->prepare($this->args)) {
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);
103 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
105 common_user_error("API method not found!", $code=404);
108 common_user_error("API method not found!", $code=404);
112 # Whitelist of API methods that don't need authentication
113 function requires_auth()
115 static $noauth = array( 'statuses/public_timeline',
119 'help/downtime_schedule');
121 static $bareauth = array('statuses/user_timeline',
123 'statuses/followers',
124 'favorites/favorites');
126 # If the site is "private", all API methods need authentication
128 if (common_config('site', 'private')) {
132 $fullname = "$this->api_action/$this->api_method";
134 if (in_array($fullname, $bareauth)) {
135 # bareauth: only needs auth if without an argument
136 if ($this->api_arg) {
141 } else if (in_array($fullname, $noauth)) {
142 # noauth: never needs auth
145 # everybody else needs auth
150 function show_basic_auth_error()
152 header('HTTP/1.1 401 Unauthorized');
153 $msg = 'Could not authenticate you.';
155 if ($this->content_type == 'xml') {
156 header('Content-Type: application/xml; charset=utf-8');
158 common_element_start('hash');
159 common_element('error', null, $msg);
160 common_element('request', null, $_SERVER['REQUEST_URI']);
161 common_element_end('hash');
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));
168 header('Content-type: text/plain');
173 function is_readonly()
175 # NOTE: before handle(), can't use $this->arg
176 $apiaction = $_REQUEST['apiaction'];
177 $method = $_REQUEST['method'];
178 list($cmdtext, $fmt) = explode('.', $method);
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'),
187 'notifications' => array('follow', 'leave'),
188 'statuses' => array('update', 'destroy'),
192 if (array_key_exists($apiaction, $write_methods)) {
193 if (!in_array($cmdtext, $write_methods[$apiaction])) {