xref: /dokuwiki/lib/plugins/config/admin.php (revision b7b9e2f23c056d1778b8aaa689ac65e1c61884b4)
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        var $_localised_prompts = false;
34
35    /**
36     * return some info
37     */
38    function getInfo(){
39
40      return array(
41        'author' => 'Christopher Smith',
42        'email'  => 'chris@jalakai.co.uk',
43        'date'   => '2006-01-24',
44        'name'   => 'Configuration Manager',
45        'desc'   => "Manage Dokuwiki's Configuration Settings",
46        'url'    => 'http://wiki.splitbrain.org/plugin:config',
47      );
48    }
49
50    function getMenuSort() { return 100; }
51
52    /**
53     * handle user request
54     */
55    function handle() {
56      global $ID;
57
58      if (!$this->_restore_session()) return $this->_close_session();
59      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
60
61      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
62
63      // don't go any further if the configuration is locked
64      if ($this->_config->_locked) return $this->_close_session();
65
66      $this->_input = $_REQUEST['config'];
67
68      while (list($key) = each($this->_config->setting)) {
69        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
70        if ($this->_config->setting[$key]->update($input)) {
71          $this->_changed = true;
72        }
73        if ($this->_config->setting[$key]->error()) $this->_error = true;
74      }
75
76      if ($this->_changed  && !$this->_error) {
77        $this->_config->save_settings($this->getPluginName());
78
79        // save state & force a page reload to get the new settings to take effect
80        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
81        $this->_close_session();
82        header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
83        exit();
84      }
85
86      $this->_close_session();
87    }
88
89    /**
90     * output appropriate html
91     */
92    function html() {
93      global $lang;
94      global $ID;
95
96      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
97            $this->setupLocale(true);
98
99      print $this->locale_xhtml('intro');
100
101      ptln('<div id="config__manager">');
102
103      if ($this->_config->locked)
104        ptln('<div class="info">'.$this->getLang('locked').'</div>');
105      elseif ($this->_error)
106        ptln('<div class="error">'.$this->getLang('error').'</div>');
107      elseif ($this->_changed)
108        ptln('<div class="success">'.$this->getLang('updated').'</div>');
109
110      ptln('<form action="'.wl($ID).'" method="post">');
111      ptln('  <table class="inline">');
112
113      foreach($this->_config->setting as $setting) {
114
115        list($label,$input) = $setting->html($this, $this->_error);
116
117        $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
118        $error = $setting->error() ? ' class="error"' : '';
119
120        ptln('    <tr'.$class.'>');
121        ptln('      <td>'.$label.'</td>');
122        ptln('      <td'.$error.'>'.$input.'</td>');
123        ptln('    </tr>');
124      }
125
126      ptln('  </table>');
127
128      ptln('<p>');
129      ptln('  <input type="hidden" name="do"     value="admin" />');
130      ptln('  <input type="hidden" name="page"   value="config" />');
131
132      if (!$this->_config->locked) {
133        ptln('  <input type="hidden" name="save"   value="1" />');
134        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" />');
135        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
136      }
137
138      ptln('</p>');
139
140      ptln('</form>');
141      ptln('</div>');
142    }
143
144    /**
145     * @return boolean   true - proceed with handle, false - don't proceed
146     */
147    function _restore_session() {
148
149      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
150      // however they can't be changed without starting the session again
151      if (!headers_sent()) {
152        session_start();
153        $this->_session_started = true;
154      }
155
156      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
157
158      $session = $_SESSION['PLUGIN_CONFIG'];
159      unset($_SESSION['PLUGIN_CONFIG']);
160
161      // still valid?
162      if (time() - $session['time'] > 120) return true;
163
164      switch ($session['state']) {
165        case 'updated' :
166          $this->_changed = true;
167          return false;
168      }
169
170      return true;
171    }
172
173    function _close_session() {
174      if ($this->_session_started) session_write_close();
175    }
176
177    function setupLocale($prompts=false) {
178
179      parent::setupLocale();
180      if (!$prompts || $this->_localised_prompts) return;
181
182      $this->_setup_localised_plugin_prompts();
183      $this->_localised_prompts = true;
184
185    }
186
187    function _setup_localised_plugin_prompts() {
188      global $conf;
189
190      $langfile   = '/lang/'.$conf[lang].'/settings.php';
191      $enlangfile = '/lang/en/settings.php';
192
193            $lang = array();
194
195      if ($dh = opendir(DOKU_PLUGIN)) {
196        while (false !== ($plugin = readdir($dh))) {
197          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
198          if (is_file(DOKU_PLUGIN.$plugin)) continue;
199
200          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
201            @include(DOKU_PLUGIN.$plugin.$enlangfile);
202            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
203          }
204        }
205        closedir($dh);
206
207        $this->lang = array_merge($lang, $this->lang);
208      }
209
210      return true;
211    }
212
213}
214