| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | abstract class PLModule |
| 23 | { |
| 24 | /** Path of the includes of the module |
| 25 | * Internal use only. |
| 26 | */ |
| 27 | private $modIncludePath; |
| 28 | |
| 29 | /** Return an array associating pathes to the corresponding hook. |
| 30 | * array( '/my/path' => make_hook(...), |
| 31 | * ...); |
| 32 | * @ref make_hook |
| 33 | */ |
| 34 | abstract public function handlers(); |
| 35 | |
| 36 | /** Register a hook |
| 37 | * @param fun name of the handler (the exact name will be handler_$fun) |
| 38 | * @param auth authentification level of needed to run this handler |
| 39 | * @param perms permission required to run this handler |
| 40 | * @param type additionnal flags |
| 41 | * |
| 42 | * Perms syntax is the following: |
| 43 | * perms = rights(,rights)* |
| 44 | * rights = right(:right)* |
| 45 | * right is an atomic right permission (like 'admin', 'user', 'groupadmin', 'groupmember'...) |
| 46 | * |
| 47 | * If type is set to NO_AUTH, the system will return 403 instead of asking auth data |
| 48 | * this is useful for Ajax handler |
| 49 | * If type is not set to NO_SKIN, the system will consider redirecting the user to https |
| 50 | */ |
| 51 | public function make_hook($fun, $auth, $perms = 'user', $type = DO_AUTH) |
| 52 | { |
| 53 | return new PlStdHook(array($this, 'handler_' . $fun), |
| 54 | $auth, $perms, $type); |
| 55 | } |
| 56 | |
| 57 | /** Register a hook that points to a wiki page. |
| 58 | */ |
| 59 | public function make_wiki_hook($auth = AUTH_PUBLIC, $perms = 'user', $type = DO_AUTH) |
| 60 | { |
| 61 | return new PlWikiHook($auth, $perms, $type); |
| 62 | } |
| 63 | |
| 64 | /** Include a 'module-specific' file. |
| 65 | * Module specific includes must be in the in the path modules/{modulename}. |
| 66 | */ |
| 67 | public function load($file) |
| 68 | { |
| 69 | require_once $this->modIncludePath . $file; |
| 70 | } |
| 71 | |
| 72 | /* static functions */ |
| 73 | |
| 74 | public static function path($modname) |
| 75 | { |
| 76 | global $globals; |
| 77 | if ($modname == 'core') { |
| 78 | $mod_path = $globals->spoolroot . '/core/modules/' . $modname; |
| 79 | } else { |
| 80 | $mod_path = $globals->spoolroot . '/modules/' . $modname; |
| 81 | } |
| 82 | return $mod_path; |
| 83 | } |
| 84 | |
| 85 | public static function factory($modname) |
| 86 | { |
| 87 | $mod_path = self::path($modname); |
| 88 | $class = ucfirst($modname) . 'Module'; |
| 89 | |
| 90 | require_once $mod_path . '.php'; |
| 91 | $module = new $class(); |
| 92 | $module->modIncludePath = $mod_path . '/'; |
| 93 | return $module; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 98 | ?> |