Manage site errors using exceptions.
[platal.git] / classes / platal.php
index 6943174..b911828 100644 (file)
@@ -65,6 +65,7 @@ abstract class PlHook
             if ($this->hasType(DO_AUTH)) {
                 if (!$session->start($this->auth)) {
                     $platal->force_login($page);
+                    return PL_FORBIDDEN;
                 }
             } else {
                 return PL_FORBIDDEN;
@@ -107,7 +108,7 @@ class PlStdHook extends PlHook
 
 class PlWikiHook extends PlHook
 {
-    public function __construct($callback, $auth = AUTH_PUBLIC, $perms = 'user', $type = DO_AUTH)
+    public function __construct($auth = AUTH_PUBLIC, $perms = 'user', $type = DO_AUTH)
     {
         parent::__construct($auth, $perms, $type);
     }
@@ -228,7 +229,7 @@ abstract class Platal
 
     public $ns;
     public $path;
-    public $argv;
+    public $argv = array();
 
     static private $_page = null;
 
@@ -359,18 +360,35 @@ abstract class Platal
             $this->path = 'index';
         }
 
-        $page->assign('platal', $this);
-        switch ($this->call_hook($page)) {
-          case PL_FORBIDDEN:
-            $this->mods['core']->handler_403($page);
-            break;
+        try {
+            $page->assign('platal', $this);
+            switch ($this->call_hook($page)) {
+              case PL_FORBIDDEN:
+                $this->mods['core']->handler_403($page);
+                break;
 
-          case PL_NOT_FOUND:
-            $this->mods['core']->handler_404($page);
-            break;
+              case PL_NOT_FOUND:
+                $this->mods['core']->handler_404($page);
+                break;
+
+              case PL_WIKI:
+                return PL_WIKI;
+            }
+        } catch (Exception $e) {
+            header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
+
+            $file = fopen(self::globals()->spoolroot . '/spool/tmp/site_errors', 'a');
+            fwrite($file, '<pre>' . date('Y-m-d G:i:s') . '</pre>'
+                        . '<pre>' . pl_entities("" . $e) . '</pre>'
+                        . '------------------------------------------------------------------' . "\n");
+            fclose($file);
 
-          case PL_WIKI:
-            return PL_WIKI;
+            if (self::globals()->debug) {
+                $page->kill(pl_entities($e->getMessage())
+                            . '<pre>' . pl_entities("" . $e) . '</pre>');
+            } else {
+                $page->kill(pl_entities($e->getMessage()));
+            }
         }
 
         $page->assign('platal', $this);
@@ -423,16 +441,15 @@ abstract class Platal
         }
     }
 
-    public static function assert($cond, $error, $userfriendly)
+    public static function assert($cond, $error, $userfriendly = null)
     {
-        global $globals;
         if ($cond === false) {
-            header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
-            $file = fopen($globals->spoolroot . '/spool/tmp/assert_erros', 'a');
-            fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
-            fclose($file);
-
-            Platal::page()->kill($userfriendly);
+            if ($userfriendly == null) {
+                $userfriendly = "Une erreur interne s'est produite.
+                    Merci de réessayer la manipulation qui a déclenché l'erreur ;
+                    si cela ne fonctionne toujours pas, merci de nous signaler le problème rencontré.";
+            }
+            throw new PlException($userfriendly, $error);
         }
     }
 
@@ -440,7 +457,8 @@ abstract class Platal
     {
         if (defined('PL_LOGGER_CLASS')) {
             $class = PL_LOGGER_CLASS;
-            return new $class($uid, $suid);
+            $logger = new $class($uid, $suid);
+            return $logger;
         } else {
             return PlLogger::dummy($uid, $suid);
         }