PlFlagset and PlDBTableEntry are exportable.
[platal.git] / classes / platal.php
index 69b1533..e5187e4 100644 (file)
@@ -23,6 +23,7 @@ define('PL_DO_AUTH',   300);
 define('PL_FORBIDDEN', 403);
 define('PL_NOT_FOUND', 404);
 define('PL_WIKI',      500);
+define('PL_JSON',      501);
 
 abstract class PlHook
 {
@@ -65,13 +66,14 @@ abstract class PlHook
             if ($this->hasType(DO_AUTH)) {
                 if (!$session->start($this->auth)) {
                     $platal->force_login($page);
+                    return PL_FORBIDDEN;
                 }
             } else {
                 return PL_FORBIDDEN;
             }
         }
         if (!$this->checkPerms()) {
-            if (!Platal::notAllowed()) {
+            if (Platal::notAllowed()) {
                 return PL_FORBIDDEN;
             }
         }
@@ -218,7 +220,6 @@ class PlHookTree
     }
 }
 
-
 abstract class Platal
 {
     private $mods;
@@ -228,7 +229,7 @@ abstract class Platal
 
     public $ns;
     public $path;
-    public $argv;
+    public $argv = array();
 
     static private $_page = null;
 
@@ -266,7 +267,7 @@ abstract class Platal
             $this->mods[$module] = $m = PLModule::factory($module);
             $hooks = $m->handlers();
             foreach ($hooks as $path=>$hook) {
-                $this->hooks->addChild(split('/', $path), $hook);
+                $this->hooks->addChild(explode('/', $path), $hook);
             }
         }
 
@@ -301,7 +302,7 @@ abstract class Platal
 
     protected function find_hook()
     {
-        $p = split('/', $this->path);
+        $p = explode('/', $this->path);
         list($hook, $matched, $remain, $aliased) = $this->hooks->findChild($p);
         if (empty($hook)) {
             return null;
@@ -317,7 +318,7 @@ abstract class Platal
 
     public function near_hook()
     {
-        $p = split('/', $this->path);
+        $p = explode('/', $this->path);
         list($hook, $matched, $remain, $aliased) = $this->hooks->findNearestChild($p);
         if (empty($hook)) {
             return null;
@@ -359,22 +360,38 @@ 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);
+            $res = $this->call_hook($page);
+            switch ($res) {
+              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;
+              case PL_WIKI:
+                return PL_WIKI;
+            }
+        } catch (Exception $e) {
+            header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
+            PlErrorReport::report($e);
+            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);
-        $page->run();
+        if ($res == PL_JSON) {
+            $page->runJSon();
+        } else {
+            $page->run();
+        }
     }
 
     public function error403()
@@ -425,19 +442,13 @@ abstract class Platal
 
     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);
-
             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é.";
             }
-            Platal::page()->kill($userfriendly);
+            throw new PlException($userfriendly, $error);
         }
     }
 
@@ -445,7 +456,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);
         }