1<?php
2/**
3 * DokuWiki Plugin adminperm (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
7 */
8
9
10class admin_plugin_adminperm extends DokuWiki_Admin_Plugin
11{
12    protected $config = DOKU_CONF . 'adminperm.json';
13
14
15    /**
16     * @return int sort number in admin menu
17     */
18    public function getMenuSort()
19    {
20        return 255;
21    }
22
23    /**
24     * @return bool true if only access for superuser, false is for superusers and moderators
25     */
26    public function forAdminOnly()
27    {
28        return true;
29    }
30
31    /**
32     * Should carry out any processing required by the plugin.
33     */
34    public function handle()
35    {
36        global $INPUT;
37        if ($INPUT->post->has('d') && checkSecurityToken()) {
38            if ($this->save($INPUT->post->arr('d'))) {
39                msg($this->getLang('saved'), 1);
40            }
41        }
42    }
43
44    /**
45     * Render HTML output, e.g. helpful text and a form
46     */
47    public function html()
48    {
49        echo $this->locale_xhtml('intro');
50
51        $cnf = $this->load(true);
52        $plugins = plugin_list('admin');
53        sort($plugins);
54
55        $form = new \dokuwiki\Form\Form();
56        $form->addFieldsetOpen($this->getLang('legend'));
57        foreach ($plugins as $plugin) {
58            /** @var DokuWiki_Admin_Plugin $obj */
59            $obj = plugin_load('admin', $plugin);
60            if ($obj === null) continue;
61
62            $label = $plugin . ($obj->forAdminOnly() ? ' (A)' : ' (M)');
63
64            $form->addTextInput('d[' . $plugin . ']', $label)->addClass('block')->val($cnf[$plugin] ?: '');
65        }
66
67        if (file_exists($this->config) && !is_writable($this->config)) {
68            msg(sprintf($this->getLang('nosave'), $this->config), -1);
69        } else {
70            $form->addButton('submit', $this->getLang('save'));
71
72        }
73
74        echo $form->toHTML();
75    }
76
77    /**
78     * Load the current config
79     *
80     * @param bool $refresh force a reload of the config instead of relying on the static copy
81     * @return string[]
82     */
83    public function load($refresh = false)
84    {
85        static $config = null;
86        if ($config === null || $refresh) {
87            $config = [];
88            if (file_exists($this->config)) {
89                $config = json_decode(io_readFile($this->config, false), true);
90            }
91        }
92        return $config;
93    }
94
95    /**
96     * Save the given config
97     *
98     * @param string[] $data
99     * @return bool
100     */
101    public function save($data)
102    {
103        $orig = $this->load(true);
104        $data = array_merge($orig, $data);
105        $data = array_map('trim', $data);
106        $data = array_filter($data);
107
108        return io_saveFile($this->config, json_encode($data, JSON_PRETTY_PRINT));
109    }
110}
111
112