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