xref: /dokuwiki/lib/plugins/config/admin.php (revision 4fa2dffce72d182e25295de4947077cf52ba1f2b)
110449332Schris<?php
210449332Schris/**
310449332Schris * Configuration Manager admin plugin
410449332Schris *
510449332Schris * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
610449332Schris * @author     Christopher Smith <chris@jalakai.co.uk>
710449332Schris */
8e04f1f16Schris// must be run within Dokuwiki
9e04f1f16Schrisif(!defined('DOKU_INC')) die();
1010449332Schris
1110449332Schrisif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
1210449332Schrisrequire_once(DOKU_PLUGIN.'admin.php');
1310449332Schris
1410449332Schrisdefine('CM_KEYMARKER','____');            // used for settings with multiple dimensions of array indices
1510449332Schris
1610449332Schrisdefine('PLUGIN_SELF',dirname(__FILE__).'/');
1710449332Schrisdefine('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
1810449332Schris
1910449332Schrisrequire_once(PLUGIN_SELF.'settings/config.class.php');  // main configuration class and generic settings classes
2010449332Schrisrequire_once(PLUGIN_SELF.'settings/extra.class.php');   // settings classes specific to these settings
2110449332Schris
2210449332Schris/**
2310449332Schris * All DokuWiki plugins to extend the admin function
2410449332Schris * need to inherit from this class
2510449332Schris */
2610449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin {
2710449332Schris
2810449332Schris    var $_file = PLUGIN_METADATA;
2910449332Schris    var $_config = null;
3010449332Schris    var $_input = null;
3110449332Schris    var $_changed = false;          // set to true if configuration has altered
3210449332Schris    var $_error = false;
3310449332Schris    var $_session_started = false;
3489d74a2fSchris        var $_localised_prompts = false;
3510449332Schris
3610449332Schris    /**
3710449332Schris     * return some info
3810449332Schris     */
3910449332Schris    function getInfo(){
4010449332Schris
4110449332Schris      return array(
4210449332Schris        'author' => 'Christopher Smith',
4310449332Schris        'email'  => 'chris@jalakai.co.uk',
4410449332Schris        'date'   => '2006-01-24',
4510449332Schris        'name'   => 'Configuration Manager',
4610449332Schris        'desc'   => "Manage Dokuwiki's Configuration Settings",
4710449332Schris        'url'    => 'http://wiki.splitbrain.org/plugin:config',
4810449332Schris      );
4910449332Schris    }
5010449332Schris
5110449332Schris    function getMenuSort() { return 100; }
5210449332Schris
5310449332Schris    /**
5410449332Schris     * handle user request
5510449332Schris     */
5610449332Schris    function handle() {
57400497e1Schris      global $ID;
5810449332Schris
5910449332Schris      if (!$this->_restore_session()) return $this->_close_session();
6010449332Schris      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
6110449332Schris
6210449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
6310449332Schris
6410449332Schris      // don't go any further if the configuration is locked
6510449332Schris      if ($this->_config->_locked) return $this->_close_session();
6610449332Schris
6710449332Schris      $this->_input = $_REQUEST['config'];
6810449332Schris
6910449332Schris      while (list($key) = each($this->_config->setting)) {
7010449332Schris        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
7110449332Schris        if ($this->_config->setting[$key]->update($input)) {
7210449332Schris          $this->_changed = true;
7310449332Schris        }
7410449332Schris        if ($this->_config->setting[$key]->error()) $this->_error = true;
7510449332Schris      }
7610449332Schris
7710449332Schris      if ($this->_changed  && !$this->_error) {
7810449332Schris        $this->_config->save_settings($this->getPluginName());
7910449332Schris
8010449332Schris        // save state & force a page reload to get the new settings to take effect
8110449332Schris        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
8210449332Schris        $this->_close_session();
83b174aeaeSchris        header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
8410449332Schris        exit();
8510449332Schris      }
8610449332Schris
8710449332Schris      $this->_close_session();
8810449332Schris    }
8910449332Schris
9010449332Schris    /**
9110449332Schris     * output appropriate html
9210449332Schris     */
9310449332Schris    function html() {
9410449332Schris      global $lang;
95400497e1Schris      global $ID;
9610449332Schris
9710449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
9889d74a2fSchris      $this->setupLocale(true);
9910449332Schris
100*4fa2dffcSBen Coburn      $this->_print_config_toc();
10110449332Schris      print $this->locale_xhtml('intro');
10210449332Schris
10324a33b42SAndreas Gohr      ptln('<div id="config__manager">');
10410449332Schris
10510449332Schris      if ($this->_config->locked)
106e4a98f5cSAnika Henke        ptln('<div class="info">'.$this->getLang('locked').'</div>');
10710449332Schris      elseif ($this->_error)
108e4a98f5cSAnika Henke        ptln('<div class="error">'.$this->getLang('error').'</div>');
10910449332Schris      elseif ($this->_changed)
110e4a98f5cSAnika Henke        ptln('<div class="success">'.$this->getLang('updated').'</div>');
11110449332Schris
112400497e1Schris      ptln('<form action="'.wl($ID).'" method="post">');
113*4fa2dffcSBen Coburn      $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
11410449332Schris
115*4fa2dffcSBen Coburn      $in_fieldset = false;
116*4fa2dffcSBen Coburn      $first_plugin_fieldset = true;
117*4fa2dffcSBen Coburn      $first_template_fieldset = true;
11810449332Schris      foreach($this->_config->setting as $setting) {
119*4fa2dffcSBen Coburn        if (is_a($setting, 'setting_fieldset')) {
120*4fa2dffcSBen Coburn          // config setting group
121*4fa2dffcSBen Coburn          if ($in_fieldset) {
122*4fa2dffcSBen Coburn            ptln('  </table>');
123*4fa2dffcSBen Coburn            ptln('  </fieldset>');
124*4fa2dffcSBen Coburn          } else {
125*4fa2dffcSBen Coburn            $in_fieldset = true;
126*4fa2dffcSBen Coburn          }
127*4fa2dffcSBen Coburn          if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
128*4fa2dffcSBen Coburn            $this->_print_h1('plugin_settings', $this->getLang('_header_plugin'));
129*4fa2dffcSBen Coburn            $first_plugin_fieldset = false;
130*4fa2dffcSBen Coburn          } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
131*4fa2dffcSBen Coburn            $this->_print_h1('template_settings', $this->getLang('_header_template'));
132*4fa2dffcSBen Coburn            $first_template_fieldset = false;
133*4fa2dffcSBen Coburn          }
134*4fa2dffcSBen Coburn          ptln('  <fieldset name="'.$setting->_key.'" id="'.$setting->_key.'">');
135*4fa2dffcSBen Coburn          ptln('  <legend>'.$setting->prompt($this).'</legend>');
136*4fa2dffcSBen Coburn          ptln('  <table class="inline">');
137*4fa2dffcSBen Coburn        } else {
138*4fa2dffcSBen Coburn          // config settings
13910449332Schris          list($label,$input) = $setting->html($this, $this->_error);
14010449332Schris
14110449332Schris          $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
142*4fa2dffcSBen Coburn          $error = $setting->error() ? ' class="value error"' : ' class="value"';
14310449332Schris
14410449332Schris          ptln('    <tr'.$class.'>');
145*4fa2dffcSBen Coburn          ptln('      <td><a class="nolink" title="$'.$this->_config->_name.'[\''.$setting->_out_key().'\']">'.$label.'</a></td>');
14610449332Schris          ptln('      <td'.$error.'>'.$input.'</td>');
14710449332Schris          ptln('    </tr>');
14810449332Schris        }
149*4fa2dffcSBen Coburn      }
15010449332Schris
15110449332Schris      ptln('  </table>');
152*4fa2dffcSBen Coburn      if ($in_fieldset) {
153*4fa2dffcSBen Coburn        ptln('  </fieldset>');
154*4fa2dffcSBen Coburn      }
15510449332Schris
15610449332Schris      ptln('<p>');
15710449332Schris      ptln('  <input type="hidden" name="do"     value="admin" />');
15810449332Schris      ptln('  <input type="hidden" name="page"   value="config" />');
15910449332Schris
16010449332Schris      if (!$this->_config->locked) {
16110449332Schris        ptln('  <input type="hidden" name="save"   value="1" />');
16291f04971SAndreas Gohr        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
163e4a98f5cSAnika Henke        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
16410449332Schris      }
16510449332Schris
16610449332Schris      ptln('</p>');
16710449332Schris
16810449332Schris      ptln('</form>');
16910449332Schris      ptln('</div>');
17010449332Schris    }
17110449332Schris
17210449332Schris    /**
17310449332Schris     * @return boolean   true - proceed with handle, false - don't proceed
17410449332Schris     */
17510449332Schris    function _restore_session() {
17610449332Schris
17710449332Schris      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
17810449332Schris      // however they can't be changed without starting the session again
17910449332Schris      if (!headers_sent()) {
18010449332Schris        session_start();
18110449332Schris        $this->_session_started = true;
18210449332Schris      }
18310449332Schris
18410449332Schris      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
18510449332Schris
18610449332Schris      $session = $_SESSION['PLUGIN_CONFIG'];
18710449332Schris      unset($_SESSION['PLUGIN_CONFIG']);
18810449332Schris
18910449332Schris      // still valid?
19010449332Schris      if (time() - $session['time'] > 120) return true;
19110449332Schris
19210449332Schris      switch ($session['state']) {
19310449332Schris        case 'updated' :
19410449332Schris          $this->_changed = true;
19510449332Schris          return false;
19610449332Schris      }
19710449332Schris
19810449332Schris      return true;
19910449332Schris    }
20010449332Schris
20110449332Schris    function _close_session() {
20210449332Schris      if ($this->_session_started) session_write_close();
20310449332Schris    }
20410449332Schris
20589d74a2fSchris    function setupLocale($prompts=false) {
20689d74a2fSchris
20789d74a2fSchris      parent::setupLocale();
20889d74a2fSchris      if (!$prompts || $this->_localised_prompts) return;
20989d74a2fSchris
21089d74a2fSchris      $this->_setup_localised_plugin_prompts();
21189d74a2fSchris      $this->_localised_prompts = true;
21289d74a2fSchris
21389d74a2fSchris    }
21489d74a2fSchris
215a954fae3SBen Coburn    function _setup_localised_plugin_prompts() {
21689d74a2fSchris      global $conf;
21789d74a2fSchris
21889d74a2fSchris      $langfile   = '/lang/'.$conf[lang].'/settings.php';
21989d74a2fSchris      $enlangfile = '/lang/en/settings.php';
22089d74a2fSchris
22189d74a2fSchris      if ($dh = opendir(DOKU_PLUGIN)) {
22289d74a2fSchris        while (false !== ($plugin = readdir($dh))) {
22389d74a2fSchris          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
22489d74a2fSchris          if (is_file(DOKU_PLUGIN.$plugin)) continue;
22589d74a2fSchris
22689d74a2fSchris          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
227f1f77134SEsther Brunner            $lang = array();
22889d74a2fSchris            @include(DOKU_PLUGIN.$plugin.$enlangfile);
22989d74a2fSchris            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
230f1f77134SEsther Brunner            foreach ($lang as $key => $value){
231f1f77134SEsther Brunner              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
232f1f77134SEsther Brunner            }
23389d74a2fSchris          }
234*4fa2dffcSBen Coburn
235*4fa2dffcSBen Coburn          // fill in the plugin name if missing (should exist for plugins with settings)
236*4fa2dffcSBen Coburn          if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
237*4fa2dffcSBen Coburn            $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
238*4fa2dffcSBen Coburn              ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix');
239*4fa2dffcSBen Coburn          }
24089d74a2fSchris        }
24189d74a2fSchris        closedir($dh);
24289d74a2fSchris      }
24389d74a2fSchris
2444a778400SEsther Brunner      // the same for the active template
2454a778400SEsther Brunner      $tpl = $conf['template'];
2464a778400SEsther Brunner
2474a778400SEsther Brunner      if (@file_exists(DOKU_TPLINC.$enlangfile)){
2484a778400SEsther Brunner        $lang = array();
2494a778400SEsther Brunner        @include(DOKU_TPLINC.$enlangfile);
2504a778400SEsther Brunner        if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
2514a778400SEsther Brunner        foreach ($lang as $key => $value){
2524a778400SEsther Brunner          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
2534a778400SEsther Brunner        }
2544a778400SEsther Brunner      }
2554a778400SEsther Brunner
256*4fa2dffcSBen Coburn      // fill in the template name if missing (should exist for templates with settings)
257*4fa2dffcSBen Coburn      if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
258*4fa2dffcSBen Coburn        $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
259*4fa2dffcSBen Coburn          ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix');
260*4fa2dffcSBen Coburn      }
261*4fa2dffcSBen Coburn
26289d74a2fSchris      return true;
26389d74a2fSchris    }
26489d74a2fSchris
265*4fa2dffcSBen Coburn    /**
266*4fa2dffcSBen Coburn    * Generates a two-level table of contents for the config plugin.
267*4fa2dffcSBen Coburn    * Uses inc/parser/xhtml.php#render_TOC to format the output.
268*4fa2dffcSBen Coburn    * Relies on internal data structures in the Doku_Renderer_xhtml class.
269*4fa2dffcSBen Coburn    *
270*4fa2dffcSBen Coburn    * @author Ben Coburn <btcoburn@silicodon.net>
271*4fa2dffcSBen Coburn    */
272*4fa2dffcSBen Coburn    function _print_config_toc() {
273*4fa2dffcSBen Coburn      // gather toc data
274*4fa2dffcSBen Coburn      $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
275*4fa2dffcSBen Coburn      foreach($this->_config->setting as $setting) {
276*4fa2dffcSBen Coburn        if (is_a($setting, 'setting_fieldset')) {
277*4fa2dffcSBen Coburn          if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
278*4fa2dffcSBen Coburn            $toc['plugin'][] = $setting;
279*4fa2dffcSBen Coburn          } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
280*4fa2dffcSBen Coburn            $toc['template'] = $setting;
281*4fa2dffcSBen Coburn          } else {
282*4fa2dffcSBen Coburn            $toc['conf'][] = $setting;
283*4fa2dffcSBen Coburn          }
284*4fa2dffcSBen Coburn        }
285*4fa2dffcSBen Coburn      }
286*4fa2dffcSBen Coburn
287*4fa2dffcSBen Coburn      // build toc list
288*4fa2dffcSBen Coburn      $xhtml_toc = array();
289*4fa2dffcSBen Coburn      $xhtml_toc[] = array('hid' => 'configuration_manager',
290*4fa2dffcSBen Coburn          'title' => $this->getLang('_configuration_manager'),
291*4fa2dffcSBen Coburn          'type'  => 'ul',
292*4fa2dffcSBen Coburn          'level' => 1);
293*4fa2dffcSBen Coburn      $xhtml_toc[] = array('hid' => 'dokuwiki_settings',
294*4fa2dffcSBen Coburn          'title' => $this->getLang('_header_dokuwiki'),
295*4fa2dffcSBen Coburn          'type'  => 'ul',
296*4fa2dffcSBen Coburn          'level' => 1);
297*4fa2dffcSBen Coburn      foreach($toc['conf'] as $setting) {
298*4fa2dffcSBen Coburn        $name = $setting->prompt($this);
299*4fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => $setting->_key,
300*4fa2dffcSBen Coburn            'title' => $name,
301*4fa2dffcSBen Coburn            'type'  => 'ul',
302*4fa2dffcSBen Coburn            'level' => 2);
303*4fa2dffcSBen Coburn      }
304*4fa2dffcSBen Coburn      if (!empty($toc['plugin'])) {
305*4fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => 'plugin_settings',
306*4fa2dffcSBen Coburn            'title' => $this->getLang('_header_plugin'),
307*4fa2dffcSBen Coburn            'type'  => 'ul',
308*4fa2dffcSBen Coburn            'level' => 1);
309*4fa2dffcSBen Coburn      }
310*4fa2dffcSBen Coburn      foreach($toc['plugin'] as $setting) {
311*4fa2dffcSBen Coburn        $name = $setting->prompt($this);
312*4fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => $setting->_key,
313*4fa2dffcSBen Coburn            'title' => $name,
314*4fa2dffcSBen Coburn            'type'  => 'ul',
315*4fa2dffcSBen Coburn            'level' => 2);
316*4fa2dffcSBen Coburn      }
317*4fa2dffcSBen Coburn      if (isset($toc['template'])) {
318*4fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => 'template_settings',
319*4fa2dffcSBen Coburn            'title' => $this->getLang('_header_template'),
320*4fa2dffcSBen Coburn            'type'  => 'ul',
321*4fa2dffcSBen Coburn            'level' => 1);
322*4fa2dffcSBen Coburn        $setting = $toc['template'];
323*4fa2dffcSBen Coburn        $name = $setting->prompt($this);
324*4fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => $setting->_key,
325*4fa2dffcSBen Coburn            'title' => $name,
326*4fa2dffcSBen Coburn            'type'  => 'ul',
327*4fa2dffcSBen Coburn            'level' => 2);
328*4fa2dffcSBen Coburn      }
329*4fa2dffcSBen Coburn
330*4fa2dffcSBen Coburn      // use the xhtml renderer to make the toc
331*4fa2dffcSBen Coburn      require_once(DOKU_INC.'inc/parser/xhtml.php');
332*4fa2dffcSBen Coburn      $r = new Doku_Renderer_xhtml;
333*4fa2dffcSBen Coburn      $r->toc = $xhtml_toc;
334*4fa2dffcSBen Coburn      print $r->render_TOC();
335*4fa2dffcSBen Coburn    }
336*4fa2dffcSBen Coburn
337*4fa2dffcSBen Coburn    function _print_h1($id, $text) {
338*4fa2dffcSBen Coburn      ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>');
339*4fa2dffcSBen Coburn    }
340*4fa2dffcSBen Coburn
341*4fa2dffcSBen Coburn
34210449332Schris}
343