xref: /dokuwiki/lib/plugins/config/admin.php (revision e04f1f162ee7d5abb22d29834ecf410602759b95)
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 */
8*e04f1f16Schris// must be run within Dokuwiki
9*e04f1f16Schrisif(!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
10010449332Schris      print $this->locale_xhtml('intro');
10110449332Schris
10224a33b42SAndreas Gohr      ptln('<div id="config__manager">');
10310449332Schris
10410449332Schris      if ($this->_config->locked)
105e4a98f5cSAnika Henke        ptln('<div class="info">'.$this->getLang('locked').'</div>');
10610449332Schris      elseif ($this->_error)
107e4a98f5cSAnika Henke        ptln('<div class="error">'.$this->getLang('error').'</div>');
10810449332Schris      elseif ($this->_changed)
109e4a98f5cSAnika Henke        ptln('<div class="success">'.$this->getLang('updated').'</div>');
11010449332Schris
111400497e1Schris      ptln('<form action="'.wl($ID).'" method="post">');
11210449332Schris      ptln('  <table class="inline">');
11310449332Schris
11410449332Schris      foreach($this->_config->setting as $setting) {
11510449332Schris
11610449332Schris        list($label,$input) = $setting->html($this, $this->_error);
11710449332Schris
11810449332Schris        $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
11910449332Schris        $error = $setting->error() ? ' class="error"' : '';
12010449332Schris
12110449332Schris        ptln('    <tr'.$class.'>');
12210449332Schris        ptln('      <td>'.$label.'</td>');
12310449332Schris        ptln('      <td'.$error.'>'.$input.'</td>');
12410449332Schris        ptln('    </tr>');
12510449332Schris      }
12610449332Schris
12710449332Schris      ptln('  </table>');
12810449332Schris
12910449332Schris      ptln('<p>');
13010449332Schris      ptln('  <input type="hidden" name="do"     value="admin" />');
13110449332Schris      ptln('  <input type="hidden" name="page"   value="config" />');
13210449332Schris
13310449332Schris      if (!$this->_config->locked) {
13410449332Schris        ptln('  <input type="hidden" name="save"   value="1" />');
13591f04971SAndreas Gohr        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
136e4a98f5cSAnika Henke        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
13710449332Schris      }
13810449332Schris
13910449332Schris      ptln('</p>');
14010449332Schris
14110449332Schris      ptln('</form>');
14210449332Schris      ptln('</div>');
14310449332Schris    }
14410449332Schris
14510449332Schris    /**
14610449332Schris     * @return boolean   true - proceed with handle, false - don't proceed
14710449332Schris     */
14810449332Schris    function _restore_session() {
14910449332Schris
15010449332Schris      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
15110449332Schris      // however they can't be changed without starting the session again
15210449332Schris      if (!headers_sent()) {
15310449332Schris        session_start();
15410449332Schris        $this->_session_started = true;
15510449332Schris      }
15610449332Schris
15710449332Schris      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
15810449332Schris
15910449332Schris      $session = $_SESSION['PLUGIN_CONFIG'];
16010449332Schris      unset($_SESSION['PLUGIN_CONFIG']);
16110449332Schris
16210449332Schris      // still valid?
16310449332Schris      if (time() - $session['time'] > 120) return true;
16410449332Schris
16510449332Schris      switch ($session['state']) {
16610449332Schris        case 'updated' :
16710449332Schris          $this->_changed = true;
16810449332Schris          return false;
16910449332Schris      }
17010449332Schris
17110449332Schris      return true;
17210449332Schris    }
17310449332Schris
17410449332Schris    function _close_session() {
17510449332Schris      if ($this->_session_started) session_write_close();
17610449332Schris    }
17710449332Schris
17889d74a2fSchris    function setupLocale($prompts=false) {
17989d74a2fSchris
18089d74a2fSchris      parent::setupLocale();
18189d74a2fSchris      if (!$prompts || $this->_localised_prompts) return;
18289d74a2fSchris
18389d74a2fSchris      $this->_setup_localised_plugin_prompts();
18489d74a2fSchris      $this->_localised_prompts = true;
18589d74a2fSchris
18689d74a2fSchris    }
18789d74a2fSchris
188a954fae3SBen Coburn    function _setup_localised_plugin_prompts() {
18989d74a2fSchris      global $conf;
19089d74a2fSchris
19189d74a2fSchris      $langfile   = '/lang/'.$conf[lang].'/settings.php';
19289d74a2fSchris      $enlangfile = '/lang/en/settings.php';
19389d74a2fSchris
19489d74a2fSchris      if ($dh = opendir(DOKU_PLUGIN)) {
19589d74a2fSchris        while (false !== ($plugin = readdir($dh))) {
19689d74a2fSchris          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
19789d74a2fSchris          if (is_file(DOKU_PLUGIN.$plugin)) continue;
19889d74a2fSchris
19989d74a2fSchris          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
200f1f77134SEsther Brunner            $lang = array();
20189d74a2fSchris            @include(DOKU_PLUGIN.$plugin.$enlangfile);
20289d74a2fSchris            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
203f1f77134SEsther Brunner            foreach ($lang as $key => $value){
204f1f77134SEsther Brunner              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
205f1f77134SEsther Brunner            }
20689d74a2fSchris          }
20789d74a2fSchris        }
20889d74a2fSchris        closedir($dh);
20989d74a2fSchris      }
21089d74a2fSchris
2114a778400SEsther Brunner      // the same for the active template
2124a778400SEsther Brunner      $tpl = $conf['template'];
2134a778400SEsther Brunner
2144a778400SEsther Brunner      if (@file_exists(DOKU_TPLINC.$enlangfile)){
2154a778400SEsther Brunner        $lang = array();
2164a778400SEsther Brunner        @include(DOKU_TPLINC.$enlangfile);
2174a778400SEsther Brunner        if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
2184a778400SEsther Brunner        foreach ($lang as $key => $value){
2194a778400SEsther Brunner          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
2204a778400SEsther Brunner        }
2214a778400SEsther Brunner      }
2224a778400SEsther Brunner
22389d74a2fSchris      return true;
22489d74a2fSchris    }
22589d74a2fSchris
22610449332Schris}
227