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) {
31 parent::handle($args);
33 $this->api_action = $this->arg('apiaction');
34 $method = $this->arg('method');
35 $argument = $this->arg('argument');
37 if (isset($argument)) {
38 $cmdext = explode('.', $argument);
39 $this->api_arg = $cmdext[0];
40 $this->api_method = $method;
41 $this->content_type = $cmdext[1];
43 #content type will be an extension on the method
44 $cmdext = explode('.', $method);
45 $this->api_method = $cmdext[0];
46 $this->content_type = $cmdext[1];
49 # common_debug("apiaction = $this->api_action, method = $this->api_method, argument = $this->api_arg, ctype = $this->content_type");
51 # XXX Maybe check to see if the command actually exists first?
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 common_show_basic_auth_error();
61 $nickname = $_SERVER['PHP_AUTH_USER'];
62 $password = $_SERVER['PHP_AUTH_PW'];
63 $user = common_check_user($nickname, $password);
66 $this->nickname = $nickname;
67 $this->process_command();
69 # basic authentication failed
70 common_show_basic_auth_error();
74 $this->process_command();
78 function process_command() {
79 $action = "twitapi$this->api_action";
80 $actionfile = INSTALLDIR."/actions/$action.php";
81 if (file_exists($actionfile)) {
82 require_once($actionfile);
83 $action_class = ucfirst($action)."Action";
84 $action_obj = new $action_class();
86 if (method_exists($action_obj, $this->api_method)) {
88 $apidata = array( 'content-type' => $this->content_type,
89 'api_method' => $this->api_method,
90 'api_arg' => $this->api_arg,
91 'nickanme' => $htis->nickanme);
93 call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
94 # all API methods should exit()
97 common_user_error("API method not found!", $code=404);
101 # Whitelist of API methods that don't need authentication
102 function requires_auth() {
103 static $noauth = array( 'statuses/public_timeline',
105 'help/downtime_schedule');
106 if (in_array("$this->api_action/$this->api_method", $noauth)) {