xref: /dokuwiki/lib/plugins/config/admin.php (revision 579b0f7e8d80287b11fd441dfa68d15e9d4bb74c)
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><span title="$'.$this->_config->_name.'[\''.$setting->_out_key().'\']">'.$label.'</span></td>');
156          ptln('      <td'.$error.'>'.$input.'</td>');
157          ptln('    </tr>');
158        }
159      }
160
161      ptln('  </table>');
162      if ($in_fieldset) {
163        ptln('  </fieldset>');
164      }
165
166      // show undefined settings list
167      if ($allow_debug && !empty($undefined_settings)) {
168        function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); }
169        usort($undefined_settings, '_setting_natural_comparison');
170        $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
171        ptln('<fieldset>');
172        ptln('<table class="inline">');
173        $undefined_setting_match = array();
174        foreach($undefined_settings as $setting) {
175          if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) {
176            $undefined_setting_key = $undefined_setting_match[1];
177          } else {
178            $undefined_setting_key = $setting->_key;
179          }
180          ptln('  <tr>');
181          ptln('    <td><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>');
182          ptln('    <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
183          ptln('  </tr>');
184        }
185        ptln('</table>');
186        ptln('</fieldset>');
187      }
188
189      // finish up form
190      ptln('<p>');
191      ptln('  <input type="hidden" name="do"     value="admin" />');
192      ptln('  <input type="hidden" name="page"   value="config" />');
193
194      if (!$this->_config->locked) {
195        ptln('  <input type="hidden" name="save"   value="1" />');
196        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
197        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
198      }
199
200      ptln('</p>');
201
202      ptln('</form>');
203      ptln('</div>');
204    }
205
206    /**
207     * @return boolean   true - proceed with handle, false - don't proceed
208     */
209    function _restore_session() {
210
211      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
212      // however they can't be changed without starting the session again
213      if (!headers_sent()) {
214        session_start();
215        $this->_session_started = true;
216      }
217
218      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
219
220      $session = $_SESSION['PLUGIN_CONFIG'];
221      unset($_SESSION['PLUGIN_CONFIG']);
222
223      // still valid?
224      if (time() - $session['time'] > 120) return true;
225
226      switch ($session['state']) {
227        case 'updated' :
228          $this->_changed = true;
229          return false;
230      }
231
232      return true;
233    }
234
235    function _close_session() {
236      if ($this->_session_started) session_write_close();
237    }
238
239    function setupLocale($prompts=false) {
240
241      parent::setupLocale();
242      if (!$prompts || $this->_localised_prompts) return;
243
244      $this->_setup_localised_plugin_prompts();
245      $this->_localised_prompts = true;
246
247    }
248
249    function _setup_localised_plugin_prompts() {
250      global $conf;
251
252      $langfile   = '/lang/'.$conf['lang'].'/settings.php';
253      $enlangfile = '/lang/en/settings.php';
254
255      if ($dh = opendir(DOKU_PLUGIN)) {
256        while (false !== ($plugin = readdir($dh))) {
257          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
258          if (is_file(DOKU_PLUGIN.$plugin)) continue;
259
260          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
261            $lang = array();
262            @include(DOKU_PLUGIN.$plugin.$enlangfile);
263            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
264            foreach ($lang as $key => $value){
265              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
266            }
267          }
268
269          // fill in the plugin name if missing (should exist for plugins with settings)
270          if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
271            $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
272              ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix');
273          }
274        }
275        closedir($dh);
276      }
277
278      // the same for the active template
279      $tpl = $conf['template'];
280
281      if (@file_exists(DOKU_TPLINC.$enlangfile)){
282        $lang = array();
283        @include(DOKU_TPLINC.$enlangfile);
284        if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
285        foreach ($lang as $key => $value){
286          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
287        }
288      }
289
290      // fill in the template name if missing (should exist for templates with settings)
291      if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
292        $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
293          ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix');
294      }
295
296      return true;
297    }
298
299    /**
300    * Generates a two-level table of contents for the config plugin.
301    * Uses inc/parser/xhtml.php#render_TOC to format the output.
302    * Relies on internal data structures in the Doku_Renderer_xhtml class.
303    *
304    * @author Ben Coburn <btcoburn@silicodon.net>
305    */
306    function _print_config_toc() {
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 list
327      $xhtml_toc = array();
328      $xhtml_toc[] = array('hid' => 'configuration_manager',
329          'title' => $this->getLang('_configuration_manager'),
330          'type'  => 'ul',
331          'level' => 1);
332      $xhtml_toc[] = array('hid' => 'dokuwiki_settings',
333          'title' => $this->getLang('_header_dokuwiki'),
334          'type'  => 'ul',
335          'level' => 1);
336      foreach($toc['conf'] as $setting) {
337        $name = $setting->prompt($this);
338        $xhtml_toc[] = array('hid' => $setting->_key,
339            'title' => $name,
340            'type'  => 'ul',
341            'level' => 2);
342      }
343      if (!empty($toc['plugin'])) {
344        $xhtml_toc[] = array('hid' => 'plugin_settings',
345            'title' => $this->getLang('_header_plugin'),
346            'type'  => 'ul',
347            'level' => 1);
348      }
349      foreach($toc['plugin'] as $setting) {
350        $name = $setting->prompt($this);
351        $xhtml_toc[] = array('hid' => $setting->_key,
352            'title' => $name,
353            'type'  => 'ul',
354            'level' => 2);
355      }
356      if (isset($toc['template'])) {
357        $xhtml_toc[] = array('hid' => 'template_settings',
358            'title' => $this->getLang('_header_template'),
359            'type'  => 'ul',
360            'level' => 1);
361        $setting = $toc['template'];
362        $name = $setting->prompt($this);
363        $xhtml_toc[] = array('hid' => $setting->_key,
364            'title' => $name,
365            'type'  => 'ul',
366            'level' => 2);
367      }
368      if ($has_undefined && $allow_debug) {
369        $xhtml_toc[] = array('hid' => 'undefined_settings',
370            'title' => $this->getLang('_header_undefined'),
371            'type'  => 'ul',
372            'level' => 1);
373      }
374
375      // use the xhtml renderer to make the toc
376      require_once(DOKU_INC.'inc/parser/xhtml.php');
377      $r = new Doku_Renderer_xhtml;
378      $r->toc = $xhtml_toc;
379      print $r->render_TOC();
380    }
381
382    function _print_h1($id, $text) {
383      ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>');
384    }
385
386
387}
388