xref: /dokuwiki/lib/plugins/config/admin.php (revision 64159a61e94d0ce680071c8890e144982c3a8cbe)
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()
152                    ? ' class="default"'
153                    : ($setting->is_protected() ? ' class="protected"' : '');
154                $error = $setting->error()
155                    ? ' class="value error"'
156                    : ' class="value"';
157                $icon = $setting->caution()
158                    ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" '.
159                      'alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />'
160                    : '';
161
162                ptln('    <tr'.$class.'>');
163                ptln('      <td class="label">');
164                ptln('        <span class="outkey">'.$setting->_out_key(true, true).'</span>');
165                ptln('        '.$icon.$label);
166                ptln('      </td>');
167                ptln('      <td'.$error.'>'.$input.'</td>');
168                ptln('    </tr>');
169            }
170        }
171
172        ptln('  </table>');
173        ptln('  </div>');
174        if ($in_fieldset) {
175            ptln('  </fieldset>');
176        }
177
178        // show undefined settings list
179        if ($allow_debug && !empty($undefined_settings)) {
180            /**
181             * Callback for sorting settings
182             *
183             * @param setting $a
184             * @param setting $b
185             * @return int if $a is lower/equal/higher than $b
186             */
187            function _setting_natural_comparison($a, $b) {
188                return strnatcmp($a->_key, $b->_key);
189            }
190
191            usort($undefined_settings, '_setting_natural_comparison');
192            $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
193            ptln('<fieldset>');
194            ptln('<div class="table">');
195            ptln('<table class="inline">');
196            $undefined_setting_match = array();
197            foreach($undefined_settings as $setting) {
198                if (
199                    preg_match(
200                        '/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/',
201                        $setting->_key,
202                        $undefined_setting_match
203                    )
204                ) {
205                    $undefined_setting_key = $undefined_setting_match[1];
206                } else {
207                    $undefined_setting_key = $setting->_key;
208                }
209                ptln('  <tr>');
210                ptln('    <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.
211                     $this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>');
212                ptln('    <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
213                ptln('  </tr>');
214            }
215            ptln('</table>');
216            ptln('</div>');
217            ptln('</fieldset>');
218        }
219
220        // finish up form
221        ptln('<p>');
222        ptln('  <input type="hidden" name="do"     value="admin" />');
223        ptln('  <input type="hidden" name="page"   value="config" />');
224
225        if (!$this->_config->locked) {
226            ptln('  <input type="hidden" name="save"   value="1" />');
227            ptln('  <button type="submit" name="submit" accesskey="s">'.$lang['btn_save'].'</button>');
228            ptln('  <button type="reset">'.$lang['btn_reset'].'</button>');
229        }
230
231        ptln('</p>');
232
233        ptln('</form>');
234        ptln('</div>');
235    }
236
237    /**
238     * @return boolean   true - proceed with handle, false - don't proceed
239     */
240    protected function _restore_session() {
241
242        // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
243        // however they can't be changed without starting the session again
244        if (!headers_sent()) {
245            session_start();
246            $this->_session_started = true;
247        }
248
249        if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
250
251        $session = $_SESSION['PLUGIN_CONFIG'];
252        unset($_SESSION['PLUGIN_CONFIG']);
253
254        // still valid?
255        if (time() - $session['time'] > 120) return true;
256
257        switch ($session['state']) {
258            case 'updated' :
259                $this->_changed = true;
260                return false;
261        }
262
263        return true;
264    }
265
266    protected function _close_session() {
267      if ($this->_session_started) session_write_close();
268    }
269
270    /**
271     * @param bool $prompts
272     */
273    public function setupLocale($prompts=false) {
274
275        parent::setupLocale();
276        if (!$prompts || $this->_localised_prompts) return;
277
278        $this->_setup_localised_plugin_prompts();
279        $this->_localised_prompts = true;
280
281    }
282
283    /**
284     * @return bool
285     */
286    protected function _setup_localised_plugin_prompts() {
287        global $conf;
288
289        $langfile   = '/lang/'.$conf['lang'].'/settings.php';
290        $enlangfile = '/lang/en/settings.php';
291
292        if ($dh = opendir(DOKU_PLUGIN)) {
293            while (false !== ($plugin = readdir($dh))) {
294                if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
295                if (is_file(DOKU_PLUGIN.$plugin)) continue;
296
297                if (file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
298                    $lang = array();
299                    @include(DOKU_PLUGIN.$plugin.$enlangfile);
300                    if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
301                    foreach ($lang as $key => $value){
302                        $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
303                    }
304                }
305
306                // fill in the plugin name if missing (should exist for plugins with settings)
307                if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
308                    $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
309                      ucwords(str_replace('_', ' ', $plugin));
310                }
311            }
312            closedir($dh);
313      }
314
315        // the same for the active template
316        $tpl = $conf['template'];
317
318        if (file_exists(tpl_incdir().$enlangfile)){
319            $lang = array();
320            @include(tpl_incdir().$enlangfile);
321            if ($conf['lang'] != 'en') @include(tpl_incdir().$langfile);
322            foreach ($lang as $key => $value){
323                $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
324            }
325        }
326
327        // fill in the template name if missing (should exist for templates with settings)
328        if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
329            $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
330              ucwords(str_replace('_', ' ', $tpl));
331        }
332
333        return true;
334    }
335
336    /**
337     * Generates a two-level table of contents for the config plugin.
338     *
339     * @author Ben Coburn <btcoburn@silicodon.net>
340     *
341     * @return array
342     */
343    public function getTOC() {
344        if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
345        $this->setupLocale(true);
346
347        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
348
349        // gather toc data
350        $has_undefined = false;
351        $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
352        foreach($this->_config->setting as $setting) {
353            if (is_a($setting, 'setting_fieldset')) {
354                if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
355                    $toc['plugin'][] = $setting;
356                } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
357                    $toc['template'] = $setting;
358                } else {
359                    $toc['conf'][] = $setting;
360                }
361            } else if (!$has_undefined && is_a($setting, 'setting_undefined')) {
362                $has_undefined = true;
363            }
364        }
365
366        // build toc
367        $t = array();
368
369        $check = false;
370        $title = $this->getLang('_configuration_manager');
371        $t[] = html_mktocitem(sectionID($title, $check), $title, 1);
372        $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1);
373        /** @var setting $setting */
374        foreach($toc['conf'] as $setting) {
375            $name = $setting->prompt($this);
376            $t[] = html_mktocitem($setting->_key, $name, 2);
377        }
378        if (!empty($toc['plugin'])) {
379            $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1);
380        }
381        foreach($toc['plugin'] as $setting) {
382            $name = $setting->prompt($this);
383            $t[] = html_mktocitem($setting->_key, $name, 2);
384        }
385        if (isset($toc['template'])) {
386            $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1);
387            $setting = $toc['template'];
388            $name = $setting->prompt($this);
389            $t[] = html_mktocitem($setting->_key, $name, 2);
390        }
391        if ($has_undefined && $allow_debug) {
392            $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
393        }
394
395        return $t;
396    }
397
398    /**
399     * @param string $id
400     * @param string $text
401     */
402    protected function _print_h1($id, $text) {
403        ptln('<h1 id="'.$id.'">'.$text.'</h1>');
404    }
405
406    /**
407     * Adds a translation to this plugin's language array
408     *
409     * @param string $key
410     * @param string $value
411     */
412    public function addLang($key, $value) {
413        if (!$this->localised) $this->setupLocale();
414        $this->lang[$key] = $value;
415    }
416}
417