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