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