xref: /dokuwiki/lib/plugins/config/admin.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
110449332Schris<?php
2*d4f83172SAndreas Gohr
310449332Schris/**
410449332Schris * Configuration Manager admin plugin
510449332Schris *
610449332Schris * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
710449332Schris * @author     Christopher Smith <chris@jalakai.co.uk>
8685bdd2eSBen Coburn * @author     Ben Coburn <btcoburn@silicodon.net>
910449332Schris */
10*d4f83172SAndreas Gohr
118553d24dSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
12c6639e6aSAndreas Gohruse dokuwiki\plugin\config\core\Configuration;
130a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\Setting;
140a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingFieldset;
150a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingHidden;
1610449332Schris
1710449332Schris/**
1810449332Schris * All DokuWiki plugins to extend the admin function
1910449332Schris * need to inherit from this class
2010449332Schris */
218c7c53b0SAndreas Gohrclass admin_plugin_config extends AdminPlugin
228c7c53b0SAndreas Gohr{
23bf9be0e3SAndreas Gohr    protected const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/';
24c6639e6aSAndreas Gohr
25077c27b2SAndreas Gohr    /** @var Configuration */
26077c27b2SAndreas Gohr    protected $configuration;
27077c27b2SAndreas Gohr
28a017dff4SAndreas Gohr    /** @var bool were there any errors in the submitted data? */
29a017dff4SAndreas Gohr    protected $hasErrors = false;
30a017dff4SAndreas Gohr
31a017dff4SAndreas Gohr    /** @var bool have the settings translations been loaded? */
32a017dff4SAndreas Gohr    protected $promptsLocalized = false;
33a017dff4SAndreas Gohr
3410449332Schris
3510449332Schris    /**
3610449332Schris     * handle user request
3710449332Schris     */
38d868eb89SAndreas Gohr    public function handle()
39d868eb89SAndreas Gohr    {
40392c9b52SHakan Sandell        global $ID, $INPUT;
4110449332Schris
42f8dcd5b0SAndreas Gohr        // always initialize the configuration
43f8dcd5b0SAndreas Gohr        $this->configuration = new Configuration();
44f8dcd5b0SAndreas Gohr
45077c27b2SAndreas Gohr        if (!$INPUT->bool('save') || !checkSecurityToken()) {
46253d4b48SGerrit Uitslag            return;
47253d4b48SGerrit Uitslag        }
4810449332Schris
4910449332Schris        // don't go any further if the configuration is locked
50077c27b2SAndreas Gohr        if ($this->configuration->isLocked()) return;
51077c27b2SAndreas Gohr
52077c27b2SAndreas Gohr        // update settings and redirect of successful
53077c27b2SAndreas Gohr        $ok = $this->configuration->updateSettings($INPUT->arr('config'));
54077c27b2SAndreas Gohr        if ($ok) { // no errors
55077c27b2SAndreas Gohr            try {
56077c27b2SAndreas Gohr                if ($this->configuration->hasChanged()) {
57077c27b2SAndreas Gohr                    $this->configuration->save();
58077c27b2SAndreas Gohr                } else {
59077c27b2SAndreas Gohr                    $this->configuration->touch();
60253d4b48SGerrit Uitslag                }
61e98b5e44SAndreas Gohr                msg($this->getLang('updated'), 1);
62077c27b2SAndreas Gohr            } catch (Exception $e) {
63077c27b2SAndreas Gohr                msg($this->getLang('error'), -1);
6410449332Schris            }
65467c1427SAndreas Gohr            send_redirect(wl($ID, ['do' => 'admin', 'page' => 'config'], true, '&'));
66a017dff4SAndreas Gohr        } else {
67a017dff4SAndreas Gohr            $this->hasErrors = true;
68a6b97c7dSGerrit Uitslag            msg($this->getLang('error'), -1);
6910449332Schris        }
7010449332Schris    }
7110449332Schris
7210449332Schris    /**
7310449332Schris     * output appropriate html
7410449332Schris     */
75d868eb89SAndreas Gohr    public function html()
76d868eb89SAndreas Gohr    {
77685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
7810449332Schris        global $lang;
79400497e1Schris        global $ID;
8010449332Schris
8189d74a2fSchris        $this->setupLocale(true);
8210449332Schris
83be1cc9aeSAndreas Gohr        echo $this->locale_xhtml('intro');
8410449332Schris
85be1cc9aeSAndreas Gohr        echo '<div id="config__manager">';
8610449332Schris
87077c27b2SAndreas Gohr        if ($this->configuration->isLocked()) {
88be1cc9aeSAndreas Gohr            echo '<div class="info">' . $this->getLang('locked') . '</div>';
89077c27b2SAndreas Gohr        }
9010449332Schris
915d85efc6SAdrian Lang        // POST to script() instead of wl($ID) so config manager still works if
925d85efc6SAdrian Lang        // rewrite config is broken. Add $ID as hidden field to remember
935d85efc6SAdrian Lang        // current ID in most cases.
94923e149aSMichael Große        echo '<form id="dw__configform" action="' . script() . '" method="post">';
95be1cc9aeSAndreas Gohr        echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>';
96634d7150SAndreas Gohr        formSecurityToken();
97a1ef8b4dSAndreas Gohr        $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
9810449332Schris
994fa2dffcSBen Coburn        $in_fieldset = false;
1004fa2dffcSBen Coburn        $first_plugin_fieldset = true;
1014fa2dffcSBen Coburn        $first_template_fieldset = true;
102077c27b2SAndreas Gohr        foreach ($this->configuration->getSettings() as $setting) {
103467c1427SAndreas Gohr            if ($setting instanceof SettingHidden) {
104685bdd2eSBen Coburn                continue;
105467c1427SAndreas Gohr            } elseif ($setting instanceof SettingFieldset) {
1064fa2dffcSBen Coburn                // config setting group
1074fa2dffcSBen Coburn                if ($in_fieldset) {
108be1cc9aeSAndreas Gohr                    echo '</table>';
109be1cc9aeSAndreas Gohr                    echo '</div>';
110be1cc9aeSAndreas Gohr                    echo '</fieldset>';
1114fa2dffcSBen Coburn                } else {
1124fa2dffcSBen Coburn                    $in_fieldset = true;
1134fa2dffcSBen Coburn                }
114a017dff4SAndreas Gohr                if ($first_plugin_fieldset && $setting->getType() == 'plugin') {
115a1ef8b4dSAndreas Gohr                    $this->printH1('plugin_settings', $this->getLang('_header_plugin'));
1164fa2dffcSBen Coburn                    $first_plugin_fieldset = false;
117a017dff4SAndreas Gohr                } elseif ($first_template_fieldset && $setting->getType() == 'template') {
118a1ef8b4dSAndreas Gohr                    $this->printH1('template_settings', $this->getLang('_header_template'));
1194fa2dffcSBen Coburn                    $first_template_fieldset = false;
1204fa2dffcSBen Coburn                }
121be1cc9aeSAndreas Gohr                echo '<fieldset id="' . $setting->getKey() . '">';
122be1cc9aeSAndreas Gohr                echo '<legend>' . $setting->prompt($this) . '</legend>';
123be1cc9aeSAndreas Gohr                echo '<div class="table">';
124be1cc9aeSAndreas Gohr                echo '<table class="inline">';
1254fa2dffcSBen Coburn            } else {
1264fa2dffcSBen Coburn                // config settings
127467c1427SAndreas Gohr                [$label, $input] = $setting->html($this, $this->hasErrors);
12810449332Schris
1295c17d2d3SAndreas Gohr                $class = $setting->isDefault()
13064159a61SAndreas Gohr                    ? ' class="default"'
1315c17d2d3SAndreas Gohr                    : ($setting->isProtected() ? ' class="protected"' : '');
1325c17d2d3SAndreas Gohr                $error = $setting->hasError()
13364159a61SAndreas Gohr                    ? ' class="value error"'
13464159a61SAndreas Gohr                    : ' class="value"';
13564159a61SAndreas Gohr                $icon = $setting->caution()
136c6639e6aSAndreas Gohr                    ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' .
13764159a61SAndreas Gohr                    'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />'
13864159a61SAndreas Gohr                    : '';
13910449332Schris
140be1cc9aeSAndreas Gohr                echo '<tr' . $class . '>';
141be1cc9aeSAndreas Gohr                echo '<td class="label">';
142be1cc9aeSAndreas Gohr                echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>';
143be1cc9aeSAndreas Gohr                echo $icon . $label;
144be1cc9aeSAndreas Gohr                echo '</td>';
145be1cc9aeSAndreas Gohr                echo '<td' . $error . '>' . $input . '</td>';
146be1cc9aeSAndreas Gohr                echo '</tr>';
14710449332Schris            }
1484fa2dffcSBen Coburn        }
14910449332Schris
150be1cc9aeSAndreas Gohr        echo '</table>';
151be1cc9aeSAndreas Gohr        echo '</div>';
1524fa2dffcSBen Coburn        if ($in_fieldset) {
153be1cc9aeSAndreas Gohr            echo '</fieldset>';
1544fa2dffcSBen Coburn        }
15510449332Schris
156685bdd2eSBen Coburn        // show undefined settings list
157077c27b2SAndreas Gohr        $undefined_settings = $this->configuration->getUndefined();
158685bdd2eSBen Coburn        if ($allow_debug && !empty($undefined_settings)) {
159253d4b48SGerrit Uitslag            /**
160253d4b48SGerrit Uitslag             * Callback for sorting settings
161253d4b48SGerrit Uitslag             *
162077c27b2SAndreas Gohr             * @param Setting $a
163077c27b2SAndreas Gohr             * @param Setting $b
164253d4b48SGerrit Uitslag             * @return int if $a is lower/equal/higher than $b
165253d4b48SGerrit Uitslag             */
166d868eb89SAndreas Gohr            function settingNaturalComparison($a, $b)
167d868eb89SAndreas Gohr            {
168077c27b2SAndreas Gohr                return strnatcmp($a->getKey(), $b->getKey());
169253d4b48SGerrit Uitslag            }
170253d4b48SGerrit Uitslag
171a1ef8b4dSAndreas Gohr            usort($undefined_settings, 'settingNaturalComparison');
172a1ef8b4dSAndreas Gohr            $this->printH1('undefined_settings', $this->getLang('_header_undefined'));
173be1cc9aeSAndreas Gohr            echo '<fieldset>';
174be1cc9aeSAndreas Gohr            echo '<div class="table">';
175be1cc9aeSAndreas Gohr            echo '<table class="inline">';
176685bdd2eSBen Coburn            foreach ($undefined_settings as $setting) {
177467c1427SAndreas Gohr                [$label, $input] = $setting->html($this);
178be1cc9aeSAndreas Gohr                echo '<tr>';
179e7296041SAndreas Gohr                echo '<td class="label">' . $label . '</td>';
180e7296041SAndreas Gohr                echo '<td>' . $input . '</td>';
181be1cc9aeSAndreas Gohr                echo '</tr>';
182685bdd2eSBen Coburn            }
183be1cc9aeSAndreas Gohr            echo '</table>';
184be1cc9aeSAndreas Gohr            echo '</div>';
185be1cc9aeSAndreas Gohr            echo '</fieldset>';
186685bdd2eSBen Coburn        }
187685bdd2eSBen Coburn
188685bdd2eSBen Coburn        // finish up form
189be1cc9aeSAndreas Gohr        echo '<p>';
190be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="do"     value="admin" />';
191be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="page"   value="config" />';
19210449332Schris
193077c27b2SAndreas Gohr        if (!$this->configuration->isLocked()) {
194be1cc9aeSAndreas Gohr            echo '<input type="hidden" name="save"   value="1" />';
195be1cc9aeSAndreas Gohr            echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>';
196be1cc9aeSAndreas Gohr            echo '<button type="reset">' . $lang['btn_reset'] . '</button>';
19710449332Schris        }
19810449332Schris
199be1cc9aeSAndreas Gohr        echo '</p>';
20010449332Schris
201be1cc9aeSAndreas Gohr        echo '</form>';
202be1cc9aeSAndreas Gohr        echo '</div>';
20310449332Schris    }
20410449332Schris
20510449332Schris    /**
206253d4b48SGerrit Uitslag     * @param bool $prompts
207253d4b48SGerrit Uitslag     */
208d868eb89SAndreas Gohr    public function setupLocale($prompts = false)
209d868eb89SAndreas Gohr    {
21089d74a2fSchris        parent::setupLocale();
211a017dff4SAndreas Gohr        if (!$prompts || $this->promptsLocalized) return;
212d6fc72e1SAndreas Gohr        $this->lang = array_merge($this->lang, $this->configuration->getLangs());
213a017dff4SAndreas Gohr        $this->promptsLocalized = true;
21489d74a2fSchris    }
21589d74a2fSchris
2164fa2dffcSBen Coburn    /**
2174fa2dffcSBen Coburn     * Generates a two-level table of contents for the config plugin.
2184fa2dffcSBen Coburn     *
2194fa2dffcSBen Coburn     * @author Ben Coburn <btcoburn@silicodon.net>
220253d4b48SGerrit Uitslag     *
221253d4b48SGerrit Uitslag     * @return array
2224fa2dffcSBen Coburn     */
223d868eb89SAndreas Gohr    public function getTOC()
224d868eb89SAndreas Gohr    {
225b8595a66SAndreas Gohr        $this->setupLocale(true);
226b8595a66SAndreas Gohr
227685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
228467c1427SAndreas Gohr        $toc = [];
229d6fc72e1SAndreas Gohr        $check = false;
230685bdd2eSBen Coburn
231a017dff4SAndreas Gohr        // gather settings data into three sub arrays
232d6fc72e1SAndreas Gohr        $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []];
233077c27b2SAndreas Gohr        foreach ($this->configuration->getSettings() as $setting) {
234467c1427SAndreas Gohr            if ($setting instanceof SettingFieldset) {
235d6fc72e1SAndreas Gohr                $labels[$setting->getType()][] = $setting;
2364fa2dffcSBen Coburn            }
2374fa2dffcSBen Coburn        }
2384fa2dffcSBen Coburn
239d6fc72e1SAndreas Gohr        // top header
240cbfba956SMichael Hamann        $title = $this->getLang('_configuration_manager');
241d6fc72e1SAndreas Gohr        $toc[] = html_mktocitem(sectionID($title, $check), $title, 1);
242d6fc72e1SAndreas Gohr
243d6fc72e1SAndreas Gohr        // main entries
244d6fc72e1SAndreas Gohr        foreach (['dokuwiki', 'plugin', 'template'] as $section) {
245d6fc72e1SAndreas Gohr            if (empty($labels[$section])) continue; // no entries, skip
246d6fc72e1SAndreas Gohr
247d6fc72e1SAndreas Gohr            // create main header
248d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem(
249d6fc72e1SAndreas Gohr                $section . '_settings',
250d6fc72e1SAndreas Gohr                $this->getLang('_header_' . $section),
251d6fc72e1SAndreas Gohr                1
252d6fc72e1SAndreas Gohr            );
253d6fc72e1SAndreas Gohr
254d6fc72e1SAndreas Gohr            // create sub headers
255d6fc72e1SAndreas Gohr            foreach ($labels[$section] as $setting) {
256d6fc72e1SAndreas Gohr                /** @var SettingFieldset $setting */
257e1b31a95SBen Coburn                $name = $setting->prompt($this);
258d6fc72e1SAndreas Gohr                $toc[] = html_mktocitem($setting->getKey(), $name, 2);
259e1b31a95SBen Coburn            }
260e1b31a95SBen Coburn        }
261e1b31a95SBen Coburn
262d6fc72e1SAndreas Gohr        // undefined settings if allowed
263d6fc72e1SAndreas Gohr        if (count($this->configuration->getUndefined()) && $allow_debug) {
264d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
265d6fc72e1SAndreas Gohr        }
266d6fc72e1SAndreas Gohr
267d6fc72e1SAndreas Gohr        return $toc;
2684fa2dffcSBen Coburn    }
2694fa2dffcSBen Coburn
270253d4b48SGerrit Uitslag    /**
271253d4b48SGerrit Uitslag     * @param string $id
272253d4b48SGerrit Uitslag     * @param string $text
273253d4b48SGerrit Uitslag     */
274d868eb89SAndreas Gohr    protected function printH1($id, $text)
275d868eb89SAndreas Gohr    {
276be1cc9aeSAndreas Gohr        echo '<h1 id="' . $id . '">' . $text . '</h1>';
2774fa2dffcSBen Coburn    }
2784fa2dffcSBen Coburn
27961e35c35SAndreas Gohr    /**
28061e35c35SAndreas Gohr     * Adds a translation to this plugin's language array
28161e35c35SAndreas Gohr     *
282a017dff4SAndreas Gohr     * Used by some settings to set up dynamic translations
283a017dff4SAndreas Gohr     *
28461e35c35SAndreas Gohr     * @param string $key
28561e35c35SAndreas Gohr     * @param string $value
28661e35c35SAndreas Gohr     */
287d868eb89SAndreas Gohr    public function addLang($key, $value)
288d868eb89SAndreas Gohr    {
28961e35c35SAndreas Gohr        if (!$this->localised) $this->setupLocale();
29061e35c35SAndreas Gohr        $this->lang[$key] = $value;
29161e35c35SAndreas Gohr    }
29210449332Schris}
293