xref: /dokuwiki/lib/plugins/config/admin.php (revision a17ae368c3b68a6ada9f26e2e9a438851d05b275)
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'   => '2007-08-05',
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      print $this->locale_xhtml('intro');
103
104      ptln('<div id="config__manager">');
105
106      if ($this->_config->locked)
107        ptln('<div class="info">'.$this->getLang('locked').'</div>');
108      elseif ($this->_error)
109        ptln('<div class="error">'.$this->getLang('error').'</div>');
110      elseif ($this->_changed)
111        ptln('<div class="success">'.$this->getLang('updated').'</div>');
112
113      ptln('<form action="'.wl($ID).'" method="post">');
114      $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
115
116      $undefined_settings = array();
117      $in_fieldset = false;
118      $first_plugin_fieldset = true;
119      $first_template_fieldset = true;
120      foreach($this->_config->setting as $setting) {
121        if (is_a($setting, 'setting_hidden')) {
122          // skip hidden (and undefined) settings
123          if ($allow_debug && is_a($setting, 'setting_undefined')) {
124            $undefined_settings[] = $setting;
125          } else {
126            continue;
127          }
128        } else if (is_a($setting, 'setting_fieldset')) {
129          // config setting group
130          if ($in_fieldset) {
131            ptln('  </table>');
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('  <table class="inline">');
146        } else {
147          // config settings
148          list($label,$input) = $setting->html($this, $this->_error);
149
150          $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
151          $error = $setting->error() ? ' class="value error"' : ' class="value"';
152
153          ptln('    <tr'.$class.'>');
154          ptln('      <td class="label">');
155          ptln('        <span class="outkey">'.$setting->_out_key(true).'</span>');
156          ptln('        '.$label);
157          ptln('      </td>');
158          ptln('      <td'.$error.'>'.$input.'</td>');
159          ptln('    </tr>');
160        }
161      }
162
163      ptln('  </table>');
164      if ($in_fieldset) {
165        ptln('  </fieldset>');
166      }
167
168      // show undefined settings list
169      if ($allow_debug && !empty($undefined_settings)) {
170        function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); }
171        usort($undefined_settings, '_setting_natural_comparison');
172        $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
173        ptln('<fieldset>');
174        ptln('<table class="inline">');
175        $undefined_setting_match = array();
176        foreach($undefined_settings as $setting) {
177          if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) {
178            $undefined_setting_key = $undefined_setting_match[1];
179          } else {
180            $undefined_setting_key = $setting->_key;
181          }
182          ptln('  <tr>');
183          ptln('    <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>');
184          ptln('    <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
185          ptln('  </tr>');
186        }
187        ptln('</table>');
188        ptln('</fieldset>');
189      }
190
191      // finish up form
192      ptln('<p>');
193      ptln('  <input type="hidden" name="do"     value="admin" />');
194      ptln('  <input type="hidden" name="page"   value="config" />');
195
196      if (!$this->_config->locked) {
197        ptln('  <input type="hidden" name="save"   value="1" />');
198        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
199        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
200      }
201
202      ptln('</p>');
203
204      ptln('</form>');
205      ptln('</div>');
206    }
207
208    /**
209     * @return boolean   true - proceed with handle, false - don't proceed
210     */
211    function _restore_session() {
212
213      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
214      // however they can't be changed without starting the session again
215      if (!headers_sent()) {
216        session_start();
217        $this->_session_started = true;
218      }
219
220      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
221
222      $session = $_SESSION['PLUGIN_CONFIG'];
223      unset($_SESSION['PLUGIN_CONFIG']);
224
225      // still valid?
226      if (time() - $session['time'] > 120) return true;
227
228      switch ($session['state']) {
229        case 'updated' :
230          $this->_changed = true;
231          return false;
232      }
233
234      return true;
235    }
236
237    function _close_session() {
238      if ($this->_session_started) session_write_close();
239    }
240
241    function setupLocale($prompts=false) {
242
243      parent::setupLocale();
244      if (!$prompts || $this->_localised_prompts) return;
245
246      $this->_setup_localised_plugin_prompts();
247      $this->_localised_prompts = true;
248
249    }
250
251    function _setup_localised_plugin_prompts() {
252      global $conf;
253
254      $langfile   = '/lang/'.$conf['lang'].'/settings.php';
255      $enlangfile = '/lang/en/settings.php';
256
257      if ($dh = opendir(DOKU_PLUGIN)) {
258        while (false !== ($plugin = readdir($dh))) {
259          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
260          if (is_file(DOKU_PLUGIN.$plugin)) continue;
261
262          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
263            $lang = array();
264            @include(DOKU_PLUGIN.$plugin.$enlangfile);
265            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
266            foreach ($lang as $key => $value){
267              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
268            }
269          }
270
271          // fill in the plugin name if missing (should exist for plugins with settings)
272          if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
273            $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
274              ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix');
275          }
276        }
277        closedir($dh);
278      }
279
280      // the same for the active template
281      $tpl = $conf['template'];
282
283      if (@file_exists(DOKU_TPLINC.$enlangfile)){
284        $lang = array();
285        @include(DOKU_TPLINC.$enlangfile);
286        if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
287        foreach ($lang as $key => $value){
288          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
289        }
290      }
291
292      // fill in the template name if missing (should exist for templates with settings)
293      if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
294        $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
295          ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix');
296      }
297
298      return true;
299    }
300
301    /**
302     * Generates a two-level table of contents for the config plugin.
303     *
304     * @author Ben Coburn <btcoburn@silicodon.net>
305     */
306    function getTOC() {
307      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
308      $this->setupLocale(true);
309
310      $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
311
312      // gather toc data
313      $has_undefined = false;
314      $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
315      foreach($this->_config->setting as $setting) {
316        if (is_a($setting, 'setting_fieldset')) {
317          if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
318            $toc['plugin'][] = $setting;
319          } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
320            $toc['template'] = $setting;
321          } else {
322            $toc['conf'][] = $setting;
323          }
324        } else if (!$has_undefined && is_a($setting, 'setting_undefined')) {
325          $has_undefined = true;
326        }
327      }
328
329      // build toc
330      $t = array();
331
332      $t[] = html_mktocitem('configuration_manager', $this->getLang('_configuration_manager'), 1);
333      $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1);
334      foreach($toc['conf'] as $setting) {
335        $name = $setting->prompt($this);
336        $t[] = html_mktocitem($setting->_key, $name, 2);
337      }
338      if (!empty($toc['plugin'])) {
339        $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1);
340      }
341      foreach($toc['plugin'] as $setting) {
342        $name = $setting->prompt($this);
343        $t[] = html_mktocitem($setting->_key, $name, 2);
344      }
345      if (isset($toc['template'])) {
346        $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1);
347        $setting = $toc['template'];
348        $name = $setting->prompt($this);
349        $t[] = html_mktocitem($setting->_key, $name, 2);
350      }
351      if ($has_undefined && $allow_debug) {
352        $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
353      }
354
355      return $t;
356    }
357
358    function _print_h1($id, $text) {
359      ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>');
360    }
361
362
363}
364