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