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