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