]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Make errors work correctly
authorEvan Prodromou <evan@status.net>
Fri, 10 Jun 2011 20:50:06 +0000 (16:50 -0400)
committerEvan Prodromou <evan@status.net>
Fri, 10 Jun 2011 20:50:06 +0000 (16:50 -0400)
plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
plugins/DomainStatusNetwork/actions/globalregister.php
plugins/DomainStatusNetwork/lib/globalapiaction.php

index efb1aae4d8c9929b41f529b2c900de256713c865..a923ac4c30fd352ad64583f1ec6052f321c73f64 100644 (file)
@@ -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);
index 42dd207f4eb3f43de937993028deb1d9b40bb41f..ed9bfc3c2c493712262bfbf58d5e1f6d6c8d1bad 100644 (file)
@@ -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;
index ef77d89cfebbd8a6bea2d0d7e12af75375df9d80..cd0c7f9526232549e9e8076940abb621df44ca82 100644 (file)
@@ -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");
     }
 }