1<?php
2/**
3 * DokuWiki Plugin newnamespacepermissions (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_newnamespacepermissions extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess');
23        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'checkPagesave');
24
25    }
26
27    /**
28     * [Custom event handler which performs action]
29     *
30     * @param Doku_Event $event  event object by reference
31     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
32     *                           handler was registered]
33     * @return void
34     */
35    public function handleActionActPreprocess(Doku_Event $event, $param) {
36        if (act_clean($event->data) !== 'edit') {
37            return;
38        }
39
40        if ($this->isSaveAllowed()) {
41            return;
42        }
43
44        // user tries to create namespace but isn't allowed to do so
45        $event->data = 'show';
46        msg($this->getLang('namespace creation not allowed'),-1);
47    }
48
49    /**
50     * [Custom event handler which performs action]
51     *
52     * @param Doku_Event $event  event object by reference
53     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
54     *                           handler was registered]
55     * @return void
56     */
57    public function checkPagesave(Doku_Event $event, $param) {
58        if ($this->isSaveAllowed()) {
59            return;
60        }
61
62        $event->preventDefault();
63        msg($this->getLang('namespace creation not allowed'), -1);
64        msg($this->getLang('you tried to save') . '<pre>' . hsc($event->data['newContent']) . '</pre>', 0);
65    }
66
67    /**
68     * Check if we're creating a new page in a new namespace and if we're allowed to do so
69     */
70    protected function isSaveAllowed() {
71        global $ID, $INPUT, $USERINFO;
72        $namespaceDir = dirname(wikiFN($ID));
73        if (file_exists($namespaceDir) && is_dir($namespaceDir)) {
74            return true;
75        }
76
77        $allowedToCreateNamespaces = $this->getConf('allow_namespace_creation');
78        $user = $INPUT->server->str('REMOTE_USER');
79        $groups = $USERINFO['grps'] ?: ['ALL'];
80
81        if (auth_isMember($allowedToCreateNamespaces, $user, $groups) || auth_isadmin()) {
82            return true;
83        }
84
85        return false;
86    }
87
88}
89
90// vim:ts=4:sw=4:et:
91