xref: /dokuwiki/lib/plugins/config/admin.php (revision d6fc72e108cb01b3391b9bea360e77ca03fbad2e)
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 */
910449332Schris
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 */
1910449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin {
2010449332Schris
21c6639e6aSAndreas Gohr    const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/';
22c6639e6aSAndreas Gohr
23077c27b2SAndreas Gohr    /** @var Configuration */
24077c27b2SAndreas Gohr    protected $configuration;
25077c27b2SAndreas Gohr
26a017dff4SAndreas Gohr    /** @var bool were there any errors in the submitted data? */
27a017dff4SAndreas Gohr    protected $hasErrors = false;
28a017dff4SAndreas Gohr
29a017dff4SAndreas Gohr    /** @var bool have the settings translations been loaded? */
30a017dff4SAndreas Gohr    protected $promptsLocalized = false;
31a017dff4SAndreas Gohr
32253d4b48SGerrit Uitslag    /**
33077c27b2SAndreas Gohr     * admin_plugin_config constructor.
34253d4b48SGerrit Uitslag     */
35077c27b2SAndreas Gohr    public function __construct() {
36077c27b2SAndreas Gohr        $this->configuration = new Configuration();
37077c27b2SAndreas Gohr    }
3810449332Schris
3910449332Schris    /**
4010449332Schris     * handle user request
4110449332Schris     */
4261e35c35SAndreas Gohr    public function handle() {
43392c9b52SHakan Sandell        global $ID, $INPUT;
4410449332Schris
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            }
653295f40aSAndreas Gohr            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'config'), true, '&'));
66a017dff4SAndreas Gohr        } else {
67a017dff4SAndreas Gohr            $this->hasErrors = true;
6810449332Schris        }
6910449332Schris    }
7010449332Schris
7110449332Schris    /**
7210449332Schris     * output appropriate html
7310449332Schris     */
7461e35c35SAndreas Gohr    public function html() {
75685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
7610449332Schris        global $lang;
77400497e1Schris        global $ID;
7810449332Schris
7989d74a2fSchris        $this->setupLocale(true);
8010449332Schris
81be1cc9aeSAndreas Gohr        echo $this->locale_xhtml('intro');
8210449332Schris
83be1cc9aeSAndreas Gohr        echo '<div id="config__manager">';
8410449332Schris
85077c27b2SAndreas Gohr        if($this->configuration->isLocked()) {
86be1cc9aeSAndreas Gohr            echo '<div class="info">' . $this->getLang('locked') . '</div>';
87077c27b2SAndreas Gohr        }
8810449332Schris
895d85efc6SAdrian Lang        // POST to script() instead of wl($ID) so config manager still works if
905d85efc6SAdrian Lang        // rewrite config is broken. Add $ID as hidden field to remember
915d85efc6SAdrian Lang        // current ID in most cases.
92be1cc9aeSAndreas Gohr        echo '<form action="' . script() . '" method="post">';
93be1cc9aeSAndreas Gohr        echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>';
94634d7150SAndreas Gohr        formSecurityToken();
95a1ef8b4dSAndreas Gohr        $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
9610449332Schris
974fa2dffcSBen Coburn        $in_fieldset = false;
984fa2dffcSBen Coburn        $first_plugin_fieldset = true;
994fa2dffcSBen Coburn        $first_template_fieldset = true;
100077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
101077c27b2SAndreas Gohr            if(is_a($setting, SettingHidden::class)) {
102685bdd2eSBen Coburn                continue;
103077c27b2SAndreas Gohr            } else if(is_a($setting, settingFieldset::class)) {
1044fa2dffcSBen Coburn                // config setting group
1054fa2dffcSBen Coburn                if($in_fieldset) {
106be1cc9aeSAndreas Gohr                    echo '</table>';
107be1cc9aeSAndreas Gohr                    echo '</div>';
108be1cc9aeSAndreas Gohr                    echo '</fieldset>';
1094fa2dffcSBen Coburn                } else {
1104fa2dffcSBen Coburn                    $in_fieldset = true;
1114fa2dffcSBen Coburn                }
112077c27b2SAndreas Gohr                // fixme this should probably be a function in setting:
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                } else if($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
126a017dff4SAndreas Gohr                list($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             */
165a1ef8b4dSAndreas Gohr            function settingNaturalComparison($a, $b) {
166077c27b2SAndreas Gohr                return strnatcmp($a->getKey(), $b->getKey());
167253d4b48SGerrit Uitslag            }
168253d4b48SGerrit Uitslag
169a1ef8b4dSAndreas Gohr            usort($undefined_settings, 'settingNaturalComparison');
170a1ef8b4dSAndreas Gohr            $this->printH1('undefined_settings', $this->getLang('_header_undefined'));
171be1cc9aeSAndreas Gohr            echo '<fieldset>';
172be1cc9aeSAndreas Gohr            echo '<div class="table">';
173be1cc9aeSAndreas Gohr            echo '<table class="inline">';
174685bdd2eSBen Coburn            $undefined_setting_match = array();
175685bdd2eSBen Coburn            foreach($undefined_settings as $setting) {
17664159a61SAndreas Gohr                if(
17764159a61SAndreas Gohr                preg_match(
178c6639e6aSAndreas Gohr                    '/^(?:plugin|tpl)' . Configuration::KEYMARKER . '.*?' . Configuration::KEYMARKER . '(.*)$/',
179077c27b2SAndreas Gohr                    $setting->getKey(),
18064159a61SAndreas Gohr                    $undefined_setting_match
18164159a61SAndreas Gohr                )
18264159a61SAndreas Gohr                ) {
183685bdd2eSBen Coburn                    $undefined_setting_key = $undefined_setting_match[1];
184685bdd2eSBen Coburn                } else {
185077c27b2SAndreas Gohr                    $undefined_setting_key = $setting->getKey();
186685bdd2eSBen Coburn                }
187be1cc9aeSAndreas Gohr                echo '<tr>';
188be1cc9aeSAndreas Gohr                echo
189077c27b2SAndreas Gohr                    '<td class="label"><span title="$meta[\'' . $undefined_setting_key . '\']">$' .
190*d6fc72e1SAndreas Gohr                    'conf' . '[\'' . $setting->getArrayKey() . '\']</span></td>';
191be1cc9aeSAndreas Gohr                echo '<td>' . $this->getLang('_msg_' . get_class($setting)) . '</td>';
192be1cc9aeSAndreas Gohr                echo '</tr>';
193685bdd2eSBen Coburn            }
194be1cc9aeSAndreas Gohr            echo '</table>';
195be1cc9aeSAndreas Gohr            echo '</div>';
196be1cc9aeSAndreas Gohr            echo '</fieldset>';
197685bdd2eSBen Coburn        }
198685bdd2eSBen Coburn
199685bdd2eSBen Coburn        // finish up form
200be1cc9aeSAndreas Gohr        echo '<p>';
201be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="do"     value="admin" />';
202be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="page"   value="config" />';
20310449332Schris
204077c27b2SAndreas Gohr        if(!$this->configuration->isLocked()) {
205be1cc9aeSAndreas Gohr            echo '<input type="hidden" name="save"   value="1" />';
206be1cc9aeSAndreas Gohr            echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>';
207be1cc9aeSAndreas Gohr            echo '<button type="reset">' . $lang['btn_reset'] . '</button>';
20810449332Schris        }
20910449332Schris
210be1cc9aeSAndreas Gohr        echo '</p>';
21110449332Schris
212be1cc9aeSAndreas Gohr        echo '</form>';
213be1cc9aeSAndreas Gohr        echo '</div>';
21410449332Schris    }
21510449332Schris
21610449332Schris    /**
217253d4b48SGerrit Uitslag     * @param bool $prompts
218253d4b48SGerrit Uitslag     */
21961e35c35SAndreas Gohr    public function setupLocale($prompts = false) {
22089d74a2fSchris        parent::setupLocale();
221a017dff4SAndreas Gohr        if(!$prompts || $this->promptsLocalized) return;
222*d6fc72e1SAndreas Gohr        $this->lang = array_merge($this->lang, $this->configuration->getLangs());
223a017dff4SAndreas Gohr        $this->promptsLocalized = true;
22489d74a2fSchris    }
22589d74a2fSchris
2264fa2dffcSBen Coburn    /**
2274fa2dffcSBen Coburn     * Generates a two-level table of contents for the config plugin.
2284fa2dffcSBen Coburn     *
2294fa2dffcSBen Coburn     * @author Ben Coburn <btcoburn@silicodon.net>
230253d4b48SGerrit Uitslag     *
231253d4b48SGerrit Uitslag     * @return array
2324fa2dffcSBen Coburn     */
23361e35c35SAndreas Gohr    public function getTOC() {
234b8595a66SAndreas Gohr        $this->setupLocale(true);
235b8595a66SAndreas Gohr
236685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
237*d6fc72e1SAndreas Gohr        $toc = array();
238*d6fc72e1SAndreas Gohr        $check = false;
239685bdd2eSBen Coburn
240a017dff4SAndreas Gohr        // gather settings data into three sub arrays
241*d6fc72e1SAndreas Gohr        $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []];
242077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
243*d6fc72e1SAndreas Gohr            if(is_a($setting, SettingFieldset::class)) {
244*d6fc72e1SAndreas Gohr                $labels[$setting->getType()][] = $setting;
2454fa2dffcSBen Coburn            }
2464fa2dffcSBen Coburn        }
2474fa2dffcSBen Coburn
248*d6fc72e1SAndreas Gohr        // top header
249cbfba956SMichael Hamann        $title = $this->getLang('_configuration_manager');
250*d6fc72e1SAndreas Gohr        $toc[] = html_mktocitem(sectionID($title, $check), $title, 1);
251*d6fc72e1SAndreas Gohr
252*d6fc72e1SAndreas Gohr        // main entries
253*d6fc72e1SAndreas Gohr        foreach(['dokuwiki', 'plugin', 'template'] as $section) {
254*d6fc72e1SAndreas Gohr            if(empty($labels[$section])) continue; // no entries, skip
255*d6fc72e1SAndreas Gohr
256*d6fc72e1SAndreas Gohr            // create main header
257*d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem(
258*d6fc72e1SAndreas Gohr                $section . '_settings',
259*d6fc72e1SAndreas Gohr                $this->getLang('_header_'.$section),
260*d6fc72e1SAndreas Gohr                1
261*d6fc72e1SAndreas Gohr            );
262*d6fc72e1SAndreas Gohr
263*d6fc72e1SAndreas Gohr            // create sub headers
264*d6fc72e1SAndreas Gohr            foreach($labels[$section] as $setting) {
265*d6fc72e1SAndreas Gohr                /** @var SettingFieldset $setting */
266e1b31a95SBen Coburn                $name = $setting->prompt($this);
267*d6fc72e1SAndreas Gohr                $toc[] = html_mktocitem($setting->getKey(), $name, 2);
268e1b31a95SBen Coburn            }
269e1b31a95SBen Coburn        }
270e1b31a95SBen Coburn
271*d6fc72e1SAndreas Gohr        // undefined settings if allowed
272*d6fc72e1SAndreas Gohr        if(count($this->configuration->getUndefined()) && $allow_debug) {
273*d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
274*d6fc72e1SAndreas Gohr        }
275*d6fc72e1SAndreas Gohr
276*d6fc72e1SAndreas Gohr        return $toc;
2774fa2dffcSBen Coburn    }
2784fa2dffcSBen Coburn
279253d4b48SGerrit Uitslag    /**
280253d4b48SGerrit Uitslag     * @param string $id
281253d4b48SGerrit Uitslag     * @param string $text
282253d4b48SGerrit Uitslag     */
283a1ef8b4dSAndreas Gohr    protected function printH1($id, $text) {
284be1cc9aeSAndreas Gohr        echo '<h1 id="' . $id . '">' . $text . '</h1>';
2854fa2dffcSBen Coburn    }
2864fa2dffcSBen Coburn
28761e35c35SAndreas Gohr    /**
28861e35c35SAndreas Gohr     * Adds a translation to this plugin's language array
28961e35c35SAndreas Gohr     *
290a017dff4SAndreas Gohr     * Used by some settings to set up dynamic translations
291a017dff4SAndreas Gohr     *
29261e35c35SAndreas Gohr     * @param string $key
29361e35c35SAndreas Gohr     * @param string $value
29461e35c35SAndreas Gohr     */
29561e35c35SAndreas Gohr    public function addLang($key, $value) {
29661e35c35SAndreas Gohr        if(!$this->localised) $this->setupLocale();
29761e35c35SAndreas Gohr        $this->lang[$key] = $value;
29861e35c35SAndreas Gohr    }
29910449332Schris}
300