xref: /dokuwiki/lib/plugins/config/admin.php (revision d868eb89f182718a31113373a6272670bd7f8012)
110449332Schris<?php
210449332Schris/**
310449332Schris * Configuration Manager admin plugin
410449332Schris *
510449332Schris * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
610449332Schris * @author     Christopher Smith <chris@jalakai.co.uk>
7685bdd2eSBen Coburn * @author     Ben Coburn <btcoburn@silicodon.net>
810449332Schris */
98553d24dSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
10c6639e6aSAndreas Gohruse dokuwiki\plugin\config\core\Configuration;
110a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\Setting;
120a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingFieldset;
130a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingHidden;
1410449332Schris
1510449332Schris/**
1610449332Schris * All DokuWiki plugins to extend the admin function
1710449332Schris * need to inherit from this class
1810449332Schris */
198c7c53b0SAndreas Gohrclass admin_plugin_config extends AdminPlugin
208c7c53b0SAndreas Gohr{
2110449332Schris
22bf9be0e3SAndreas Gohr    protected const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/';
23c6639e6aSAndreas Gohr
24077c27b2SAndreas Gohr    /** @var Configuration */
25077c27b2SAndreas Gohr    protected $configuration;
26077c27b2SAndreas Gohr
27a017dff4SAndreas Gohr    /** @var bool were there any errors in the submitted data? */
28a017dff4SAndreas Gohr    protected $hasErrors = false;
29a017dff4SAndreas Gohr
30a017dff4SAndreas Gohr    /** @var bool have the settings translations been loaded? */
31a017dff4SAndreas Gohr    protected $promptsLocalized = false;
32a017dff4SAndreas Gohr
3310449332Schris
3410449332Schris    /**
3510449332Schris     * handle user request
3610449332Schris     */
37*d868eb89SAndreas Gohr    public function handle()
38*d868eb89SAndreas Gohr    {
39392c9b52SHakan Sandell        global $ID, $INPUT;
4010449332Schris
41f8dcd5b0SAndreas Gohr        // always initialize the configuration
42f8dcd5b0SAndreas Gohr        $this->configuration = new Configuration();
43f8dcd5b0SAndreas Gohr
44077c27b2SAndreas Gohr        if(!$INPUT->bool('save') || !checkSecurityToken()) {
45253d4b48SGerrit Uitslag            return;
46253d4b48SGerrit Uitslag        }
4710449332Schris
4810449332Schris        // don't go any further if the configuration is locked
49077c27b2SAndreas Gohr        if($this->configuration->isLocked()) return;
50077c27b2SAndreas Gohr
51077c27b2SAndreas Gohr        // update settings and redirect of successful
52077c27b2SAndreas Gohr        $ok = $this->configuration->updateSettings($INPUT->arr('config'));
53077c27b2SAndreas Gohr        if($ok) { // no errors
54077c27b2SAndreas Gohr            try {
55077c27b2SAndreas Gohr                if($this->configuration->hasChanged()) {
56077c27b2SAndreas Gohr                    $this->configuration->save();
57077c27b2SAndreas Gohr                } else {
58077c27b2SAndreas Gohr                    $this->configuration->touch();
59253d4b48SGerrit Uitslag                }
60e98b5e44SAndreas Gohr                msg($this->getLang('updated'), 1);
61077c27b2SAndreas Gohr            } catch(Exception $e) {
62077c27b2SAndreas Gohr                msg($this->getLang('error'), -1);
6310449332Schris            }
64467c1427SAndreas Gohr            send_redirect(wl($ID, ['do' => 'admin', 'page' => 'config'], true, '&'));
65a017dff4SAndreas Gohr        } else {
66a017dff4SAndreas Gohr            $this->hasErrors = true;
67a6b97c7dSGerrit Uitslag            msg($this->getLang('error'), -1);
6810449332Schris        }
6910449332Schris    }
7010449332Schris
7110449332Schris    /**
7210449332Schris     * output appropriate html
7310449332Schris     */
74*d868eb89SAndreas Gohr    public function html()
75*d868eb89SAndreas Gohr    {
76685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
7710449332Schris        global $lang;
78400497e1Schris        global $ID;
7910449332Schris
8089d74a2fSchris        $this->setupLocale(true);
8110449332Schris
82be1cc9aeSAndreas Gohr        echo $this->locale_xhtml('intro');
8310449332Schris
84be1cc9aeSAndreas Gohr        echo '<div id="config__manager">';
8510449332Schris
86077c27b2SAndreas Gohr        if($this->configuration->isLocked()) {
87be1cc9aeSAndreas Gohr            echo '<div class="info">' . $this->getLang('locked') . '</div>';
88077c27b2SAndreas Gohr        }
8910449332Schris
905d85efc6SAdrian Lang        // POST to script() instead of wl($ID) so config manager still works if
915d85efc6SAdrian Lang        // rewrite config is broken. Add $ID as hidden field to remember
925d85efc6SAdrian Lang        // current ID in most cases.
93923e149aSMichael Große        echo '<form id="dw__configform" action="' . script() . '" method="post">';
94be1cc9aeSAndreas Gohr        echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>';
95634d7150SAndreas Gohr        formSecurityToken();
96a1ef8b4dSAndreas Gohr        $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
9710449332Schris
984fa2dffcSBen Coburn        $in_fieldset = false;
994fa2dffcSBen Coburn        $first_plugin_fieldset = true;
1004fa2dffcSBen Coburn        $first_template_fieldset = true;
101077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
102467c1427SAndreas Gohr            if ($setting instanceof SettingHidden) {
103685bdd2eSBen Coburn                continue;
104467c1427SAndreas Gohr            } elseif ($setting instanceof SettingFieldset) {
1054fa2dffcSBen Coburn                // config setting group
1064fa2dffcSBen Coburn                if($in_fieldset) {
107be1cc9aeSAndreas Gohr                    echo '</table>';
108be1cc9aeSAndreas Gohr                    echo '</div>';
109be1cc9aeSAndreas Gohr                    echo '</fieldset>';
1104fa2dffcSBen Coburn                } else {
1114fa2dffcSBen Coburn                    $in_fieldset = true;
1124fa2dffcSBen Coburn                }
113a017dff4SAndreas Gohr                if ($first_plugin_fieldset && $setting->getType() == 'plugin') {
114a1ef8b4dSAndreas Gohr                    $this->printH1('plugin_settings', $this->getLang('_header_plugin'));
1154fa2dffcSBen Coburn                    $first_plugin_fieldset = false;
116a017dff4SAndreas Gohr                } elseif ($first_template_fieldset && $setting->getType() == 'template') {
117a1ef8b4dSAndreas Gohr                    $this->printH1('template_settings', $this->getLang('_header_template'));
1184fa2dffcSBen Coburn                    $first_template_fieldset = false;
1194fa2dffcSBen Coburn                }
120be1cc9aeSAndreas Gohr                echo '<fieldset id="' . $setting->getKey() . '">';
121be1cc9aeSAndreas Gohr                echo '<legend>' . $setting->prompt($this) . '</legend>';
122be1cc9aeSAndreas Gohr                echo '<div class="table">';
123be1cc9aeSAndreas Gohr                echo '<table class="inline">';
1244fa2dffcSBen Coburn            } else {
1254fa2dffcSBen Coburn                // config settings
126467c1427SAndreas Gohr                [$label, $input] = $setting->html($this, $this->hasErrors);
12710449332Schris
1285c17d2d3SAndreas Gohr                $class = $setting->isDefault()
12964159a61SAndreas Gohr                    ? ' class="default"'
1305c17d2d3SAndreas Gohr                    : ($setting->isProtected() ? ' class="protected"' : '');
1315c17d2d3SAndreas Gohr                $error = $setting->hasError()
13264159a61SAndreas Gohr                    ? ' class="value error"'
13364159a61SAndreas Gohr                    : ' class="value"';
13464159a61SAndreas Gohr                $icon = $setting->caution()
135c6639e6aSAndreas Gohr                    ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' .
13664159a61SAndreas Gohr                    'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />'
13764159a61SAndreas Gohr                    : '';
13810449332Schris
139be1cc9aeSAndreas Gohr                echo '<tr' . $class . '>';
140be1cc9aeSAndreas Gohr                echo '<td class="label">';
141be1cc9aeSAndreas Gohr                echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>';
142be1cc9aeSAndreas Gohr                echo $icon . $label;
143be1cc9aeSAndreas Gohr                echo '</td>';
144be1cc9aeSAndreas Gohr                echo '<td' . $error . '>' . $input . '</td>';
145be1cc9aeSAndreas Gohr                echo '</tr>';
14610449332Schris            }
1474fa2dffcSBen Coburn        }
14810449332Schris
149be1cc9aeSAndreas Gohr        echo '</table>';
150be1cc9aeSAndreas Gohr        echo '</div>';
1514fa2dffcSBen Coburn        if($in_fieldset) {
152be1cc9aeSAndreas Gohr            echo '</fieldset>';
1534fa2dffcSBen Coburn        }
15410449332Schris
155685bdd2eSBen Coburn        // show undefined settings list
156077c27b2SAndreas Gohr        $undefined_settings = $this->configuration->getUndefined();
157685bdd2eSBen Coburn        if($allow_debug && !empty($undefined_settings)) {
158253d4b48SGerrit Uitslag            /**
159253d4b48SGerrit Uitslag             * Callback for sorting settings
160253d4b48SGerrit Uitslag             *
161077c27b2SAndreas Gohr             * @param Setting $a
162077c27b2SAndreas Gohr             * @param Setting $b
163253d4b48SGerrit Uitslag             * @return int if $a is lower/equal/higher than $b
164253d4b48SGerrit Uitslag             */
165*d868eb89SAndreas Gohr            function settingNaturalComparison($a, $b)
166*d868eb89SAndreas Gohr            {
167077c27b2SAndreas Gohr                return strnatcmp($a->getKey(), $b->getKey());
168253d4b48SGerrit Uitslag            }
169253d4b48SGerrit Uitslag
170a1ef8b4dSAndreas Gohr            usort($undefined_settings, 'settingNaturalComparison');
171a1ef8b4dSAndreas Gohr            $this->printH1('undefined_settings', $this->getLang('_header_undefined'));
172be1cc9aeSAndreas Gohr            echo '<fieldset>';
173be1cc9aeSAndreas Gohr            echo '<div class="table">';
174be1cc9aeSAndreas Gohr            echo '<table class="inline">';
175685bdd2eSBen Coburn            foreach($undefined_settings as $setting) {
176467c1427SAndreas Gohr                [$label, $input] = $setting->html($this);
177be1cc9aeSAndreas Gohr                echo '<tr>';
178e7296041SAndreas Gohr                echo '<td class="label">' . $label . '</td>';
179e7296041SAndreas Gohr                echo '<td>' . $input . '</td>';
180be1cc9aeSAndreas Gohr                echo '</tr>';
181685bdd2eSBen Coburn            }
182be1cc9aeSAndreas Gohr            echo '</table>';
183be1cc9aeSAndreas Gohr            echo '</div>';
184be1cc9aeSAndreas Gohr            echo '</fieldset>';
185685bdd2eSBen Coburn        }
186685bdd2eSBen Coburn
187685bdd2eSBen Coburn        // finish up form
188be1cc9aeSAndreas Gohr        echo '<p>';
189be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="do"     value="admin" />';
190be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="page"   value="config" />';
19110449332Schris
192077c27b2SAndreas Gohr        if(!$this->configuration->isLocked()) {
193be1cc9aeSAndreas Gohr            echo '<input type="hidden" name="save"   value="1" />';
194be1cc9aeSAndreas Gohr            echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>';
195be1cc9aeSAndreas Gohr            echo '<button type="reset">' . $lang['btn_reset'] . '</button>';
19610449332Schris        }
19710449332Schris
198be1cc9aeSAndreas Gohr        echo '</p>';
19910449332Schris
200be1cc9aeSAndreas Gohr        echo '</form>';
201be1cc9aeSAndreas Gohr        echo '</div>';
20210449332Schris    }
20310449332Schris
20410449332Schris    /**
205253d4b48SGerrit Uitslag     * @param bool $prompts
206253d4b48SGerrit Uitslag     */
207*d868eb89SAndreas Gohr    public function setupLocale($prompts = false)
208*d868eb89SAndreas Gohr    {
20989d74a2fSchris        parent::setupLocale();
210a017dff4SAndreas Gohr        if(!$prompts || $this->promptsLocalized) return;
211d6fc72e1SAndreas Gohr        $this->lang = array_merge($this->lang, $this->configuration->getLangs());
212a017dff4SAndreas Gohr        $this->promptsLocalized = true;
21389d74a2fSchris    }
21489d74a2fSchris
2154fa2dffcSBen Coburn    /**
2164fa2dffcSBen Coburn     * Generates a two-level table of contents for the config plugin.
2174fa2dffcSBen Coburn     *
2184fa2dffcSBen Coburn     * @author Ben Coburn <btcoburn@silicodon.net>
219253d4b48SGerrit Uitslag     *
220253d4b48SGerrit Uitslag     * @return array
2214fa2dffcSBen Coburn     */
222*d868eb89SAndreas Gohr    public function getTOC()
223*d868eb89SAndreas Gohr    {
224b8595a66SAndreas Gohr        $this->setupLocale(true);
225b8595a66SAndreas Gohr
226685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
227467c1427SAndreas Gohr        $toc = [];
228d6fc72e1SAndreas Gohr        $check = false;
229685bdd2eSBen Coburn
230a017dff4SAndreas Gohr        // gather settings data into three sub arrays
231d6fc72e1SAndreas Gohr        $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []];
232077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
233467c1427SAndreas Gohr            if($setting instanceof SettingFieldset) {
234d6fc72e1SAndreas Gohr                $labels[$setting->getType()][] = $setting;
2354fa2dffcSBen Coburn            }
2364fa2dffcSBen Coburn        }
2374fa2dffcSBen Coburn
238d6fc72e1SAndreas Gohr        // top header
239cbfba956SMichael Hamann        $title = $this->getLang('_configuration_manager');
240d6fc72e1SAndreas Gohr        $toc[] = html_mktocitem(sectionID($title, $check), $title, 1);
241d6fc72e1SAndreas Gohr
242d6fc72e1SAndreas Gohr        // main entries
243d6fc72e1SAndreas Gohr        foreach(['dokuwiki', 'plugin', 'template'] as $section) {
244d6fc72e1SAndreas Gohr            if(empty($labels[$section])) continue; // no entries, skip
245d6fc72e1SAndreas Gohr
246d6fc72e1SAndreas Gohr            // create main header
247d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem(
248d6fc72e1SAndreas Gohr                $section . '_settings',
249d6fc72e1SAndreas Gohr                $this->getLang('_header_' . $section),
250d6fc72e1SAndreas Gohr                1
251d6fc72e1SAndreas Gohr            );
252d6fc72e1SAndreas Gohr
253d6fc72e1SAndreas Gohr            // create sub headers
254d6fc72e1SAndreas Gohr            foreach($labels[$section] as $setting) {
255d6fc72e1SAndreas Gohr                /** @var SettingFieldset $setting */
256e1b31a95SBen Coburn                $name = $setting->prompt($this);
257d6fc72e1SAndreas Gohr                $toc[] = html_mktocitem($setting->getKey(), $name, 2);
258e1b31a95SBen Coburn            }
259e1b31a95SBen Coburn        }
260e1b31a95SBen Coburn
261d6fc72e1SAndreas Gohr        // undefined settings if allowed
262d6fc72e1SAndreas Gohr        if(count($this->configuration->getUndefined()) && $allow_debug) {
263d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
264d6fc72e1SAndreas Gohr        }
265d6fc72e1SAndreas Gohr
266d6fc72e1SAndreas Gohr        return $toc;
2674fa2dffcSBen Coburn    }
2684fa2dffcSBen Coburn
269253d4b48SGerrit Uitslag    /**
270253d4b48SGerrit Uitslag     * @param string $id
271253d4b48SGerrit Uitslag     * @param string $text
272253d4b48SGerrit Uitslag     */
273*d868eb89SAndreas Gohr    protected function printH1($id, $text)
274*d868eb89SAndreas Gohr    {
275be1cc9aeSAndreas Gohr        echo '<h1 id="' . $id . '">' . $text . '</h1>';
2764fa2dffcSBen Coburn    }
2774fa2dffcSBen Coburn
27861e35c35SAndreas Gohr    /**
27961e35c35SAndreas Gohr     * Adds a translation to this plugin's language array
28061e35c35SAndreas Gohr     *
281a017dff4SAndreas Gohr     * Used by some settings to set up dynamic translations
282a017dff4SAndreas Gohr     *
28361e35c35SAndreas Gohr     * @param string $key
28461e35c35SAndreas Gohr     * @param string $value
28561e35c35SAndreas Gohr     */
286*d868eb89SAndreas Gohr    public function addLang($key, $value)
287*d868eb89SAndreas Gohr    {
28861e35c35SAndreas Gohr        if(!$this->localised) $this->setupLocale();
28961e35c35SAndreas Gohr        $this->lang[$key] = $value;
29061e35c35SAndreas Gohr    }
29110449332Schris}
292