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'); 19if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/config/images/'); 20 21require_once(PLUGIN_SELF.'settings/config.class.php'); // main configuration class and generic settings classes 22require_once(PLUGIN_SELF.'settings/extra.class.php'); // settings classes specific to these settings 23 24/** 25 * All DokuWiki plugins to extend the admin function 26 * need to inherit from this class 27 */ 28class admin_plugin_config extends DokuWiki_Admin_Plugin { 29 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; 37 38 /** 39 * return some info 40 */ 41 function getInfo(){ 42 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', 50 ); 51 } 52 53 function getMenuSort() { return 100; } 54 55 /** 56 * handle user request 57 */ 58 function handle() { 59 global $ID; 60 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(); 64 65 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 66 67 // don't go any further if the configuration is locked 68 if ($this->_config->_locked) return $this->_close_session(); 69 70 $this->_input = $_REQUEST['config']; 71 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; 76 } 77 if ($this->_config->setting[$key]->error()) $this->_error = true; 78 } 79 80 if ($this->_changed && !$this->_error) { 81 $this->_config->save_settings($this->getPluginName()); 82 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(); 88 } 89 90 $this->_close_session(); 91 } 92 93 /** 94 * output appropriate html 95 */ 96 function html() { 97 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 98 global $lang; 99 global $ID; 100 101 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 102 $this->setupLocale(true); 103 104 print $this->locale_xhtml('intro'); 105 106 ptln('<div id="config__manager">'); 107 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>'); 114 115 ptln('<form action="'.wl($ID).'" method="post">'); 116 formSecurityToken(); 117 $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); 118 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; 130 } 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; 138 } 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; 145 } 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); 152 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()).'" />' : ''; 156 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>'); 164 } 165 } 166 167 ptln(' </table>'); 168 if ($in_fieldset) { 169 ptln(' </fieldset>'); 170 } 171 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; 185 } 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>'); 190 } 191 ptln('</table>'); 192 ptln('</fieldset>'); 193 } 194 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" />'); 199 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'].'" />'); 204 } 205 206 ptln('</p>'); 207 208 ptln('</form>'); 209 ptln('</div>'); 210 } 211 212 /** 213 * @return boolean true - proceed with handle, false - don't proceed 214 */ 215 function _restore_session() { 216 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; 222 } 223 224 if (!isset($_SESSION['PLUGIN_CONFIG'])) return true; 225 226 $session = $_SESSION['PLUGIN_CONFIG']; 227 unset($_SESSION['PLUGIN_CONFIG']); 228 229 // still valid? 230 if (time() - $session['time'] > 120) return true; 231 232 switch ($session['state']) { 233 case 'updated' : 234 $this->_changed = true; 235 return false; 236 } 237 238 return true; 239 } 240 241 function _close_session() { 242 if ($this->_session_started) session_write_close(); 243 } 244 245 function setupLocale($prompts=false) { 246 247 parent::setupLocale(); 248 if (!$prompts || $this->_localised_prompts) return; 249 250 $this->_setup_localised_plugin_prompts(); 251 $this->_localised_prompts = true; 252 253 } 254 255 function _setup_localised_plugin_prompts() { 256 global $conf; 257 258 $langfile = '/lang/'.$conf['lang'].'/settings.php'; 259 $enlangfile = '/lang/en/settings.php'; 260 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; 265 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; 272 } 273 } 274 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'); 279 } 280 } 281 closedir($dh); 282 } 283 284 // the same for the active template 285 $tpl = $conf['template']; 286 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; 293 } 294 } 295 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'); 300 } 301 302 return true; 303 } 304 305 /** 306 * Generates a two-level table of contents for the config plugin. 307 * 308 * @author Ben Coburn <btcoburn@silicodon.net> 309 */ 310 function getTOC() { 311 if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 312 $this->setupLocale(true); 313 314 $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 315 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; 327 } 328 } else if (!$has_undefined && is_a($setting, 'setting_undefined')) { 329 $has_undefined = true; 330 } 331 } 332 333 // build toc 334 $t = array(); 335 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); 341 } 342 if (!empty($toc['plugin'])) { 343 $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1); 344 } 345 foreach($toc['plugin'] as $setting) { 346 $name = $setting->prompt($this); 347 $t[] = html_mktocitem($setting->_key, $name, 2); 348 } 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); 354 } 355 if ($has_undefined && $allow_debug) { 356 $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 357 } 358 359 return $t; 360 } 361 362 function _print_h1($id, $text) { 363 ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>'); 364 } 365 366 367} 368