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