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
31 function handle($args)
33 parent::handle($args);
35 $this->api_action = $this->arg('apiaction');
36 $method = $this->arg('method');
37 $argument = $this->arg('argument');
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]);
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]);
52 if ($this->requires_auth()) {
53 if (!isset($_SERVER['PHP_AUTH_USER'])) {
55 # This header makes basic auth go
56 header('WWW-Authenticate: Basic realm="Laconica API"');
58 # If the user hits cancel -- bam!
59 $this->show_basic_auth_error();
61 $nickname = $_SERVER['PHP_AUTH_USER'];
62 $password = $_SERVER['PHP_AUTH_PW'];
63 $user = common_check_user($nickname, $password);
67 $this->process_command();
69 # basic authentication failed
70 $this->show_basic_auth_error();
75 # Look for the user in the session
76 if (common_logged_in()) {
77 $this->user = common_current_user();
80 $this->process_command();
84 function process_command()
86 $action = "twitapi$this->api_action";
87 $actionfile = INSTALLDIR."/actions/$action.php";
89 if (file_exists($actionfile)) {
90 require_once($actionfile);
91 $action_class = ucfirst($action)."Action";
92 $action_obj = new $action_class();
94 if (!$action_obj->prepare($this->args)) {
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);
104 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
106 $this->clientError("API method not found!", $code=404);
109 $this->clientError("API method not found!", $code=404);
113 # Whitelist of API methods that don't need authentication
114 function requires_auth()
116 static $noauth = array( 'statuses/public_timeline',
120 'help/downtime_schedule',
125 static $bareauth = array('statuses/user_timeline',
127 'statuses/followers',
128 'favorites/favorites');
130 # If the site is "private", all API methods need authentication
132 if (common_config('site', 'private')) {
136 $fullname = "$this->api_action/$this->api_method";
138 if (in_array($fullname, $bareauth)) {
139 # bareauth: only needs auth if without an argument
140 if ($this->api_arg) {
145 } else if (in_array($fullname, $noauth)) {
146 # noauth: never needs auth
149 # everybody else needs auth
154 function show_basic_auth_error()
156 header('HTTP/1.1 401 Unauthorized');
157 $msg = 'Could not authenticate you.';
159 if ($this->content_type == 'xml') {
160 header('Content-Type: application/xml; charset=utf-8');
162 $this->elementStart('hash');
163 $this->element('error', null, $msg);
164 $this->element('request', null, $_SERVER['REQUEST_URI']);
165 $this->elementEnd('hash');
167 } else if ($this->content_type == 'json') {
168 header('Content-Type: application/json; charset=utf-8');
169 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
170 print(json_encode($error_array));
172 header('Content-type: text/plain');
177 function isReadOnly()
179 # NOTE: before handle(), can't use $this->arg
180 $apiaction = $_REQUEST['apiaction'];
181 $method = $_REQUEST['method'];
182 list($cmdtext, $fmt) = explode('.', $method);
184 static $write_methods = array(
185 'account' => array('update_location', 'update_delivery_device', 'end_session'),
186 'blocks' => array('create', 'destroy'),
187 'direct_messages' => array('create', 'destroy'),
188 'favorites' => array('create', 'destroy'),
189 'friendships' => array('create', 'destroy'),
191 'notifications' => array('follow', 'leave'),
192 'statuses' => array('update', 'destroy'),
196 if (array_key_exists($apiaction, $write_methods)) {
197 if (!in_array($cmdtext, $write_methods[$apiaction])) {