xref: /dokuwiki/lib/plugins/config/admin.php (revision b4f2363aa1360136c8a826f09aaebc6505211c73)
1<?php
2/**
3 * Configuration Manager admin plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Christopher Smith <chris@jalakai.co.uk>
7 * @author     Ben Coburn <btcoburn@silicodon.net>
8 */
9
10define('CM_KEYMARKER','____');            // used for settings with multiple dimensions of array indices
11
12define('PLUGIN_SELF',dirname(__FILE__).'/');
13define('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
14if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/config/images/');
15
16require_once(PLUGIN_SELF.'settings/config.class.php');  // main configuration class and generic settings classes
17require_once(PLUGIN_SELF.'settings/extra.class.php');   // settings classes specific to these settings
18
19/**
20 * All DokuWiki plugins to extend the admin function
21 * need to inherit from this class
22 */
23class admin_plugin_config extends DokuWiki_Admin_Plugin {
24
25    protected $_file = PLUGIN_METADATA;
26    protected $_config = null;
27    protected $_input = null;
28    protected $_changed = false;          // set to true if configuration has altered
29    protected $_error = false;
30    protected $_session_started = false;
31    protected $_localised_prompts = false;
32
33    /**
34     * @return int
35     */
36    public function getMenuSort() { return 100; }
37
38    /**
39     * handle user request
40     */
41    public function handle() {
42        global $ID, $INPUT;
43
44        if(!$this->_restore_session() || $INPUT->int('save') != 1 || !checkSecurityToken()) {
45            $this->_close_session();
46            return;
47        }
48
49        if(is_null($this->_config)) {
50            $this->_config = new configuration($this->_file);
51        }
52
53        // don't go any further if the configuration is locked
54        if($this->_config->locked) {
55            $this->_close_session();
56            return;
57        }
58
59        $this->_input = $INPUT->arr('config');
60
61        foreach ($this->_config->setting as $key => $value){
62            $input = isset($this->_input[$key]) ? $this->_input[$key] : null;
63            if ($this->_config->setting[$key]->update($input)) {
64                $this->_changed = true;
65            }
66            if ($this->_config->setting[$key]->error()) $this->_error = true;
67        }
68
69        if ($this->_changed  && !$this->_error) {
70            $this->_config->save_settings($this->getPluginName());
71
72            // save state & force a page reload to get the new settings to take effect
73            $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
74            $this->_close_session();
75            send_redirect(wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
76            exit();
77        } elseif(!$this->_error) {
78            $this->_config->touch_settings(); // just touch to refresh cache
79        }
80
81        $this->_close_session();
82    }
83
84    /**
85     * output appropriate html
86     */
87    public function html() {
88        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
89        global $lang;
90        global $ID;
91
92        if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
93        $this->setupLocale(true);
94
95        print $this->locale_xhtml('intro');
96
97        ptln('<div id="config__manager">');
98
99        if ($this->_config->locked)
100            ptln('<div class="info">'.$this->getLang('locked').'</div>');
101        elseif ($this->_error)
102            ptln('<div class="error">'.$this->getLang('error').'</div>');
103        elseif ($this->_changed)
104            ptln('<div class="success">'.$this->getLang('updated').'</div>');
105
106        // POST to script() instead of wl($ID) so config manager still works if
107        // rewrite config is broken. Add $ID as hidden field to remember
108        // current ID in most cases.
109        ptln('<form action="'.script().'" method="post">');
110        ptln('<div class="no"><input type="hidden" name="id" value="'.$ID.'" /></div>');
111        formSecurityToken();
112        $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
113
114        /** @var setting[] $undefined_settings */
115        $undefined_settings = array();
116        $in_fieldset = false;
117        $first_plugin_fieldset = true;
118        $first_template_fieldset = true;
119        foreach($this->_config->setting as $setting) {
120            if (is_a($setting, 'setting_hidden')) {
121                // skip hidden (and undefined) settings
122                if ($allow_debug && is_a($setting, 'setting_undefined')) {
123                    $undefined_settings[] = $setting;
124                } else {
125                    continue;
126                }
127            } else if (is_a($setting, 'setting_fieldset')) {
128                // config setting group
129                if ($in_fieldset) {
130                    ptln('  </table>');
131                    ptln('  </div>');
132                    ptln('  </fieldset>');
133                } else {
134                    $in_fieldset = true;
135                }
136                if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
137                    $this->_print_h1('plugin_settings', $this->getLang('_header_plugin'));
138                    $first_plugin_fieldset = false;
139                } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
140                    $this->_print_h1('template_settings', $this->getLang('_header_template'));
141                    $first_template_fieldset = false;
142                }
143                ptln('  <fieldset id="'.$setting->_key.'">');
144                ptln('  <legend>'.$setting->prompt($this).'</legend>');
145                ptln('  <div class="table">');
146                ptln('  <table class="inline">');
147            } else {
148                // config settings
149                list($label,$input) = $setting->html($this, $this->_error);
150
151                $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
152                $error = $setting->error() ? ' class="value error"' : ' class="value"';
153                $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : '';
154
155                ptln('    <tr'.$class.'>');
156                ptln('      <td class="label">');
157                ptln('        <span class="outkey">'.$setting->_out_key(true, true).'</span>');
158                ptln('        '.$icon.$label);
159                ptln('      </td>');
160                ptln('      <td'.$error.'>'.$input.'</td>');
161                ptln('    </tr>');
162            }
163        }
164
165        ptln('  </table>');
166        ptln('  </div>');
167        if ($in_fieldset) {
168            ptln('  </fieldset>');
169        }
170
171        // show undefined settings list
172        if ($allow_debug && !empty($undefined_settings)) {
173            /**
174             * Callback for sorting settings
175             *
176             * @param setting $a
177             * @param setting $b
178             * @return int if $a is lower/equal/higher than $b
179             */
180            function _setting_natural_comparison($a, $b) {
181                return strnatcmp($a->_key, $b->_key);
182            }
183
184            usort($undefined_settings, '_setting_natural_comparison');
185            $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
186            ptln('<fieldset>');
187            ptln('<div class="table">');
188            ptln('<table class="inline">');
189            $undefined_setting_match = array();
190            foreach($undefined_settings as $setting) {
191                if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) {
192                    $undefined_setting_key = $undefined_setting_match[1];
193                } else {
194                    $undefined_setting_key = $setting->_key;
195                }
196                ptln('  <tr>');
197                ptln('    <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>');
198                ptln('    <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
199                ptln('  </tr>');
200            }
201            ptln('</table>');
202            ptln('</div>');
203            ptln('</fieldset>');
204        }
205
206        // finish up form
207        ptln('<p>');
208        ptln('  <input type="hidden" name="do"     value="admin" />');
209        ptln('  <input type="hidden" name="page"   value="config" />');
210
211        if (!$this->_config->locked) {
212            ptln('  <input type="hidden" name="save"   value="1" />');
213            ptln('  <button type="submit" name="submit" accesskey="s">'.$lang['btn_save'].'</button>');
214            ptln('  <button type="reset">'.$lang['btn_reset'].'</button>');
215        }
216
217        ptln('</p>');
218
219        ptln('</form>');
220        ptln('</div>');
221    }
222
223    /**
224     * @return boolean   true - proceed with handle, false - don't proceed
225     */
226    protected function _restore_session() {
227
228        // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
229        // however they can't be changed without starting the session again
230        if (!headers_sent()) {
231            session_start();
232            $this->_session_started = true;
233        }
234
235        if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
236
237        $session = $_SESSION['PLUGIN_CONFIG'];
238        unset($_SESSION['PLUGIN_CONFIG']);
239
240        // still valid?
241        if (time() - $session['time'] > 120) return true;
242
243        switch ($session['state']) {
244            case 'updated' :
245                $this->_changed = true;
246                return false;
247        }
248
249        return true;
250    }
251
252    protected function _close_session() {
253      if ($this->_session_started) session_write_close();
254    }
255
256    /**
257     * @param bool $prompts
258     */
259    public function setupLocale($prompts=false) {
260
261        parent::setupLocale();
262        if (!$prompts || $this->_localised_prompts) return;
263
264        $this->_setup_localised_plugin_prompts();
265        $this->_localised_prompts = true;
266
267    }
268
269    /**
270     * @return bool
271     */
272    protected function _setup_localised_plugin_prompts() {
273        global $conf;
274
275        $langfile   = '/lang/'.$conf['lang'].'/settings.php';
276        $enlangfile = '/lang/en/settings.php';
277
278        if ($dh = opendir(DOKU_PLUGIN)) {
279            while (false !== ($plugin = readdir($dh))) {
280                if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
281                if (is_file(DOKU_PLUGIN.$plugin)) continue;
282
283                if (file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
284                    $lang = array();
285                    @include(DOKU_PLUGIN.$plugin.$enlangfile);
286                    if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
287                    foreach ($lang as $key => $value){
288                        $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
289                    }
290                }
291
292                // fill in the plugin name if missing (should exist for plugins with settings)
293                if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
294                    $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
295                      ucwords(str_replace('_', ' ', $plugin));
296                }
297            }
298            closedir($dh);
299      }
300
301        // the same for the active template
302        $tpl = $conf['template'];
303
304        if (file_exists(tpl_incdir().$enlangfile)){
305            $lang = array();
306            @include(tpl_incdir().$enlangfile);
307            if ($conf['lang'] != 'en') @include(tpl_incdir().$langfile);
308            foreach ($lang as $key => $value){
309                $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
310            }
311        }
312
313        // fill in the template name if missing (should exist for templates with settings)
314        if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
315            $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
316              ucwords(str_replace('_', ' ', $tpl));
317        }
318
319        return true;
320    }
321
322    /**
323     * Generates a two-level table of contents for the config plugin.
324     *
325     * @author Ben Coburn <btcoburn@silicodon.net>
326     *
327     * @return array
328     */
329    public function getTOC() {
330        if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
331        $this->setupLocale(true);
332
333        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
334
335        // gather toc data
336        $has_undefined = false;
337        $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
338        foreach($this->_config->setting as $setting) {
339            if (is_a($setting, 'setting_fieldset')) {
340                if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
341                    $toc['plugin'][] = $setting;
342                } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
343                    $toc['template'] = $setting;
344                } else {
345                    $toc['conf'][] = $setting;
346                }
347            } else if (!$has_undefined && is_a($setting, 'setting_undefined')) {
348                $has_undefined = true;
349            }
350        }
351
352        // build toc
353        $t = array();
354
355        $check = false;
356        $title = $this->getLang('_configuration_manager');
357        $t[] = html_mktocitem(sectionID($title, $check), $title, 1);
358        $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1);
359        /** @var setting $setting */
360        foreach($toc['conf'] as $setting) {
361            $name = $setting->prompt($this);
362            $t[] = html_mktocitem($setting->_key, $name, 2);
363        }
364        if (!empty($toc['plugin'])) {
365            $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1);
366        }
367        foreach($toc['plugin'] as $setting) {
368            $name = $setting->prompt($this);
369            $t[] = html_mktocitem($setting->_key, $name, 2);
370        }
371        if (isset($toc['template'])) {
372            $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1);
373            $setting = $toc['template'];
374            $name = $setting->prompt($this);
375            $t[] = html_mktocitem($setting->_key, $name, 2);
376        }
377        if ($has_undefined && $allow_debug) {
378            $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
379        }
380
381        return $t;
382    }
383
384    /**
385     * @param string $id
386     * @param string $text
387     */
388    protected function _print_h1($id, $text) {
389        ptln('<h1 id="'.$id.'">'.$text.'</h1>');
390    }
391
392    /**
393     * Adds a translation to this plugin's language array
394     *
395     * @param string $key
396     * @param string $value
397     */
398    public function addLang($key, $value) {
399        if (!$this->localised) $this->setupLocale();
400        $this->lang[$key] = $value;
401    }
402}
403