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 * @author Ben Coburn <btcoburn@silicodon.net> 8 */ 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12define('CM_KEYMARKER','____'); // used for settings with multiple dimensions of array indices 13 14define('PLUGIN_SELF',dirname(__FILE__).'/'); 15define('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php'); 16if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/config/images/'); 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' => '2007-08-05', 44 'name' => 'Configuration Manager', 45 'desc' => "Manage Dokuwiki's Configuration Settings", 46 'url' => 'http://dokuwiki.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 if (!checkSecurityToken()) return $this->_close_session(); 61 62 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 63 64 // don't go any further if the configuration is locked 65 if ($this->_config->_locked) return $this->_close_session(); 66 67 $this->_input = $_REQUEST['config']; 68 69 while (list($key) = each($this->_config->setting)) { 70 $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL; 71 if ($this->_config->setting[$key]->update($input)) { 72 $this->_changed = true; 73 } 74 if ($this->_config->setting[$key]->error()) $this->_error = true; 75 } 76 77 if ($this->_changed && !$this->_error) { 78 $this->_config->save_settings($this->getPluginName()); 79 80 // save state & force a page reload to get the new settings to take effect 81 $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time()); 82 $this->_close_session(); 83 header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&')); 84 exit(); 85 } 86 87 $this->_close_session(); 88 } 89 90 /** 91 * output appropriate html 92 */ 93 function html() { 94 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 95 global $lang; 96 global $ID; 97 98 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 99 $this->setupLocale(true); 100 101 print $this->locale_xhtml('intro'); 102 103 ptln('<div id="config__manager">'); 104 105 if ($this->_config->locked) 106 ptln('<div class="info">'.$this->getLang('locked').'</div>'); 107 elseif ($this->_error) 108 ptln('<div class="error">'.$this->getLang('error').'</div>'); 109 elseif ($this->_changed) 110 ptln('<div class="success">'.$this->getLang('updated').'</div>'); 111 112 // POST to script() instead of wl($ID) so config manager still works if 113 // rewrite config is broken. Add $ID as hidden field to remember 114 // current ID in most cases. 115 ptln('<form action="'.script().'" method="post">'); 116 ptln('<input type="hidden" name="id" value="'.$ID.'" />'); 117 formSecurityToken(); 118 $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); 119 120 $undefined_settings = array(); 121 $in_fieldset = false; 122 $first_plugin_fieldset = true; 123 $first_template_fieldset = true; 124 foreach($this->_config->setting as $setting) { 125 if (is_a($setting, 'setting_hidden')) { 126 // skip hidden (and undefined) settings 127 if ($allow_debug && is_a($setting, 'setting_undefined')) { 128 $undefined_settings[] = $setting; 129 } else { 130 continue; 131 } 132 } else if (is_a($setting, 'setting_fieldset')) { 133 // config setting group 134 if ($in_fieldset) { 135 ptln(' </table>'); 136 ptln(' </fieldset>'); 137 } else { 138 $in_fieldset = true; 139 } 140 if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { 141 $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); 142 $first_plugin_fieldset = false; 143 } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { 144 $this->_print_h1('template_settings', $this->getLang('_header_template')); 145 $first_template_fieldset = false; 146 } 147 ptln(' <fieldset id="'.$setting->_key.'">'); 148 ptln(' <legend>'.$setting->prompt($this).'</legend>'); 149 ptln(' <table class="inline">'); 150 } else { 151 // config settings 152 list($label,$input) = $setting->html($this, $this->_error); 153 154 $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : ''); 155 $error = $setting->error() ? ' class="value error"' : ' class="value"'; 156 $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : ''; 157 158 ptln(' <tr'.$class.'>'); 159 ptln(' <td class="label">'); 160 ptln(' <span class="outkey">'.$setting->_out_key(true, true).'</span>'); 161 ptln(' '.$icon.$label); 162 ptln(' </td>'); 163 ptln(' <td'.$error.'>'.$input.'</td>'); 164 ptln(' </tr>'); 165 } 166 } 167 168 ptln(' </table>'); 169 if ($in_fieldset) { 170 ptln(' </fieldset>'); 171 } 172 173 // show undefined settings list 174 if ($allow_debug && !empty($undefined_settings)) { 175 function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); } 176 usort($undefined_settings, '_setting_natural_comparison'); 177 $this->_print_h1('undefined_settings', $this->getLang('_header_undefined')); 178 ptln('<fieldset>'); 179 ptln('<table class="inline">'); 180 $undefined_setting_match = array(); 181 foreach($undefined_settings as $setting) { 182 if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) { 183 $undefined_setting_key = $undefined_setting_match[1]; 184 } else { 185 $undefined_setting_key = $setting->_key; 186 } 187 ptln(' <tr>'); 188 ptln(' <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>'); 189 ptln(' <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>'); 190 ptln(' </tr>'); 191 } 192 ptln('</table>'); 193 ptln('</fieldset>'); 194 } 195 196 // finish up form 197 ptln('<p>'); 198 ptln(' <input type="hidden" name="do" value="admin" />'); 199 ptln(' <input type="hidden" name="page" value="config" />'); 200 201 if (!$this->_config->locked) { 202 ptln(' <input type="hidden" name="save" value="1" />'); 203 ptln(' <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />'); 204 ptln(' <input type="reset" class="button" value="'.$lang['btn_reset'].'" />'); 205 } 206 207 ptln('</p>'); 208 209 ptln('</form>'); 210 ptln('</div>'); 211 } 212 213 /** 214 * @return boolean true - proceed with handle, false - don't proceed 215 */ 216 function _restore_session() { 217 218 // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set, 219 // however they can't be changed without starting the session again 220 if (!headers_sent()) { 221 session_start(); 222 $this->_session_started = true; 223 } 224 225 if (!isset($_SESSION['PLUGIN_CONFIG'])) return true; 226 227 $session = $_SESSION['PLUGIN_CONFIG']; 228 unset($_SESSION['PLUGIN_CONFIG']); 229 230 // still valid? 231 if (time() - $session['time'] > 120) return true; 232 233 switch ($session['state']) { 234 case 'updated' : 235 $this->_changed = true; 236 return false; 237 } 238 239 return true; 240 } 241 242 function _close_session() { 243 if ($this->_session_started) session_write_close(); 244 } 245 246 function setupLocale($prompts=false) { 247 248 parent::setupLocale(); 249 if (!$prompts || $this->_localised_prompts) return; 250 251 $this->_setup_localised_plugin_prompts(); 252 $this->_localised_prompts = true; 253 254 } 255 256 function _setup_localised_plugin_prompts() { 257 global $conf; 258 259 $langfile = '/lang/'.$conf['lang'].'/settings.php'; 260 $enlangfile = '/lang/en/settings.php'; 261 262 if ($dh = opendir(DOKU_PLUGIN)) { 263 while (false !== ($plugin = readdir($dh))) { 264 if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue; 265 if (is_file(DOKU_PLUGIN.$plugin)) continue; 266 267 if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){ 268 $lang = array(); 269 @include(DOKU_PLUGIN.$plugin.$enlangfile); 270 if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile); 271 foreach ($lang as $key => $value){ 272 $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value; 273 } 274 } 275 276 // fill in the plugin name if missing (should exist for plugins with settings) 277 if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) { 278 $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] = 279 ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix'); 280 } 281 } 282 closedir($dh); 283 } 284 285 // the same for the active template 286 $tpl = $conf['template']; 287 288 if (@file_exists(DOKU_TPLINC.$enlangfile)){ 289 $lang = array(); 290 @include(DOKU_TPLINC.$enlangfile); 291 if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile); 292 foreach ($lang as $key => $value){ 293 $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value; 294 } 295 } 296 297 // fill in the template name if missing (should exist for templates with settings) 298 if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) { 299 $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] = 300 ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix'); 301 } 302 303 return true; 304 } 305 306 /** 307 * Generates a two-level table of contents for the config plugin. 308 * 309 * @author Ben Coburn <btcoburn@silicodon.net> 310 */ 311 function getTOC() { 312 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 313 $this->setupLocale(true); 314 315 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 316 317 // gather toc data 318 $has_undefined = false; 319 $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null); 320 foreach($this->_config->setting as $setting) { 321 if (is_a($setting, 'setting_fieldset')) { 322 if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { 323 $toc['plugin'][] = $setting; 324 } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { 325 $toc['template'] = $setting; 326 } else { 327 $toc['conf'][] = $setting; 328 } 329 } else if (!$has_undefined && is_a($setting, 'setting_undefined')) { 330 $has_undefined = true; 331 } 332 } 333 334 // build toc 335 $t = array(); 336 337 $t[] = html_mktocitem('configuration_manager', $this->getLang('_configuration_manager'), 1); 338 $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1); 339 foreach($toc['conf'] as $setting) { 340 $name = $setting->prompt($this); 341 $t[] = html_mktocitem($setting->_key, $name, 2); 342 } 343 if (!empty($toc['plugin'])) { 344 $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1); 345 } 346 foreach($toc['plugin'] as $setting) { 347 $name = $setting->prompt($this); 348 $t[] = html_mktocitem($setting->_key, $name, 2); 349 } 350 if (isset($toc['template'])) { 351 $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1); 352 $setting = $toc['template']; 353 $name = $setting->prompt($this); 354 $t[] = html_mktocitem($setting->_key, $name, 2); 355 } 356 if ($has_undefined && $allow_debug) { 357 $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 358 } 359 360 return $t; 361 } 362 363 function _print_h1($id, $text) { 364 ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>'); 365 } 366 367 368} 369