xref: /dokuwiki/lib/plugins/config/admin.php (revision b174aeae6cff69a7877494b3731c849f09b3a24a)
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 */
810449332Schris
910449332Schrisif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
1010449332Schrisif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
1110449332Schrisrequire_once(DOKU_PLUGIN.'admin.php');
1210449332Schris
1310449332Schrisdefine('CM_KEYMARKER','____');            // used for settings with multiple dimensions of array indices
1410449332Schris
1510449332Schrisdefine('PLUGIN_SELF',dirname(__FILE__).'/');
1610449332Schrisdefine('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
1710449332Schris
1810449332Schrisrequire_once(PLUGIN_SELF.'settings/config.class.php');  // main configuration class and generic settings classes
1910449332Schrisrequire_once(PLUGIN_SELF.'settings/extra.class.php');   // settings classes specific to these settings
2010449332Schris
2110449332Schris/**
2210449332Schris * All DokuWiki plugins to extend the admin function
2310449332Schris * need to inherit from this class
2410449332Schris */
2510449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin {
2610449332Schris
2710449332Schris    var $_file = PLUGIN_METADATA;
2810449332Schris    var $_config = null;
2910449332Schris    var $_input = null;
3010449332Schris    var $_changed = false;          // set to true if configuration has altered
3110449332Schris    var $_error = false;
3210449332Schris    var $_session_started = false;
3389d74a2fSchris        var $_localised_prompts = false;
3410449332Schris
3510449332Schris    /**
3610449332Schris     * return some info
3710449332Schris     */
3810449332Schris    function getInfo(){
3910449332Schris
4010449332Schris      return array(
4110449332Schris        'author' => 'Christopher Smith',
4210449332Schris        'email'  => 'chris@jalakai.co.uk',
4310449332Schris        'date'   => '2006-01-24',
4410449332Schris        'name'   => 'Configuration Manager',
4510449332Schris        'desc'   => "Manage Dokuwiki's Configuration Settings",
4610449332Schris        'url'    => 'http://wiki.splitbrain.org/plugin:config',
4710449332Schris      );
4810449332Schris    }
4910449332Schris
5010449332Schris    function getMenuSort() { return 100; }
5110449332Schris
5210449332Schris    /**
5310449332Schris     * handle user request
5410449332Schris     */
5510449332Schris    function handle() {
56400497e1Schris      global $ID;
5710449332Schris
5810449332Schris      if (!$this->_restore_session()) return $this->_close_session();
5910449332Schris      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
6010449332Schris
6110449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
6210449332Schris
6310449332Schris      // don't go any further if the configuration is locked
6410449332Schris      if ($this->_config->_locked) return $this->_close_session();
6510449332Schris
6610449332Schris      $this->_input = $_REQUEST['config'];
6710449332Schris
6810449332Schris      while (list($key) = each($this->_config->setting)) {
6910449332Schris        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
7010449332Schris        if ($this->_config->setting[$key]->update($input)) {
7110449332Schris          $this->_changed = true;
7210449332Schris        }
7310449332Schris        if ($this->_config->setting[$key]->error()) $this->_error = true;
7410449332Schris      }
7510449332Schris
7610449332Schris      if ($this->_changed  && !$this->_error) {
7710449332Schris        $this->_config->save_settings($this->getPluginName());
7810449332Schris
7910449332Schris        // save state & force a page reload to get the new settings to take effect
8010449332Schris        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
8110449332Schris        $this->_close_session();
82*b174aeaeSchris        header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
8310449332Schris        exit();
8410449332Schris      }
8510449332Schris
8610449332Schris      $this->_close_session();
8710449332Schris    }
8810449332Schris
8910449332Schris    /**
9010449332Schris     * output appropriate html
9110449332Schris     */
9210449332Schris    function html() {
9310449332Schris      global $lang;
94400497e1Schris      global $ID;
9510449332Schris
9610449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
9789d74a2fSchris            $this->setupLocale(true);
9810449332Schris
9910449332Schris      print $this->locale_xhtml('intro');
10010449332Schris
10110449332Schris      ptln('<div id="configmanager">');
10210449332Schris
10310449332Schris      if ($this->_config->locked)
10410449332Schris        ptln('<p class="info">'.$this->getLang('locked').'</p>');
10510449332Schris      elseif ($this->_error)
10610449332Schris        ptln('<p class="error">'.$this->getLang('error').'</p>');
10710449332Schris      elseif ($this->_changed)
10810449332Schris        ptln('<p class="ok">'.$this->getLang('updated').'</p>');
10910449332Schris
110400497e1Schris      ptln('<form action="'.wl($ID).'" method="post">');
11110449332Schris      ptln('  <table class="inline">');
11210449332Schris
11310449332Schris      foreach($this->_config->setting as $setting) {
11410449332Schris
11510449332Schris        list($label,$input) = $setting->html($this, $this->_error);
11610449332Schris
11710449332Schris        $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
11810449332Schris        $error = $setting->error() ? ' class="error"' : '';
11910449332Schris
12010449332Schris        ptln('    <tr'.$class.'>');
12110449332Schris        ptln('      <td>'.$label.'</td>');
12210449332Schris        ptln('      <td'.$error.'>'.$input.'</td>');
12310449332Schris        ptln('    </tr>');
12410449332Schris      }
12510449332Schris
12610449332Schris      ptln('  </table>');
12710449332Schris
12810449332Schris      ptln('<p>');
12910449332Schris      ptln('  <input type="hidden" name="do"     value="admin" />');
13010449332Schris      ptln('  <input type="hidden" name="page"   value="config" />');
13110449332Schris
13210449332Schris      if (!$this->_config->locked) {
13310449332Schris        ptln('  <input type="hidden" name="save"   value="1" />');
13410449332Schris        ptln('  <input type="submit" name="submit" value="'.$lang['btn_save'].'" />');
13510449332Schris        ptln('  <input type="reset" value="'.$lang['btn_reset'].'" />');
13610449332Schris      }
13710449332Schris
13810449332Schris      ptln('</p>');
13910449332Schris
14010449332Schris      ptln('</form>');
14110449332Schris      ptln('</div>');
14210449332Schris    }
14310449332Schris
14410449332Schris    /**
14510449332Schris     * @return boolean   true - proceed with handle, false - don't proceed
14610449332Schris     */
14710449332Schris    function _restore_session() {
14810449332Schris
14910449332Schris      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
15010449332Schris      // however they can't be changed without starting the session again
15110449332Schris      if (!headers_sent()) {
15210449332Schris        session_start();
15310449332Schris        $this->_session_started = true;
15410449332Schris      }
15510449332Schris
15610449332Schris      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
15710449332Schris
15810449332Schris      $session = $_SESSION['PLUGIN_CONFIG'];
15910449332Schris      unset($_SESSION['PLUGIN_CONFIG']);
16010449332Schris
16110449332Schris      // still valid?
16210449332Schris      if (time() - $session['time'] > 120) return true;
16310449332Schris
16410449332Schris      switch ($session['state']) {
16510449332Schris        case 'updated' :
16610449332Schris          $this->_changed = true;
16710449332Schris          return false;
16810449332Schris      }
16910449332Schris
17010449332Schris      return true;
17110449332Schris    }
17210449332Schris
17310449332Schris    function _close_session() {
17410449332Schris      if ($this->_session_started) session_write_close();
17510449332Schris    }
17610449332Schris
17789d74a2fSchris    function setupLocale($prompts=false) {
17889d74a2fSchris
17989d74a2fSchris      parent::setupLocale();
18089d74a2fSchris      if (!$prompts || $this->_localised_prompts) return;
18189d74a2fSchris
18289d74a2fSchris      $this->_setup_localised_plugin_prompts();
18389d74a2fSchris      $this->_localised_prompts = true;
18489d74a2fSchris
18589d74a2fSchris    }
18689d74a2fSchris
18789d74a2fSchris    function _setup_localised_plugin_prompts() {
18889d74a2fSchris      global $conf;
18989d74a2fSchris
19089d74a2fSchris      $langfile   = '/lang/'.$conf[lang].'/settings.php';
19189d74a2fSchris      $enlangfile = '/lang/en/settings.php';
19289d74a2fSchris
19389d74a2fSchris            $lang = array();
19489d74a2fSchris
19589d74a2fSchris      if ($dh = opendir(DOKU_PLUGIN)) {
19689d74a2fSchris        while (false !== ($plugin = readdir($dh))) {
19789d74a2fSchris          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
19889d74a2fSchris          if (is_file(DOKU_PLUGIN.$plugin)) continue;
19989d74a2fSchris
20089d74a2fSchris          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
20189d74a2fSchris            @include(DOKU_PLUGIN.$plugin.$enlangfile);
20289d74a2fSchris            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
20389d74a2fSchris          }
20489d74a2fSchris        }
20589d74a2fSchris        closedir($dh);
20689d74a2fSchris
20789d74a2fSchris        $this->lang = array_merge($lang, $this->lang);
20889d74a2fSchris      }
20989d74a2fSchris
21089d74a2fSchris      return true;
21189d74a2fSchris    }
21289d74a2fSchris
21310449332Schris}
214