4e1229bb38d771c8e8e09a64dd286b99465a672c
[platal.git] / classes / plmodule.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 array('hook' => array($this, 'handler_'.$fun),
54 'auth' => $auth,
55 'perms' => $perms,
56 'type' => $type);
57 }
58
59 /** Include a 'module-specific' file.
60 * Module specific includes must be in the in the path modules/{modulename}.
61 */
62 public function load($file)
63 {
64 require_once $this->modIncludePath . $file;
65 }
66
67 /* static functions */
68
69 public static function path($modname)
70 {
71 global $globals;
72 if ($modname == 'core') {
73 $mod_path = $globals->spoolroot . '/core/modules/' . $modname;
74 } else {
75 $mod_path = $globals->spoolroot . '/modules/' . $modname;
76 }
77 return $mod_path;
78 }
79
80 public static function factory($modname)
81 {
82 $mod_path = self::path($modname);
83 $class = ucfirst($modname) . 'Module';
84
85 require_once $mod_path . '.php';
86 $module = new $class();
87 $module->modIncludePath = $mod_path . '/';
88 return $module;
89 }
90 }
91
92 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
93 ?>