From b62f88580b5d5a12e804094ddc46033b62a8dbff Mon Sep 17 00:00:00 2001 From: x2000habouzit Date: Mon, 3 Jul 2006 21:36:14 +0000 Subject: [PATCH] Proof of concept: 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 | 54 ++++++++++++++++++++ classes/Platal.php | 113 +++++++++++++++++++++++++++++++++++++++++ htdocs/test.php | 30 +++++++++++ include/platal/session.inc.php | 2 +- modules/core.php | 57 +++++++++++++++++++++ templates/403.tpl | 24 +++++++++ templates/404.tpl | 24 +++++++++ 7 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 classes/PLModule.php create mode 100644 classes/Platal.php create mode 100644 htdocs/test.php create mode 100644 modules/core.php create mode 100644 templates/403.tpl create mode 100644 templates/404.tpl diff --git a/classes/PLModule.php b/classes/PLModule.php new file mode 100644 index 0000000..d7172fc --- /dev/null +++ b/classes/PLModule.php @@ -0,0 +1,54 @@ +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 index 0000000..b587458 --- /dev/null +++ b/classes/Platal.php @@ -0,0 +1,113 @@ +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 index 0000000..39ad26a --- /dev/null +++ b/htdocs/test.php @@ -0,0 +1,30 @@ +run(); + +?> diff --git a/include/platal/session.inc.php b/include/platal/session.inc.php index 3e30719..54d99fb 100644 --- a/include/platal/session.inc.php +++ b/include/platal/session.inc.php @@ -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 index 0000000..e9766b8 --- /dev/null +++ b/modules/core.php @@ -0,0 +1,57 @@ + $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 index 0000000..3a23f10 --- /dev/null +++ b/templates/403.tpl @@ -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 *} +{* *} +{**************************************************************************} + +

Tu n'as pas les permissions nécessaires pour accéder à cette page

+ diff --git a/templates/404.tpl b/templates/404.tpl new file mode 100644 index 0000000..18f68b4 --- /dev/null +++ b/templates/404.tpl @@ -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 *} +{* *} +{**************************************************************************} + +

Cette page n'existe pas !!!

+ -- 2.1.4