Twitter-compatible API - cleaned up sloppy control flow: exit() statements everywhere

darcs-hash:20081001025421-462f3-3bf666327de3a3c5ea506b2c387741dc3d0e82bf.gz
This commit is contained in:
zach 2008-09-30 22:54:21 -04:00
parent dec2f29c6a
commit 6beddfdbb9
11 changed files with 105 additions and 143 deletions

View File

@ -26,14 +26,14 @@ class ApiAction extends Action {
var $api_arg; var $api_arg;
var $api_method; var $api_method;
var $api_action; var $api_action;
function handle($args) { function handle($args) {
parent::handle($args); parent::handle($args);
$this->api_action = $this->arg('apiaction'); $this->api_action = $this->arg('apiaction');
$method = $this->arg('method'); $method = $this->arg('method');
$argument = $this->arg('argument'); $argument = $this->arg('argument');
if (isset($argument)) { if (isset($argument)) {
$cmdext = explode('.', $argument); $cmdext = explode('.', $argument);
$this->api_arg = $cmdext[0]; $this->api_arg = $cmdext[0];
@ -45,71 +45,70 @@ class ApiAction extends Action {
$this->api_method = $cmdext[0]; $this->api_method = $cmdext[0];
$this->content_type = strtolower($cmdext[1]); $this->content_type = strtolower($cmdext[1]);
} }
# XXX Maybe check to see if the command actually exists first? # XXX Maybe check to see if the command actually exists first?
if($this->requires_auth()) { if($this->requires_auth()) {
if (!isset($_SERVER['PHP_AUTH_USER'])) { if (!isset($_SERVER['PHP_AUTH_USER'])) {
# This header makes basic auth go # This header makes basic auth go
header('WWW-Authenticate: Basic realm="Laconica API"'); header('WWW-Authenticate: Basic realm="Laconica API"');
# if the user hits cancel -- bam! # if the user hits cancel -- bam!
$this->show_basic_auth_error(); $this->show_basic_auth_error();
} else { } else {
$nickname = $_SERVER['PHP_AUTH_USER']; $nickname = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW']; $password = $_SERVER['PHP_AUTH_PW'];
$user = common_check_user($nickname, $password); $user = common_check_user($nickname, $password);
if ($user) { if ($user) {
$this->user = $user; $this->user = $user;
$this->process_command(); $this->process_command();
} else { } else {
# basic authentication failed # basic authentication failed
$this->show_basic_auth_error(); $this->show_basic_auth_error();
} }
} }
} else { } else {
$this->process_command(); $this->process_command();
} }
} }
function process_command() { function process_command() {
$action = "twitapi$this->api_action"; $action = "twitapi$this->api_action";
$actionfile = INSTALLDIR."/actions/$action.php"; $actionfile = INSTALLDIR."/actions/$action.php";
if (file_exists($actionfile)) { if (file_exists($actionfile)) {
require_once($actionfile); require_once($actionfile);
$action_class = ucfirst($action)."Action"; $action_class = ucfirst($action)."Action";
$action_obj = new $action_class(); $action_obj = new $action_class();
if (method_exists($action_obj, $this->api_method)) { if (method_exists($action_obj, $this->api_method)) {
$apidata = array( 'content-type' => $this->content_type, $apidata = array( 'content-type' => $this->content_type,
'api_method' => $this->api_method, 'api_method' => $this->api_method,
'api_arg' => $this->api_arg, 'api_arg' => $this->api_arg,
'user' => $this->user); 'user' => $this->user);
call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
# all API methods should exit()
}
}
common_user_error("API method not found!", $code=404);
}
call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
}
} else {
common_user_error("API method not found!", $code=404);
}
}
# Whitelist of API methods that don't need authentication # Whitelist of API methods that don't need authentication
function requires_auth() { function requires_auth() {
static $noauth = array( 'statuses/public_timeline', static $noauth = array( 'statuses/public_timeline',
'statuses/show', 'statuses/show',
'users/show', 'users/show',
'help/test', 'help/test',
'help/downtime_schedule'); 'help/downtime_schedule');
static $bareauth = array('statuses/user_timeline', static $bareauth = array('statuses/user_timeline',
'statuses/friends', 'statuses/friends',
'statuses/followers'); 'statuses/followers');
$fullname = "$this->api_action/$this->api_method"; $fullname = "$this->api_action/$this->api_method";
if (in_array($fullname, $bareauth)) { if (in_array($fullname, $bareauth)) {
# bareauth: only needs auth if without an argument # bareauth: only needs auth if without an argument
if ($this->api_arg) { if ($this->api_arg) {
@ -125,12 +124,11 @@ class ApiAction extends Action {
return true; return true;
} }
} }
function show_basic_auth_error() { function show_basic_auth_error() {
header('HTTP/1.1 401 Unauthorized'); header('HTTP/1.1 401 Unauthorized');
header('Content-type: text/plain'); header('Content-type: text/plain');
print("Could not authenticate you."); # exactly what Twitter says - no \n print("Could not authenticate you."); # exactly what Twitter says - no \n
exit();
} }
} }

View File

@ -24,73 +24,66 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
class TwitapiaccountAction extends TwitterapiAction { class TwitapiaccountAction extends TwitterapiAction {
function is_readonly() { function is_readonly() {
static $write_methods = array( 'update_location', static $write_methods = array( 'update_location',
'update_delivery_device'); 'update_delivery_device');
$cmdtext = explode('.', $this->arg('method')); $cmdtext = explode('.', $this->arg('method'));
if (in_array($cmdtext[0], $write_methods)) { if (in_array($cmdtext[0], $write_methods)) {
return false; return false;
} }
return true; return true;
} }
function verify_credentials($args, $apidata) { function verify_credentials($args, $apidata) {
if ($apidata['content-type'] == 'xml') { if ($apidata['content-type'] == 'xml') {
header('Content-Type: application/xml; charset=utf-8'); header('Content-Type: application/xml; charset=utf-8');
print '<authorized>true</authorized>'; print '<authorized>true</authorized>';
} elseif ($apidata['content-type'] == 'json') { } elseif ($apidata['content-type'] == 'json') {
header('Content-Type: application/json; charset=utf-8'); header('Content-Type: application/json; charset=utf-8');
print '{"authorized":true}'; print '{"authorized":true}';
} else { } else {
common_user_error(_('API method not found!'), $code=404); common_user_error(_('API method not found!'), $code=404);
} }
exit();
} }
function end_session($args, $apidata) { function end_session($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
function update_location($args, $apidata) { function update_location($args, $apidata) {
parent::handle($args); parent::handle($args);
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
}
if ($_SERVER['REQUEST_METHOD'] != 'POST') { if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit(); return;
} }
$location = trim($this->arg('location')); $location = trim($this->arg('location'));
if (!is_null($location) && strlen($location) > 255) { if (!is_null($location) && strlen($location) > 255) {
// XXX: But Twitter just truncates and runs with it. -- Zach // XXX: But Twitter just truncates and runs with it. -- Zach
$this->client_error(_('That\'s too long. Max notice size is 255 chars.'), 406, $apidate['content-type']); $this->client_error(_('That\'s too long. Max notice size is 255 chars.'), 406, $apidate['content-type']);
exit(); return;
} }
$user = $apidata['user']; $user = $apidata['user'];
$profile = $user->getProfile(); $profile = $user->getProfile();
if (!$profile) { if (!$profile) {
common_server_error(_('User has no profile.')); common_server_error(_('User has no profile.'));
exit(); return;
} }
$orig_profile = clone($profile); $orig_profile = clone($profile);
$profile->location = $location; $profile->location = $location;
common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__); common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
common_debug('New profile: ' . common_log_objstring($profile), __FILE__); common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
@ -99,29 +92,25 @@ class TwitapiaccountAction extends TwitterapiAction {
if (!$result) { if (!$result) {
common_log_db_error($profile, 'UPDATE', __FILE__); common_log_db_error($profile, 'UPDATE', __FILE__);
common_server_error(_('Couldn\'t save profile.')); common_server_error(_('Couldn\'t save profile.'));
exit(); return;
} }
common_broadcast_profile($profile); common_broadcast_profile($profile);
$type = $apidata['content-type']; $type = $apidata['content-type'];
$this->init_document($type); $this->init_document($type);
$this->show_profile($profile, $type); $this->show_profile($profile, $type);
$this->end_document($type); $this->end_document($type);
exit();
} }
function update_delivery_device($args, $apidata) { function update_delivery_device($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
function rate_limit_status($args, $apidata) { function rate_limit_status($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
} }

View File

@ -26,13 +26,11 @@ class TwitapiblocksAction extends TwitterapiAction {
function create($args, $apidata) { function create($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
function destroy($args, $apidata) { function destroy($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
} }

View File

@ -104,7 +104,6 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
} }
exit();
} }
// had to change this from "new" to "create" to avoid PHP reserved word // had to change this from "new" to "create" to avoid PHP reserved word
@ -113,7 +112,7 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
if ($_SERVER['REQUEST_METHOD'] != 'POST') { if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit(); return;
} }
$user = $apidata['user']; $user = $apidata['user'];
@ -130,30 +129,30 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
} else if (mb_strlen($status) > 140) { } else if (mb_strlen($status) > 140) {
$this->client_error(_('That\'s too long. Max message size is 140 chars.'), $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
$code = 406, $apidata['content-type']); $code = 406, $apidata['content-type']);
exit(); return;
} }
$other = $this->get_user($this->trimmed('user')); $other = $this->get_user($this->trimmed('user'));
if (!$other) { if (!$other) {
$this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']); $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
exit(); return;
} else if (!$user->mutuallySubscribed($other)) { } else if (!$user->mutuallySubscribed($other)) {
$this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'), $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
$code = 403, $apidata['content-type']); $code = 403, $apidata['content-type']);
exit(); return;
} else if ($user->id == $other->id) { } else if ($user->id == $other->id) {
// Sending msgs to yourself is allowed by Twitter // Sending msgs to yourself is allowed by Twitter
$this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'), $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
$code = 403, $apidata['content-type']); $code = 403, $apidata['content-type']);
exit(); return;
} }
$message = Message::saveNew($user->id, $other->id, $content, $source); $message = Message::saveNew($user->id, $other->id, $content, $source);
if (is_string($message)) { if (is_string($message)) {
$this->server_error($message); $this->server_error($message);
exit(); return;
} }
$this->notify($user, $other, $message); $this->notify($user, $other, $message);
@ -164,13 +163,11 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
$this->show_single_json_dmsg($message); $this->show_single_json_dmsg($message);
} }
exit();
} }
function destroy($args, $apidata) { function destroy($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
function show_xml_dmsgs($message) { function show_xml_dmsgs($message) {

View File

@ -66,7 +66,7 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$profile) { if (!$profile) {
common_server_error(_('User has no profile.')); common_server_error(_('User has no profile.'));
exit(); return;
} }
$page = $this->arg('page'); $page = $this->arg('page');
@ -83,7 +83,7 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$notice) { if (!$notice) {
common_server_error(_('Could not retrieve favorite notices.')); common_server_error(_('Could not retrieve favorite notices.'));
exit(); return;
} }
$sitename = common_config('site', 'name'); $sitename = common_config('site', 'name');
@ -111,7 +111,6 @@ class TwitapifavoritesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
} }
exit();
} }
function create($args, $apidata) { function create($args, $apidata) {
@ -119,14 +118,14 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) { if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
exit; return;
} }
// Check for RESTfulness // Check for RESTfulness
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) { if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
// XXX: Twitter just prints the err msg, no XML / JSON. // XXX: Twitter just prints the err msg, no XML / JSON.
$this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
exit(); return;
} }
$user = $apidata['user']; $user = $apidata['user'];
@ -135,13 +134,13 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$notice) { if (!$notice) {
$this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']); $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
exit(); return;
} }
// XXX: Twitter lets you fave things repeatedly via api. // XXX: Twitter lets you fave things repeatedly via api.
if ($user->hasFave($notice)) { if ($user->hasFave($notice)) {
$this->client_error(_('This notice is already a favorite!'), 403, $apidata['content-type']); $this->client_error(_('This notice is already a favorite!'), 403, $apidata['content-type']);
exit(); return;
} }
common_debug("notice: " . $apidata['api_arg']); common_debug("notice: " . $apidata['api_arg']);
@ -150,7 +149,7 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$fave) { if (!$fave) {
common_server_error(_('Could not create favorite.')); common_server_error(_('Could not create favorite.'));
exit(); return;
} }
$this->notify($fave, $notice, $user); $this->notify($fave, $notice, $user);
@ -162,13 +161,11 @@ class TwitapifavoritesAction extends TwitterapiAction {
$this->show_single_json_status($notice); $this->show_single_json_status($notice);
} }
exit();
} }
function destroy($args, $apidata) { function destroy($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
// XXX: these two funcs swiped from faves. Maybe put in util.php, or some common base class? // XXX: these two funcs swiped from faves. Maybe put in util.php, or some common base class?

View File

@ -42,7 +42,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if ($_SERVER['REQUEST_METHOD'] != 'POST') { if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit(); return;
} }
$id = $apidata['api_arg']; $id = $apidata['api_arg'];
@ -51,7 +51,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$other) { if (!$other) {
$this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']); $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
exit(); return;
} }
$user = $apidata['user']; $user = $apidata['user'];
@ -59,7 +59,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if ($user->isSubscribed($other)) { if ($user->isSubscribed($other)) {
$errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname); $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
$this->client_error($errmsg, 403, $apidata['content-type']); $this->client_error($errmsg, 403, $apidata['content-type']);
exit(); return;
} }
$sub = new Subscription(); $sub = new Subscription();
@ -75,7 +75,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$result) { if (!$result) {
$errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname); $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
$this->client_error($errmsg, 400, $apidata['content-type']); $this->client_error($errmsg, 400, $apidata['content-type']);
exit(); return;
} }
$sub->query('COMMIT'); $sub->query('COMMIT');
@ -86,7 +86,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$this->init_document($type); $this->init_document($type);
$this->show_profile($other, $type); $this->show_profile($other, $type);
$this->end_document($type); $this->end_document($type);
exit();
} }
//destroy //destroy
@ -106,7 +106,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) { if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
$this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
exit(); return;
} }
$id = $apidata['api_arg']; $id = $apidata['api_arg'];
@ -126,14 +126,14 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$sub->query('COMMIT'); $sub->query('COMMIT');
} else { } else {
$this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']); $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
exit(); return;
} }
$type = $apidata['content-type']; $type = $apidata['content-type'];
$this->init_document($type); $this->init_document($type);
$this->show_profile($other, $type); $this->show_profile($other, $type);
$this->end_document($type); $this->end_document($type);
exit();
} }
// Tests if a friendship exists between two users. // Tests if a friendship exists between two users.
@ -154,7 +154,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) { if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
exit; return;
} }
$user_a_id = $this->trimmed('user_a'); $user_a_id = $this->trimmed('user_a');
@ -165,7 +165,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$user_a || !$user_b) { if (!$user_a || !$user_b) {
$this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']); $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
exit(); return;
} }
if ($user_a->isSubscribed($user_b)) { if ($user_a->isSubscribed($user_b)) {
@ -189,7 +189,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
break; break;
} }
exit();
} }
} }

View File

@ -45,13 +45,12 @@ class TwitapihelpAction extends TwitterapiAction {
} else { } else {
common_user_error(_('API method not found!'), $code=404); common_user_error(_('API method not found!'), $code=404);
} }
exit();
} }
function downtime_schedule($args, $apidata) { function downtime_schedule($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
} }

View File

@ -27,13 +27,11 @@ class TwitapinotificationsAction extends TwitterapiAction {
function follow($args, $apidata) { function follow($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
function leave($args, $apidata) { function leave($args, $apidata) {
parent::handle($args); parent::handle($args);
common_server_error(_('API method under construction.'), $code=501); common_server_error(_('API method under construction.'), $code=501);
exit();
} }
} }

View File

@ -98,7 +98,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_server_error(_('Couldn\'t find any statuses.'), $code = 503); common_server_error(_('Couldn\'t find any statuses.'), $code = 503);
} }
exit();
} }
/* /*
@ -177,7 +176,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
} }
exit();
} }
/* /*
@ -235,7 +233,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!$profile) { if (!$profile) {
common_server_error(_('User has no profile.')); common_server_error(_('User has no profile.'));
exit(); return;
} }
$count = $this->arg('count'); $count = $this->arg('count');
@ -290,7 +288,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
} }
exit();
} }
function update($args, $apidata) { function update($args, $apidata) {
@ -299,12 +296,12 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) { if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
exit; return;
} }
if ($_SERVER['REQUEST_METHOD'] != 'POST') { if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit(); return;
} }
$user = $apidata['user']; $user = $apidata['user'];
@ -322,7 +319,7 @@ class TwitapistatusesAction extends TwitterapiAction {
// No error is given, but the status is not posted to the // No error is given, but the status is not posted to the
// user's timeline. Seems bad. Shouldn't we throw an // user's timeline. Seems bad. Shouldn't we throw an
// errror? -- Zach // errror? -- Zach
exit(); return;
} else if (mb_strlen($status) > 140) { } else if (mb_strlen($status) > 140) {
@ -331,7 +328,7 @@ class TwitapistatusesAction extends TwitterapiAction {
// that assume Twitter will truncate for them. Should we just // that assume Twitter will truncate for them. Should we just
// truncate too? -- Zach // truncate too? -- Zach
$this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']); $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
exit(); return;
} }
$reply_to = NULL; $reply_to = NULL;
@ -345,7 +342,7 @@ class TwitapistatusesAction extends TwitterapiAction {
$reply_to = $in_reply_to_status_id; $reply_to = $in_reply_to_status_id;
} else { } else {
$this->client_error(_('Not found'), $code = 404, $apidata['content-type']); $this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
exit(); return;
} }
} }
@ -353,7 +350,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (is_string($notice)) { if (is_string($notice)) {
$this->server_error($notice); $this->server_error($notice);
exit(); return;
} }
common_broadcast_notice($notice); common_broadcast_notice($notice);
@ -367,7 +364,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$apidata['api_arg'] = $notice->id; $apidata['api_arg'] = $notice->id;
$this->show($args, $apidata); $this->show($args, $apidata);
exit();
} }
/* /*
@ -447,7 +443,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
} }
exit();
} }
function show($args, $apidata) { function show($args, $apidata) {
@ -455,7 +450,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) { if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
exit; return;
} }
$notice_id = $apidata['api_arg']; $notice_id = $apidata['api_arg'];
@ -472,7 +467,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']); $this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
} }
exit();
} }
@ -497,14 +491,14 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) { if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
exit; return;
} }
// Check for RESTfulness // Check for RESTfulness
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) { if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
// XXX: Twitter just prints the err msg, no XML / JSON. // XXX: Twitter just prints the err msg, no XML / JSON.
$this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']); $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
exit(); return;
} }
$user = $apidata['user']; $user = $apidata['user'];
@ -513,7 +507,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!$notice) { if (!$notice) {
$this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']); $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
exit(); return;
} }
if ($user->id == $notice->profile_id) { if ($user->id == $notice->profile_id) {
@ -532,7 +526,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']); $this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
} }
exit();
} }
# User Methods # User Methods
@ -626,7 +619,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$this->init_document($type); $this->init_document($type);
$this->show_profiles($others, $type); $this->show_profiles($others, $type);
$this->end_document($type); $this->end_document($type);
exit();
} }
function get_subs_user($apidata) { function get_subs_user($apidata) {
@ -673,7 +665,6 @@ class TwitapistatusesAction extends TwitterapiAction {
break; break;
default: default:
$this->client_error(_('unsupported file type')); $this->client_error(_('unsupported file type'));
exit();
} }
} }

View File

@ -23,11 +23,11 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
class TwitapiusersAction extends TwitterapiAction { class TwitapiusersAction extends TwitterapiAction {
function is_readonly() { function is_readonly() {
return true; return true;
} }
/* /*
Returns extended information of a given user, specified by ID or Returns extended information of a given user, specified by ID or
screen name as per the required id parameter below. This information screen name as per the required id parameter below. This information
includes design settings, so third party developers can theme their includes design settings, so third party developers can theme their
@ -47,45 +47,45 @@ class TwitapiusersAction extends TwitterapiAction {
* email. Optional. The email address of a user. Ex: * email. Optional. The email address of a user. Ex:
http://twitter.com/users/show.xml?email=test@example.com http://twitter.com/users/show.xml?email=test@example.com
*/ */
function show($args, $apidata) { function show($args, $apidata) {
parent::handle($args); parent::handle($args);
if (!in_array($apidata['content-type'], array('xml', 'json'))) { if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404); common_user_error(_('API method not found!'), $code = 404);
exit; return;
} }
$user = null; $user = null;
$email = $this->arg('email'); $email = $this->arg('email');
if (isset($apidata['api_arg'])) { if (isset($apidata['api_arg'])) {
if (is_numeric($apidata['api_arg'])) { if (is_numeric($apidata['api_arg'])) {
// by user id // by user id
$user = User::staticGet($apidata['api_arg']); $user = User::staticGet($apidata['api_arg']);
} else { } else {
// by nickname // by nickname
$nickname = common_canonical_nickname($apidata['api_arg']); $nickname = common_canonical_nickname($apidata['api_arg']);
$user = User::staticGet('nickname', $nickname); $user = User::staticGet('nickname', $nickname);
} }
} elseif ($email) { } elseif ($email) {
// or, find user by email address // or, find user by email address
// XXX: The Twitter API spec say an id is *required*, but you can actually // XXX: The Twitter API spec say an id is *required*, but you can actually
// pull up a user with just an email address. -- Zach // pull up a user with just an email address. -- Zach
$user = User::staticGet('email', $email); $user = User::staticGet('email', $email);
} }
if (!$user) { if (!$user) {
// XXX: Twitter returns a random(?) user instead of throwing and err! -- Zach // XXX: Twitter returns a random(?) user instead of throwing and err! -- Zach
$this->client_error(_('User not found.'), 404, $apidata['content-type']); $this->client_error(_('User not found.'), 404, $apidata['content-type']);
exit(); return;
} }
$profile = $user->getProfile(); $profile = $user->getProfile();
if (!$profile) { if (!$profile) {
common_server_error(_('User has no profile.')); common_server_error(_('User has no profile.'));
exit(); return;
} }
$twitter_user = $this->twitter_user_array($profile, true); $twitter_user = $this->twitter_user_array($profile, true);
@ -115,7 +115,7 @@ class TwitapiusersAction extends TwitterapiAction {
$twitter_user['following'] = ''; $twitter_user['following'] = '';
$twitter_user['notifications'] = ''; $twitter_user['notifications'] = '';
if ($apidata['content-type'] == 'xml') { if ($apidata['content-type'] == 'xml') {
$this->init_document('xml'); $this->init_document('xml');
$this->show_twitter_xml_user($twitter_user); $this->show_twitter_xml_user($twitter_user);
$this->end_document('xml'); $this->end_document('xml');
@ -123,8 +123,7 @@ class TwitapiusersAction extends TwitterapiAction {
$this->init_document('json'); $this->init_document('json');
$this->show_json_objects($twitter_user); $this->show_json_objects($twitter_user);
$this->end_document('json'); $this->end_document('json');
} }
exit();
} }
} }

View File

@ -194,7 +194,6 @@ class TwitterapiAction extends Action {
$twitter_status = $this->twitter_status_array($notice); $twitter_status = $this->twitter_status_array($notice);
$this->show_twitter_xml_status($twitter_status); $this->show_twitter_xml_status($twitter_status);
$this->end_document('xml'); $this->end_document('xml');
exit();
} }
function show_single_json_status($notice) { function show_single_json_status($notice) {
@ -202,7 +201,6 @@ class TwitterapiAction extends Action {
$status = $this->twitter_status_array($notice); $status = $this->twitter_status_array($notice);
$this->show_json_objects($status); $this->show_json_objects($status);
$this->end_document('json'); $this->end_document('json');
exit();
} }
function show_single_xml_dmsg($message) { function show_single_xml_dmsg($message) {
@ -469,7 +467,6 @@ class TwitterapiAction extends Action {
$this->end_document('json'); $this->end_document('json');
} }
exit();
} }
function init_twitter_rss() { function init_twitter_rss() {