Proof of concept:
authorx2000habouzit <x2000habouzit@839d8a87-29fc-0310-9880-83ba4fa771e5>
Mon, 3 Jul 2006 21:36:14 +0000 (21:36 +0000)
committerx2000habouzit <x2000habouzit@839d8a87-29fc-0310-9880-83ba4fa771e5>
Mon, 3 Jul 2006 21:36:14 +0000 (21:36 +0000)
work on a semantic and ergonomic uri system in platal

git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@356 839d8a87-29fc-0310-9880-83ba4fa771e5

classes/PLModule.php [new file with mode: 0644]
classes/Platal.php [new file with mode: 0644]
htdocs/test.php [new file with mode: 0644]
include/platal/session.inc.php
modules/core.php [new file with mode: 0644]
templates/403.tpl [new file with mode: 0644]
templates/404.tpl [new file with mode: 0644]

diff --git a/classes/PLModule.php b/classes/PLModule.php
new file mode 100644 (file)
index 0000000..d7172fc
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+class PLModule
+{
+    var $platal;
+
+    function PLModule(&$platal)
+    {
+        $this->platal =& $platal;
+    }
+
+    function handlers()     { die("implement me"); }
+    function menu_entries() { die("implement me"); }
+
+    function make_hook($fun, $auth, $perms, $type = SKINNED)
+    {
+        return array('hook'  => array($this, $fun),
+                     'auth'  => $auth,
+                     'perms' => split('[ ,|]', $perms),
+                     'type'  => $type);
+    }
+
+    /* static functions */
+
+    function factory(&$platal, $modname)
+    {
+        $mod_path = dirname(__FILE__).'/../modules/'.strtolower($modname).'.php';
+        $class    = ucfirst($modname).'Module';
+
+        require_once $mod_path;
+        return new $class($site);
+    }
+}
+
+?>
diff --git a/classes/Platal.php b/classes/Platal.php
new file mode 100644 (file)
index 0000000..b587458
--- /dev/null
@@ -0,0 +1,113 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+define('PL_OK', 0);
+define('PL_NEEDLOGIN', 1);
+define('PL_FORBIDDEN', 403);
+define('PL_NOT_FOUND', 404);
+
+class Platal
+{
+    var $__mods;
+    var $__hooks;
+
+    var $path;
+    var $menu;
+    var $auth;
+
+    function Platal()
+    {
+        $this->path = Get::_get('p', null);
+
+        $this->__mods  = array();
+        $this->__hooks = array();
+        $this->menu    = array();
+
+        foreach (glob(dirname(__FILE__).'/../modules/*.php') as $module) {
+            $module = basename($module, '.php');
+            $m =& PLModule::factory($this, $module);
+            $this->__mods[$module] =& $m;
+            $this->__hooks += $m->handlers();
+            $this->menu = array_merge($this->menu, $m->menu_entries());
+        }
+
+        krsort($this->__hooks);
+        usort($this->menu, array($this, '_menu_cmp'));
+    }
+
+    function load_class($cls)
+    {
+        require_once dirname(__FILE__).'/../classes/'.$cls.'.php';
+    }
+
+    function _menu_cmp($a, $b)
+    {
+        return strcasecmp($a['path'], $b['path']);
+    }
+
+    function call_hook(&$page)
+    {
+        $p = $this->path;
+
+        while ($path) {
+            if (array_key_exists($p, $this->__hooks))
+                break;
+
+            $p = substr($p, 0, strrpos($p, '/'));
+        }
+        if (empty($this->__hooks[$p])) {
+            return PL_NOT_FOUND;
+        }
+
+        $args = array_merge(array(&$page),
+            explode('/', substr($this->path, strlen($p) + 1)));
+
+        $hook = $this->__hooks[$p];
+        if ($hook['auth'] > Session::get('auth', AUTH_PUBLIC)) {
+            $_SESSION['session']->doAuth($page);
+        }
+        return call_user_func_array($hook['hook'], $args);
+    }
+
+    function run()
+    {
+        global $page;
+
+        new_skinned_page('index.tpl', AUTH_PUBLIC);
+
+        if (empty($this->path)) {
+            $page->run();
+        }
+
+        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;
+        }
+        $page->run();
+    }
+}
+
+?>
diff --git a/htdocs/test.php b/htdocs/test.php
new file mode 100644 (file)
index 0000000..39ad26a
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once 'xorg.inc.php';
+
+require_once dirname(__FILE__).'/../classes/Platal.php';
+require_once dirname(__FILE__).'/../classes/PLModule.php';
+
+$platal = new Platal();
+$platal->run();
+
+?>
index 3e30719..54d99fb 100644 (file)
@@ -51,7 +51,7 @@ function check_perms()
     
 function has_perms()
 {
-    return logged() && Session::get('perms')==PERMS_ADMIN;
+    return logged() && Session::get('perms') == PERMS_ADMIN;
 }
 
 // }}}
diff --git a/modules/core.php b/modules/core.php
new file mode 100644 (file)
index 0000000..e9766b8
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+class CoreModule extends PLModule
+{
+    function menu_entries()
+    {
+        return array();
+    }
+
+    function handlers()
+    {
+        return array(
+            '403'   => $this->make_hook('handler_403', AUTH_PUBLIC, ''),
+            '404'   => $this->make_hook('handler_404', AUTH_PUBLIC, ''),
+        );
+    }
+
+    function handler_auth(&$page)
+    {
+        return PL_OK;
+    }
+
+    function handler_403(&$page)
+    {
+        header('HTTP/1.0 403 Forbidden');
+        $page->changeTpl('403.tpl');
+        return PL_OK;
+    }
+
+    function handler_404(&$page)
+    {
+        header('HTTP/1.0 404 Not Found');
+        $page->changeTpl('404.tpl');
+        return PL_OK;
+    }
+}
+
+?>
diff --git a/templates/403.tpl b/templates/403.tpl
new file mode 100644 (file)
index 0000000..3a23f10
--- /dev/null
@@ -0,0 +1,24 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2006 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<h1 class="erreur">Tu n'as pas les permissions nécessaires pour accéder à cette page</h1>
+
diff --git a/templates/404.tpl b/templates/404.tpl
new file mode 100644 (file)
index 0000000..18f68b4
--- /dev/null
@@ -0,0 +1,24 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2006 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<h1 class="erreur">Cette page n'existe pas !!!</h1>
+