Creates a new plat/al error code for invalid request. This is intended
authorVincent Zanotti <vincent.zanotti@m4x.org>
Mon, 3 Jan 2011 16:51:31 +0000 (17:51 +0100)
committerVincent Zanotti <vincent.zanotti@m4x.org>
Mon, 3 Jan 2011 16:51:31 +0000 (17:51 +0100)
to be used for requests where the URL format, or the input is invalid
(for instance non JSON-encoded input for the API).

Signed-off-by: Vincent Zanotti <vincent.zanotti@m4x.org>
classes/platal.php
modules/core.php
templates/40x.tpl [moved from templates/403.tpl with 100% similarity]

index 755cf67..cbd9870 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-define('PL_DO_AUTH',   300);
-define('PL_FORBIDDEN', 403);
-define('PL_NOT_FOUND', 404);
-define('PL_WIKI',      500);
-define('PL_JSON',      501);
+// Return values for handlers and hooks. This defines the behavior of both the
+// plat/al engine, and the invidivual hooks.
+define('PL_DO_AUTH',     300);  // User should be redirected to the login page.
+define('PL_BAD_REQUEST', 400);  // Request is not valid, and could not be interpreted.
+define('PL_FORBIDDEN',   403);  // User is not allowed to view page (auth or permission error).
+define('PL_NOT_FOUND',   404);  // Page doesn't not exist. Engine will try to offer suggestions.
+define('PL_WIKI',        500);  // Page is a wiki page, plat/al engine should yield to the wiki engine.
+define('PL_JSON',        501);  // Page is valid, but result should be JSON-encoded, not HTML-encoded.
 
 abstract class PlHook
 {
@@ -421,6 +424,10 @@ abstract class Platal
             $page->assign('platal', $this);
             $res = $this->call_hook($page);
             switch ($res) {
+              case PL_BAD_REQUEST:
+                $this->mods['core']->handler_400($page);
+                break;
+
               case PL_FORBIDDEN:
                 $this->mods['core']->handler_403($page);
                 break;
index 6557ca4..a109a4c 100644 (file)
@@ -24,6 +24,7 @@ class CoreModule extends PLModule
     function handlers()
     {
         return array(
+            '400'           => $this->make_hook('400',           AUTH_PUBLIC),
             '403'           => $this->make_hook('403',           AUTH_PUBLIC),
             '404'           => $this->make_hook('404',           AUTH_PUBLIC),
             'login'         => $this->make_hook('login',         AUTH_COOKIE),
@@ -51,17 +52,23 @@ class CoreModule extends PLModule
         exit;
     }
 
-    function handler_403($page)
+    function handler_400(PlPage& $page)
+    {
+        header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
+        $page->coreTpl('40x.tpl');
+        $page->trigError('Ta requête est invalide.');
+    }
+
+    function handler_403(PlPage& $page)
     {
-        global $globals;
         header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
+        $page->coreTpl('40x.tpl');
         $page->trigError('Tu n\'as pas les permissions nécessaires pour accéder à cette page.');
-        $page->coreTpl('403.tpl');
     }
 
-    function handler_404($page)
+    function handler_404(PlPage& $page)
     {
-        global $globals, $platal;
+        global $platal;
         header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
         $page->coreTpl('404.tpl');
         $page->assign('near', $platal->near_hook());
similarity index 100%
rename from templates/403.tpl
rename to templates/40x.tpl