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 ptln('<form action="'.script().'" method="post">'); 113 formSecurityToken(); 114 $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); 115 116 $undefined_settings = array(); 117 $in_fieldset = false; 118 $first_plugin_fieldset = true; 119 $first_template_fieldset = true; 120 foreach($this->_config->setting as $setting) { 121 if (is_a($setting, 'setting_hidden')) { 122 // skip hidden (and undefined) settings 123 if ($allow_debug && is_a($setting, 'setting_undefined')) { 124 $undefined_settings[] = $setting; 125 } else { 126 continue; 127 } 128 } else if (is_a($setting, 'setting_fieldset')) { 129 // config setting group 130 if ($in_fieldset) { 131 ptln(' </table>'); 132 ptln(' </fieldset>'); 133 } else { 134 $in_fieldset = true; 135 } 136 if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { 137 $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); 138 $first_plugin_fieldset = false; 139 } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { 140 $this->_print_h1('template_settings', $this->getLang('_header_template')); 141 $first_template_fieldset = false; 142 } 143 ptln(' <fieldset id="'.$setting->_key.'">'); 144 ptln(' <legend>'.$setting->prompt($this).'</legend>'); 145 ptln(' <table class="inline">'); 146 } else { 147 // config settings 148 list($label,$input) = $setting->html($this, $this->_error); 149 150 $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : ''); 151 $error = $setting->error() ? ' class="value error"' : ' class="value"'; 152 $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : ''; 153 154 ptln(' <tr'.$class.'>'); 155 ptln(' <td class="label">'); 156 ptln(' <span class="outkey">'.$setting->_out_key(true, true).'</span>'); 157 ptln(' '.$icon.$label); 158 ptln(' </td>'); 159 ptln(' <td'.$error.'>'.$input.'</td>'); 160 ptln(' </tr>'); 161 } 162 } 163 164 ptln(' </table>'); 165 if ($in_fieldset) { 166 ptln(' </fieldset>'); 167 } 168 169 // show undefined settings list 170 if ($allow_debug && !empty($undefined_settings)) { 171 function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); } 172 usort($undefined_settings, '_setting_natural_comparison'); 173 $this->_print_h1('undefined_settings', $this->getLang('_header_undefined')); 174 ptln('<fieldset>'); 175 ptln('<table class="inline">'); 176 $undefined_setting_match = array(); 177 foreach($undefined_settings as $setting) { 178 if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) { 179 $undefined_setting_key = $undefined_setting_match[1]; 180 } else { 181 $undefined_setting_key = $setting->_key; 182 } 183 ptln(' <tr>'); 184 ptln(' <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>'); 185 ptln(' <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>'); 186 ptln(' </tr>'); 187 } 188 ptln('</table>'); 189 ptln('</fieldset>'); 190 } 191 192 // finish up form 193 ptln('<p>'); 194 ptln(' <input type="hidden" name="do" value="admin" />'); 195 ptln(' <input type="hidden" name="page" value="config" />'); 196 197 if (!$this->_config->locked) { 198 ptln(' <input type="hidden" name="save" value="1" />'); 199 ptln(' <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />'); 200 ptln(' <input type="reset" class="button" value="'.$lang['btn_reset'].'" />'); 201 } 202 203 ptln('</p>'); 204 205 ptln('</form>'); 206 ptln('</div>'); 207 } 208 209 /** 210 * @return boolean true - proceed with handle, false - don't proceed 211 */ 212 function _restore_session() { 213 214 // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set, 215 // however they can't be changed without starting the session again 216 if (!headers_sent()) { 217 session_start(); 218 $this->_session_started = true; 219 } 220 221 if (!isset($_SESSION['PLUGIN_CONFIG'])) return true; 222 223 $session = $_SESSION['PLUGIN_CONFIG']; 224 unset($_SESSION['PLUGIN_CONFIG']); 225 226 // still valid? 227 if (time() - $session['time'] > 120) return true; 228 229 switch ($session['state']) { 230 case 'updated' : 231 $this->_changed = true; 232 return false; 233 } 234 235 return true; 236 } 237 238 function _close_session() { 239 if ($this->_session_started) session_write_close(); 240 } 241 242 function setupLocale($prompts=false) { 243 244 parent::setupLocale(); 245 if (!$prompts || $this->_localised_prompts) return; 246 247 $this->_setup_localised_plugin_prompts(); 248 $this->_localised_prompts = true; 249 250 } 251 252 function _setup_localised_plugin_prompts() { 253 global $conf; 254 255 $langfile = '/lang/'.$conf['lang'].'/settings.php'; 256 $enlangfile = '/lang/en/settings.php'; 257 258 if ($dh = opendir(DOKU_PLUGIN)) { 259 while (false !== ($plugin = readdir($dh))) { 260 if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue; 261 if (is_file(DOKU_PLUGIN.$plugin)) continue; 262 263 if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){ 264 $lang = array(); 265 @include(DOKU_PLUGIN.$plugin.$enlangfile); 266 if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile); 267 foreach ($lang as $key => $value){ 268 $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value; 269 } 270 } 271 272 // fill in the plugin name if missing (should exist for plugins with settings) 273 if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) { 274 $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] = 275 ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix'); 276 } 277 } 278 closedir($dh); 279 } 280 281 // the same for the active template 282 $tpl = $conf['template']; 283 284 if (@file_exists(DOKU_TPLINC.$enlangfile)){ 285 $lang = array(); 286 @include(DOKU_TPLINC.$enlangfile); 287 if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile); 288 foreach ($lang as $key => $value){ 289 $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value; 290 } 291 } 292 293 // fill in the template name if missing (should exist for templates with settings) 294 if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) { 295 $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] = 296 ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix'); 297 } 298 299 return true; 300 } 301 302 /** 303 * Generates a two-level table of contents for the config plugin. 304 * 305 * @author Ben Coburn <btcoburn@silicodon.net> 306 */ 307 function getTOC() { 308 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 309 $this->setupLocale(true); 310 311 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 312 313 // gather toc data 314 $has_undefined = false; 315 $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null); 316 foreach($this->_config->setting as $setting) { 317 if (is_a($setting, 'setting_fieldset')) { 318 if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { 319 $toc['plugin'][] = $setting; 320 } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { 321 $toc['template'] = $setting; 322 } else { 323 $toc['conf'][] = $setting; 324 } 325 } else if (!$has_undefined && is_a($setting, 'setting_undefined')) { 326 $has_undefined = true; 327 } 328 } 329 330 // build toc 331 $t = array(); 332 333 $t[] = html_mktocitem('configuration_manager', $this->getLang('_configuration_manager'), 1); 334 $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1); 335 foreach($toc['conf'] as $setting) { 336 $name = $setting->prompt($this); 337 $t[] = html_mktocitem($setting->_key, $name, 2); 338 } 339 if (!empty($toc['plugin'])) { 340 $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1); 341 } 342 foreach($toc['plugin'] as $setting) { 343 $name = $setting->prompt($this); 344 $t[] = html_mktocitem($setting->_key, $name, 2); 345 } 346 if (isset($toc['template'])) { 347 $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1); 348 $setting = $toc['template']; 349 $name = $setting->prompt($this); 350 $t[] = html_mktocitem($setting->_key, $name, 2); 351 } 352 if ($has_undefined && $allow_debug) { 353 $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 354 } 355 356 return $t; 357 } 358 359 function _print_h1($id, $text) { 360 ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>'); 361 } 362 363 364} 365