xref: /dokuwiki/lib/plugins/config/admin.php (revision 1044933224145c95bacf2d3534eaed7d9381fdf4)
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
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'admin.php');
12
13define('CM_KEYMARKER','____');            // used for settings with multiple dimensions of array indices
14
15define('PLUGIN_SELF',dirname(__FILE__).'/');
16define('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
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
34    /**
35     * return some info
36     */
37    function getInfo(){
38
39      return array(
40        'author' => 'Christopher Smith',
41        'email'  => 'chris@jalakai.co.uk',
42        'date'   => '2006-01-24',
43        'name'   => 'Configuration Manager',
44        'desc'   => "Manage Dokuwiki's Configuration Settings",
45        'url'    => 'http://wiki.splitbrain.org/plugin:config',
46      );
47    }
48
49    function getMenuSort() { return 100; }
50
51    /**
52     * handle user request
53     */
54    function handle() {
55
56      if (!$this->_restore_session()) return $this->_close_session();
57      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
58
59      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
60
61      // don't go any further if the configuration is locked
62      if ($this->_config->_locked) return $this->_close_session();
63
64      $this->_input = $_REQUEST['config'];
65
66      while (list($key) = each($this->_config->setting)) {
67        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
68        if ($this->_config->setting[$key]->update($input)) {
69          $this->_changed = true;
70        }
71        if ($this->_config->setting[$key]->error()) $this->_error = true;
72      }
73
74      if ($this->_changed  && !$this->_error) {
75        $this->_config->save_settings($this->getPluginName());
76
77        // save state & force a page reload to get the new settings to take effect
78        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
79        $this->_close_session();
80        header("Location: ".wl($ID)."?do=admin&page=config");
81        exit();
82      }
83
84      $this->_close_session();
85    }
86
87    /**
88     * output appropriate html
89     */
90    function html() {
91      global $lang;
92
93      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
94
95      print $this->locale_xhtml('intro');
96
97      ptln('<div id="configmanager">');
98
99      if ($this->_config->locked)
100        ptln('<p class="info">'.$this->getLang('locked').'</p>');
101      elseif ($this->_error)
102        ptln('<p class="error">'.$this->getLang('error').'</p>');
103      elseif ($this->_changed)
104        ptln('<p class="ok">'.$this->getLang('updated').'</p>');
105
106      ptln('<form action="'.wl($id).'" method="post">');
107      ptln('  <table class="inline">');
108
109      foreach($this->_config->setting as $setting) {
110
111        list($label,$input) = $setting->html($this, $this->_error);
112
113        $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
114        $error = $setting->error() ? ' class="error"' : '';
115
116        ptln('    <tr'.$class.'>');
117        ptln('      <td>'.$label.'</td>');
118        ptln('      <td'.$error.'>'.$input.'</td>');
119        ptln('    </tr>');
120      }
121
122      ptln('  </table>');
123
124      ptln('<p>');
125      ptln('  <input type="hidden" name="do"     value="admin" />');
126      ptln('  <input type="hidden" name="page"   value="config" />');
127
128      if (!$this->_config->locked) {
129        ptln('  <input type="hidden" name="save"   value="1" />');
130        ptln('  <input type="submit" name="submit" value="'.$lang['btn_save'].'" />');
131        ptln('  <input type="reset" value="'.$lang['btn_reset'].'" />');
132      }
133
134      ptln('</p>');
135
136      ptln('</form>');
137      ptln('</div>');
138    }
139
140    /**
141     * @return boolean   true - proceed with handle, false - don't proceed
142     */
143    function _restore_session() {
144
145      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
146      // however they can't be changed without starting the session again
147      if (!headers_sent()) {
148        session_start();
149        $this->_session_started = true;
150      }
151
152      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
153
154      $session = $_SESSION['PLUGIN_CONFIG'];
155      unset($_SESSION['PLUGIN_CONFIG']);
156
157      // still valid?
158      if (time() - $session['time'] > 120) return true;
159
160      switch ($session['state']) {
161        case 'updated' :
162          $this->_changed = true;
163          return false;
164      }
165
166      return true;
167    }
168
169    function _close_session() {
170      if ($this->_session_started) session_write_close();
171    }
172
173}
174