| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 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); 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 |
| 41 | * @param perms permission required to run this handler |
| 42 | * @param type additionnal flags |
| 43 | * |
| 44 | * Perms syntax is the following: |
| 45 | * perms = rights(,rights)* |
| 46 | * rights = right(:right)* |
| 47 | * right is an atomic right permission (like 'admin', 'user', 'groupadmin', 'groupmember'...) |
| 48 | * |
| 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. |
| 52 | */ |
| 53 | public function make_hook($fun, $auth, $perms = 'user', $type = DO_AUTH) |
| 54 | { |
| 55 | return new PlStdHook(array($this, 'handler_' . $fun), $auth, $perms, $type); |
| 56 | } |
| 57 | |
| 58 | /** Register an API hook. |
| 59 | * @param fun name of the handler (the exact name will be handler_$fun); the |
| 60 | * handler will be invoked with a PlPage, the authenticated PlUser, the |
| 61 | * JSON-decoded payload (if any), and the unmatched path components |
| 62 | * @param auth authentification level required, when not API-authenticated |
| 63 | * @param perms permission required to run this handler |
| 64 | * @param type additionnal flags (only NO_HTTPS is supported at the moment) |
| 65 | * |
| 66 | * See {@link make_hook} above for details on permissions. |
| 67 | * |
| 68 | * WARNING: It is expected that the API authentication mechanism will not be |
| 69 | * protected against short-term replay of requests (for instance replay of a |
| 70 | * given request within 5-10 seconds). |
| 71 | * |
| 72 | * You are explicitly advised to make any API request idempotent (eg. use |
| 73 | * "DELETE /api/email/foo@example.com" instead of "DELETE /api/email/0" to |
| 74 | * delete the first email in a list). |
| 75 | */ |
| 76 | public function make_api_hook($fun, $auth, $perms = 'user', $type = NO_AUTH) |
| 77 | { |
| 78 | return new PlApiHook(array($this, 'handler_' . $fun), $auth, $perms, $type); |
| 79 | } |
| 80 | |
| 81 | /** Register a token-authentified hook (rss, csv, ical, ...) |
| 82 | * @param fun name of the handler (the exact name will be handler_$fun); the |
| 83 | * handler will be invoked with the PlPage object, the PlUser of the |
| 84 | * request, and the unmatched path components |
| 85 | * @param auth authentification level required, when not token-authentified |
| 86 | * @param perms permission required to run this handler |
| 87 | * @param type additionnal flags |
| 88 | * |
| 89 | * See {@link make_hook} above for details on permissions and additional |
| 90 | * flags. Note that DO_AUTH has no effect here, as the request will always |
| 91 | * be passively identified. |
| 92 | * |
| 93 | * This hook requires that the first two unmatched path components form a |
| 94 | * valid (user, token) pair; if not, a session-based authentification will |
| 95 | * be attempted, in which case $auth will be honored. |
| 96 | * Note that because token-based authentication is weak, it should only be |
| 97 | * used for readonly handlers normally served in AUTH_COOKIE. |
| 98 | */ |
| 99 | public function make_token_hook($fun, $auth, $perms = 'user', $type = NO_HTTPS) |
| 100 | { |
| 101 | return new PlTokenHook(array($this, 'handler_' . $fun), $auth, $perms, $type); |
| 102 | } |
| 103 | |
| 104 | /** Register a hook that points to a wiki page. |
| 105 | */ |
| 106 | public function make_wiki_hook($auth = AUTH_PUBLIC, $perms = 'user', $type = DO_AUTH) |
| 107 | { |
| 108 | return new PlWikiHook($auth, $perms, $type); |
| 109 | } |
| 110 | |
| 111 | /** Include a 'module-specific' file. |
| 112 | * Module specific includes must be in the in the path modules/{modulename}. |
| 113 | */ |
| 114 | public function load($file) |
| 115 | { |
| 116 | require_once $this->modIncludePath . $file; |
| 117 | } |
| 118 | |
| 119 | /* static functions */ |
| 120 | |
| 121 | public static function path($modname) |
| 122 | { |
| 123 | global $globals; |
| 124 | if ($modname == 'core') { |
| 125 | $mod_path = $globals->coreroot . '/modules/' . $modname; |
| 126 | } else { |
| 127 | $mod_path = $globals->spoolroot . '/modules/' . $modname; |
| 128 | } |
| 129 | return $mod_path; |
| 130 | } |
| 131 | |
| 132 | public static function factory($modname) |
| 133 | { |
| 134 | $mod_path = self::path($modname); |
| 135 | $class = ucfirst($modname) . 'Module'; |
| 136 | |
| 137 | require_once $mod_path . '.php'; |
| 138 | $module = new $class(); |
| 139 | $module->modIncludePath = $mod_path . '/'; |
| 140 | return $module; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 145 | ?> |