Adds a new PlHook subclass for token-authenticated requests.
[platal.git] / classes / plmodule.php
CommitLineData
b62f8858 1<?php
2/***************************************************************************
2ab75571 3 * Copyright (C) 2003-2010 Polytechnique.org *
b62f8858 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
bf517daf 22abstract class PLModule
b62f8858 23{
24173926
FB
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();
b62f8858 35
bf517daf 36 /** Register a hook
a59a4446
VZ
37 * @param fun name of the handler (the exact name will be handler_$fun); the
38 * handler will be invoked with the PlPage object, and the unmatched path
39 * components
40 * @param auth authentification level required to run this handler
bf517daf 41 * @param perms permission required to run this handler
42 * @param type additionnal flags
43 *
44 * Perms syntax is the following:
a59a4446
VZ
45 * perms = rights(,rights)*
46 * rights = right(:right)*
bf517daf 47 * right is an atomic right permission (like 'admin', 'user', 'groupadmin', 'groupmember'...)
48 *
a59a4446
VZ
49 * If type is set to NO_AUTH, the system will return 403 instead of asking
50 * auth data; this is useful for Ajax handler. If type is not set to
51 * NO_SKIN, the system will consider redirecting the user to https.
bf517daf 52 */
53 public function make_hook($fun, $auth, $perms = 'user', $type = DO_AUTH)
b62f8858 54 {
a59a4446
VZ
55 return new PlStdHook(array($this, 'handler_' . $fun), $auth, $perms, $type);
56 }
57
58 /** Register a token-authentified hook (rss, csv, ical, ...)
59 * @param fun name of the handler (the exact name will be handler_$fun); the
60 * handler will be invoked with the PlPage object, the PlUser of the
61 * request, and the unmatched path components
62 * @param auth authentification level required, when not token-authentified
63 * @param perms permission required to run this handler
64 * @param type additionnal flags
65 *
66 * See {@link make_hook} above for details on permissions and additional
67 * flags. Note that DO_AUTH has no effect here, as the request will always
68 * be passively identified.
69 *
70 * This hook requires that the first two unmatched path components form a
71 * valid (user, token) pair; if not, a session-based authentification will
72 * be attempted, in which case $auth will be honored.
73 * Note that because token-based authentication is weak, it should only be
74 * used for readonly handlers normally served in AUTH_COOKIE.
75 */
76 public function make_token_hook($fun, $auth, $perms = 'user', $type = NO_HTTPS)
77 {
78 return new PlTokenHook(array($this, 'handler_' . $fun), $auth, $perms, $type);
b62f8858 79 }
80
bfb9093b
FB
81 /** Register a hook that points to a wiki page.
82 */
83 public function make_wiki_hook($auth = AUTH_PUBLIC, $perms = 'user', $type = DO_AUTH)
84 {
46ca2746 85 return new PlWikiHook($auth, $perms, $type);
bfb9093b
FB
86 }
87
24173926
FB
88 /** Include a 'module-specific' file.
89 * Module specific includes must be in the in the path modules/{modulename}.
90 */
91 public function load($file)
92 {
93 require_once $this->modIncludePath . $file;
94 }
95
b62f8858 96 /* static functions */
97
ac2f544d 98 public static function path($modname)
b62f8858 99 {
1edb5aa5
FB
100 global $globals;
101 if ($modname == 'core') {
24173926 102 $mod_path = $globals->spoolroot . '/core/modules/' . $modname;
1edb5aa5 103 } else {
24173926 104 $mod_path = $globals->spoolroot . '/modules/' . $modname;
1edb5aa5 105 }
ac2f544d
FB
106 return $mod_path;
107 }
108
109 public static function factory($modname)
110 {
111 $mod_path = self::path($modname);
a18afbdc 112 $class = ucfirst($modname) . 'Module';
b62f8858 113
24173926
FB
114 require_once $mod_path . '.php';
115 $module = new $class();
116 $module->modIncludePath = $mod_path . '/';
117 return $module;
b62f8858 118 }
119}
120
a7de4ef7 121// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
b62f8858 122?>