From 625e63a30340592766029718b3c9e5101eede036 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 10 Jun 2011 16:50:06 -0400 Subject: [PATCH] Make errors work correctly --- .../DomainStatusNetworkPlugin.php | 9 ++++++ .../actions/globalregister.php | 17 ++++++++--- .../lib/globalapiaction.php | 29 ++++++++++++++----- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php b/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php index efb1aae4d8..a923ac4c30 100644 --- a/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php +++ b/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php @@ -157,6 +157,15 @@ class DomainStatusNetworkPlugin extends Plugin return true; } + function onLoginAction($action, &$login) { + $this->debug($action); + if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) { + $login = true; + return false; + } + return true; + } + static function nicknameForDomain($domain) { $registered = self::registeredDomain($domain); diff --git a/plugins/DomainStatusNetwork/actions/globalregister.php b/plugins/DomainStatusNetwork/actions/globalregister.php index 42dd207f4e..ed9bfc3c2c 100644 --- a/plugins/DomainStatusNetwork/actions/globalregister.php +++ b/plugins/DomainStatusNetwork/actions/globalregister.php @@ -57,8 +57,17 @@ class GlobalregisterAction extends GlobalApiAction function prepare($argarray) { - parent::prepare($argarray); - return true; + try { + parent::prepare($argarray); + return true; + } catch (ClientException $e) { + $this->showError($e->getMessage(), $e->getCode()); + return false; + } catch (Exception $e) { + common_log(LOG_ERR, $e->getMessage()); + $this->showError(_('An internal error occurred.'), 500); + return false; + } } /** @@ -75,10 +84,10 @@ class GlobalregisterAction extends GlobalApiAction DomainStatusNetworkPlugin::registerEmail($this->email, true); $this->showSuccess(); } catch (ClientException $e) { - $this->showError($e->getMessage()); + $this->showError($e->getMessage(), $e->getCode()); } catch (Exception $e) { common_log(LOG_ERR, $e->getMessage()); - $this->showError(_('An internal error occurred.')); + $this->showError(_('An internal error occurred.'), 500); } return; diff --git a/plugins/DomainStatusNetwork/lib/globalapiaction.php b/plugins/DomainStatusNetwork/lib/globalapiaction.php index ef77d89cfe..cd0c7f9526 100644 --- a/plugins/DomainStatusNetwork/lib/globalapiaction.php +++ b/plugins/DomainStatusNetwork/lib/globalapiaction.php @@ -64,13 +64,13 @@ class GlobalApiAction extends Action parent::prepare($args); if (!common_config('globalapi', 'enabled')) { - throw new ClientException(_('Global API not enabled.')); + throw new ClientException(_('Global API not enabled.'), 403); } $apikey = $this->trimmed('apikey'); if (empty($apikey)) { - throw new ClientException(_('No API key.')); + throw new ClientException(_('No API key.'), 403); } $expected = common_config('globalapi', 'key'); @@ -78,7 +78,7 @@ class GlobalApiAction extends Action if ($expected != $apikey) { // FIXME: increment a counter by IP address to prevent brute-force // attacks on the key. - throw new ClientException(_('Bad API key.')); + throw new ClientException(_('Bad API key.'), 403); } $email = common_canonical_email($this->trimmed('email')); @@ -96,23 +96,36 @@ class GlobalApiAction extends Action return true; } - function showError($message) + function showError($message, $code=400) { - $this->showOutput(array('error' => $message)); + $this->showOutput(array('error' => $message), $code); } - function showSuccess($values=null) + function showSuccess($values=null, $code=200) { if (empty($values)) { $values = array(); } $values['success'] = 1; - $this->showOutput($values); + $this->showOutput($values, $code); } - function showOutput($values) + function showOutput($values, $code) { + if (array_key_exists($code, ClientErrorAction::$status)) { + $status_string = ClientErrorAction::$status[$code]; + } else if (array_key_exists($code, ServerErrorAction::$status)) { + $status_string = ServerErrorAction::$status[$code]; + } else { + // bad code! + $code = 500; + $status_string = ServerErrorAction::$status[$code]; + } + + header('HTTP/1.1 '.$code.' '.$status_string); + header('Content-Type: application/json; charset=utf-8'); print(json_encode($values)); + print("\n"); } } -- 2.39.5