first commit. dokuwiki.
[h2N7SspZmY.git] / lib / plugins / config / admin.php
blobcbee3440a68da6157af6d30f4a3967813e91ae7e
1 <?php
2 /**
3 * Configuration Manager admin plugin
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Christopher Smith <chris@jalakai.co.uk>
7 * @author Ben Coburn <btcoburn@silicodon.net>
8 */
9 // must be run within Dokuwiki
10 if(!defined('DOKU_INC')) die();
12 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13 require_once(DOKU_PLUGIN.'admin.php');
15 define('CM_KEYMARKER','____'); // used for settings with multiple dimensions of array indices
17 define('PLUGIN_SELF',dirname(__FILE__).'/');
18 define('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
19 if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/config/images/');
21 require_once(PLUGIN_SELF.'settings/config.class.php'); // main configuration class and generic settings classes
22 require_once(PLUGIN_SELF.'settings/extra.class.php'); // settings classes specific to these settings
24 /**
25 * All DokuWiki plugins to extend the admin function
26 * need to inherit from this class
28 class admin_plugin_config extends DokuWiki_Admin_Plugin {
30 var $_file = PLUGIN_METADATA;
31 var $_config = null;
32 var $_input = null;
33 var $_changed = false; // set to true if configuration has altered
34 var $_error = false;
35 var $_session_started = false;
36 var $_localised_prompts = false;
38 /**
39 * return some info
41 function getInfo(){
43 return array(
44 'author' => 'Christopher Smith',
45 'email' => 'chris@jalakai.co.uk',
46 'date' => '2007-08-05',
47 'name' => 'Configuration Manager',
48 'desc' => "Manage Dokuwiki's Configuration Settings",
49 'url' => 'http://dokuwiki.org/plugin:config',
53 function getMenuSort() { return 100; }
55 /**
56 * handle user request
58 function handle() {
59 global $ID;
61 if (!$this->_restore_session()) return $this->_close_session();
62 if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
63 if (!checkSecurityToken()) return $this->_close_session();
65 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
67 // don't go any further if the configuration is locked
68 if ($this->_config->_locked) return $this->_close_session();
70 $this->_input = $_REQUEST['config'];
72 while (list($key) = each($this->_config->setting)) {
73 $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
74 if ($this->_config->setting[$key]->update($input)) {
75 $this->_changed = true;
77 if ($this->_config->setting[$key]->error()) $this->_error = true;
80 if ($this->_changed && !$this->_error) {
81 $this->_config->save_settings($this->getPluginName());
83 // save state & force a page reload to get the new settings to take effect
84 $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
85 $this->_close_session();
86 header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
87 exit();
90 $this->_close_session();
93 /**
94 * output appropriate html
96 function html() {
97 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
98 global $lang;
99 global $ID;
101 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
102 $this->setupLocale(true);
104 print $this->locale_xhtml('intro');
106 ptln('<div id="config__manager">');
108 if ($this->_config->locked)
109 ptln('<div class="info">'.$this->getLang('locked').'</div>');
110 elseif ($this->_error)
111 ptln('<div class="error">'.$this->getLang('error').'</div>');
112 elseif ($this->_changed)
113 ptln('<div class="success">'.$this->getLang('updated').'</div>');
115 ptln('<form action="'.wl($ID).'" method="post">');
116 formSecurityToken();
117 $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
119 $undefined_settings = array();
120 $in_fieldset = false;
121 $first_plugin_fieldset = true;
122 $first_template_fieldset = true;
123 foreach($this->_config->setting as $setting) {
124 if (is_a($setting, 'setting_hidden')) {
125 // skip hidden (and undefined) settings
126 if ($allow_debug && is_a($setting, 'setting_undefined')) {
127 $undefined_settings[] = $setting;
128 } else {
129 continue;
131 } else if (is_a($setting, 'setting_fieldset')) {
132 // config setting group
133 if ($in_fieldset) {
134 ptln(' </table>');
135 ptln(' </fieldset>');
136 } else {
137 $in_fieldset = true;
139 if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
140 $this->_print_h1('plugin_settings', $this->getLang('_header_plugin'));
141 $first_plugin_fieldset = false;
142 } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
143 $this->_print_h1('template_settings', $this->getLang('_header_template'));
144 $first_template_fieldset = false;
146 ptln(' <fieldset id="'.$setting->_key.'">');
147 ptln(' <legend>'.$setting->prompt($this).'</legend>');
148 ptln(' <table class="inline">');
149 } else {
150 // config settings
151 list($label,$input) = $setting->html($this, $this->_error);
153 $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
154 $error = $setting->error() ? ' class="value error"' : ' class="value"';
155 $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : '';
157 ptln(' <tr'.$class.'>');
158 ptln(' <td class="label">');
159 ptln(' <span class="outkey">'.$setting->_out_key(true, true).'</span>');
160 ptln(' '.$icon.$label);
161 ptln(' </td>');
162 ptln(' <td'.$error.'>'.$input.'</td>');
163 ptln(' </tr>');
167 ptln(' </table>');
168 if ($in_fieldset) {
169 ptln(' </fieldset>');
172 // show undefined settings list
173 if ($allow_debug && !empty($undefined_settings)) {
174 function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); }
175 usort($undefined_settings, '_setting_natural_comparison');
176 $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
177 ptln('<fieldset>');
178 ptln('<table class="inline">');
179 $undefined_setting_match = array();
180 foreach($undefined_settings as $setting) {
181 if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) {
182 $undefined_setting_key = $undefined_setting_match[1];
183 } else {
184 $undefined_setting_key = $setting->_key;
186 ptln(' <tr>');
187 ptln(' <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>');
188 ptln(' <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
189 ptln(' </tr>');
191 ptln('</table>');
192 ptln('</fieldset>');
195 // finish up form
196 ptln('<p>');
197 ptln(' <input type="hidden" name="do" value="admin" />');
198 ptln(' <input type="hidden" name="page" value="config" />');
200 if (!$this->_config->locked) {
201 ptln(' <input type="hidden" name="save" value="1" />');
202 ptln(' <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
203 ptln(' <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
206 ptln('</p>');
208 ptln('</form>');
209 ptln('</div>');
213 * @return boolean true - proceed with handle, false - don't proceed
215 function _restore_session() {
217 // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
218 // however they can't be changed without starting the session again
219 if (!headers_sent()) {
220 session_start();
221 $this->_session_started = true;
224 if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
226 $session = $_SESSION['PLUGIN_CONFIG'];
227 unset($_SESSION['PLUGIN_CONFIG']);
229 // still valid?
230 if (time() - $session['time'] > 120) return true;
232 switch ($session['state']) {
233 case 'updated' :
234 $this->_changed = true;
235 return false;
238 return true;
241 function _close_session() {
242 if ($this->_session_started) session_write_close();
245 function setupLocale($prompts=false) {
247 parent::setupLocale();
248 if (!$prompts || $this->_localised_prompts) return;
250 $this->_setup_localised_plugin_prompts();
251 $this->_localised_prompts = true;
255 function _setup_localised_plugin_prompts() {
256 global $conf;
258 $langfile = '/lang/'.$conf['lang'].'/settings.php';
259 $enlangfile = '/lang/en/settings.php';
261 if ($dh = opendir(DOKU_PLUGIN)) {
262 while (false !== ($plugin = readdir($dh))) {
263 if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
264 if (is_file(DOKU_PLUGIN.$plugin)) continue;
266 if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
267 $lang = array();
268 @include(DOKU_PLUGIN.$plugin.$enlangfile);
269 if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
270 foreach ($lang as $key => $value){
271 $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
275 // fill in the plugin name if missing (should exist for plugins with settings)
276 if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
277 $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
278 ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix');
281 closedir($dh);
284 // the same for the active template
285 $tpl = $conf['template'];
287 if (@file_exists(DOKU_TPLINC.$enlangfile)){
288 $lang = array();
289 @include(DOKU_TPLINC.$enlangfile);
290 if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
291 foreach ($lang as $key => $value){
292 $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
296 // fill in the template name if missing (should exist for templates with settings)
297 if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
298 $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
299 ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix');
302 return true;
306 * Generates a two-level table of contents for the config plugin.
308 * @author Ben Coburn <btcoburn@silicodon.net>
310 function getTOC() {
311 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
312 $this->setupLocale(true);
314 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
316 // gather toc data
317 $has_undefined = false;
318 $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
319 foreach($this->_config->setting as $setting) {
320 if (is_a($setting, 'setting_fieldset')) {
321 if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
322 $toc['plugin'][] = $setting;
323 } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
324 $toc['template'] = $setting;
325 } else {
326 $toc['conf'][] = $setting;
328 } else if (!$has_undefined && is_a($setting, 'setting_undefined')) {
329 $has_undefined = true;
333 // build toc
334 $t = array();
336 $t[] = html_mktocitem('configuration_manager', $this->getLang('_configuration_manager'), 1);
337 $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1);
338 foreach($toc['conf'] as $setting) {
339 $name = $setting->prompt($this);
340 $t[] = html_mktocitem($setting->_key, $name, 2);
342 if (!empty($toc['plugin'])) {
343 $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1);
345 foreach($toc['plugin'] as $setting) {
346 $name = $setting->prompt($this);
347 $t[] = html_mktocitem($setting->_key, $name, 2);
349 if (isset($toc['template'])) {
350 $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1);
351 $setting = $toc['template'];
352 $name = $setting->prompt($this);
353 $t[] = html_mktocitem($setting->_key, $name, 2);
355 if ($has_undefined && $allow_debug) {
356 $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
359 return $t;
362 function _print_h1($id, $text) {
363 ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>');