110449332Schris<?php 210449332Schris/** 310449332Schris * Configuration Manager admin plugin 410449332Schris * 510449332Schris * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 610449332Schris * @author Christopher Smith <chris@jalakai.co.uk> 7685bdd2eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 810449332Schris */ 9e04f1f16Schris// must be run within Dokuwiki 10e04f1f16Schrisif(!defined('DOKU_INC')) die(); 1110449332Schris 1210449332Schrisdefine('CM_KEYMARKER','____'); // used for settings with multiple dimensions of array indices 1310449332Schris 1410449332Schrisdefine('PLUGIN_SELF',dirname(__FILE__).'/'); 1510449332Schrisdefine('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php'); 167ed33b2aSAnika Henkeif(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/config/images/'); 1710449332Schris 1810449332Schrisrequire_once(PLUGIN_SELF.'settings/config.class.php'); // main configuration class and generic settings classes 1910449332Schrisrequire_once(PLUGIN_SELF.'settings/extra.class.php'); // settings classes specific to these settings 2010449332Schris 2110449332Schris/** 2210449332Schris * All DokuWiki plugins to extend the admin function 2310449332Schris * need to inherit from this class 2410449332Schris */ 2510449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin { 2610449332Schris 2710449332Schris var $_file = PLUGIN_METADATA; 2810449332Schris var $_config = null; 2910449332Schris var $_input = null; 3010449332Schris var $_changed = false; // set to true if configuration has altered 3110449332Schris var $_error = false; 3210449332Schris var $_session_started = false; 3389d74a2fSchris var $_localised_prompts = false; 3410449332Schris 35253d4b48SGerrit Uitslag /** 36253d4b48SGerrit Uitslag * @return int 37253d4b48SGerrit Uitslag */ 3810449332Schris function getMenuSort() { return 100; } 3910449332Schris 4010449332Schris /** 4110449332Schris * handle user request 4210449332Schris */ 4310449332Schris function handle() { 44392c9b52SHakan Sandell global $ID, $INPUT; 4510449332Schris 46253d4b48SGerrit Uitslag if(!$this->_restore_session() || $INPUT->int('save') != 1 || !checkSecurityToken()) { 47253d4b48SGerrit Uitslag $this->_close_session(); 48253d4b48SGerrit Uitslag return; 49253d4b48SGerrit Uitslag } 5010449332Schris 51253d4b48SGerrit Uitslag if(is_null($this->_config)) { 52253d4b48SGerrit Uitslag $this->_config = new configuration($this->_file); 53253d4b48SGerrit Uitslag } 5410449332Schris 5510449332Schris // don't go any further if the configuration is locked 56253d4b48SGerrit Uitslag if($this->_config->locked) { 57253d4b48SGerrit Uitslag $this->_close_session(); 58253d4b48SGerrit Uitslag return; 59253d4b48SGerrit Uitslag } 6010449332Schris 61392c9b52SHakan Sandell $this->_input = $INPUT->arr('config'); 6210449332Schris 6310449332Schris while (list($key) = each($this->_config->setting)) { 640ea51e63SMatt Perry $input = isset($this->_input[$key]) ? $this->_input[$key] : null; 6510449332Schris if ($this->_config->setting[$key]->update($input)) { 6610449332Schris $this->_changed = true; 6710449332Schris } 6810449332Schris if ($this->_config->setting[$key]->error()) $this->_error = true; 6910449332Schris } 7010449332Schris 7110449332Schris if ($this->_changed && !$this->_error) { 7210449332Schris $this->_config->save_settings($this->getPluginName()); 7310449332Schris 7410449332Schris // save state & force a page reload to get the new settings to take effect 7510449332Schris $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time()); 7610449332Schris $this->_close_session(); 773295f40aSAndreas Gohr send_redirect(wl($ID,array('do'=>'admin','page'=>'config'),true,'&')); 7810449332Schris exit(); 79e0e514e1SAndreas Gohr } elseif(!$this->_error) { 80e0e514e1SAndreas Gohr $this->_config->touch_settings(); // just touch to refresh cache 8110449332Schris } 8210449332Schris 8310449332Schris $this->_close_session(); 8410449332Schris } 8510449332Schris 8610449332Schris /** 8710449332Schris * output appropriate html 8810449332Schris */ 8910449332Schris function html() { 90685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 9110449332Schris global $lang; 92400497e1Schris global $ID; 9310449332Schris 9410449332Schris if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 9589d74a2fSchris $this->setupLocale(true); 9610449332Schris 9710449332Schris print $this->locale_xhtml('intro'); 9810449332Schris 9924a33b42SAndreas Gohr ptln('<div id="config__manager">'); 10010449332Schris 10110449332Schris if ($this->_config->locked) 102e4a98f5cSAnika Henke ptln('<div class="info">'.$this->getLang('locked').'</div>'); 10310449332Schris elseif ($this->_error) 104e4a98f5cSAnika Henke ptln('<div class="error">'.$this->getLang('error').'</div>'); 10510449332Schris elseif ($this->_changed) 106e4a98f5cSAnika Henke ptln('<div class="success">'.$this->getLang('updated').'</div>'); 10710449332Schris 1085d85efc6SAdrian Lang // POST to script() instead of wl($ID) so config manager still works if 1095d85efc6SAdrian Lang // rewrite config is broken. Add $ID as hidden field to remember 1105d85efc6SAdrian Lang // current ID in most cases. 111f338aa80SAndreas Gohr ptln('<form action="'.script().'" method="post">'); 112f24af591SAnika Henke ptln('<div class="no"><input type="hidden" name="id" value="'.$ID.'" /></div>'); 113634d7150SAndreas Gohr formSecurityToken(); 1144fa2dffcSBen Coburn $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); 11510449332Schris 116253d4b48SGerrit Uitslag /** @var setting[] $undefined_settings */ 117685bdd2eSBen Coburn $undefined_settings = array(); 1184fa2dffcSBen Coburn $in_fieldset = false; 1194fa2dffcSBen Coburn $first_plugin_fieldset = true; 1204fa2dffcSBen Coburn $first_template_fieldset = true; 12110449332Schris foreach($this->_config->setting as $setting) { 122685bdd2eSBen Coburn if (is_a($setting, 'setting_hidden')) { 123685bdd2eSBen Coburn // skip hidden (and undefined) settings 124685bdd2eSBen Coburn if ($allow_debug && is_a($setting, 'setting_undefined')) { 125685bdd2eSBen Coburn $undefined_settings[] = $setting; 126685bdd2eSBen Coburn } else { 127685bdd2eSBen Coburn continue; 128685bdd2eSBen Coburn } 129685bdd2eSBen Coburn } else if (is_a($setting, 'setting_fieldset')) { 1304fa2dffcSBen Coburn // config setting group 1314fa2dffcSBen Coburn if ($in_fieldset) { 1324fa2dffcSBen Coburn ptln(' </table>'); 133e260f93bSAnika Henke ptln(' </div>'); 1344fa2dffcSBen Coburn ptln(' </fieldset>'); 1354fa2dffcSBen Coburn } else { 1364fa2dffcSBen Coburn $in_fieldset = true; 1374fa2dffcSBen Coburn } 1384fa2dffcSBen Coburn if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { 1394fa2dffcSBen Coburn $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); 1404fa2dffcSBen Coburn $first_plugin_fieldset = false; 1414fa2dffcSBen Coburn } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { 1424fa2dffcSBen Coburn $this->_print_h1('template_settings', $this->getLang('_header_template')); 1434fa2dffcSBen Coburn $first_template_fieldset = false; 1444fa2dffcSBen Coburn } 145685bdd2eSBen Coburn ptln(' <fieldset id="'.$setting->_key.'">'); 1464fa2dffcSBen Coburn ptln(' <legend>'.$setting->prompt($this).'</legend>'); 147c7b28ffdSAnika Henke ptln(' <div class="table">'); 1484fa2dffcSBen Coburn ptln(' <table class="inline">'); 1494fa2dffcSBen Coburn } else { 1504fa2dffcSBen Coburn // config settings 15110449332Schris list($label,$input) = $setting->html($this, $this->_error); 15210449332Schris 15310449332Schris $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : ''); 1544fa2dffcSBen Coburn $error = $setting->error() ? ' class="value error"' : ' class="value"'; 1557ed33b2aSAnika Henke $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : ''; 15610449332Schris 15710449332Schris ptln(' <tr'.$class.'>'); 158dde31035SAndreas Gohr ptln(' <td class="label">'); 1597ed33b2aSAnika Henke ptln(' <span class="outkey">'.$setting->_out_key(true, true).'</span>'); 160bccdba06SAndreas Gohr ptln(' '.$icon.$label); 161dde31035SAndreas Gohr ptln(' </td>'); 16210449332Schris ptln(' <td'.$error.'>'.$input.'</td>'); 16310449332Schris ptln(' </tr>'); 16410449332Schris } 1654fa2dffcSBen Coburn } 16610449332Schris 16710449332Schris ptln(' </table>'); 168c7b28ffdSAnika Henke ptln(' </div>'); 1694fa2dffcSBen Coburn if ($in_fieldset) { 1704fa2dffcSBen Coburn ptln(' </fieldset>'); 1714fa2dffcSBen Coburn } 17210449332Schris 173685bdd2eSBen Coburn // show undefined settings list 174685bdd2eSBen Coburn if ($allow_debug && !empty($undefined_settings)) { 175253d4b48SGerrit Uitslag /** 176253d4b48SGerrit Uitslag * Callback for sorting settings 177253d4b48SGerrit Uitslag * 178253d4b48SGerrit Uitslag * @param setting $a 179253d4b48SGerrit Uitslag * @param setting $b 180253d4b48SGerrit Uitslag * @return int if $a is lower/equal/higher than $b 181253d4b48SGerrit Uitslag */ 182253d4b48SGerrit Uitslag function _setting_natural_comparison($a, $b) { 183253d4b48SGerrit Uitslag return strnatcmp($a->_key, $b->_key); 184253d4b48SGerrit Uitslag } 185253d4b48SGerrit Uitslag 186685bdd2eSBen Coburn usort($undefined_settings, '_setting_natural_comparison'); 187685bdd2eSBen Coburn $this->_print_h1('undefined_settings', $this->getLang('_header_undefined')); 188685bdd2eSBen Coburn ptln('<fieldset>'); 189c7b28ffdSAnika Henke ptln('<div class="table">'); 190685bdd2eSBen Coburn ptln('<table class="inline">'); 191685bdd2eSBen Coburn $undefined_setting_match = array(); 192685bdd2eSBen Coburn foreach($undefined_settings as $setting) { 193685bdd2eSBen Coburn if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) { 194685bdd2eSBen Coburn $undefined_setting_key = $undefined_setting_match[1]; 195685bdd2eSBen Coburn } else { 196685bdd2eSBen Coburn $undefined_setting_key = $setting->_key; 197685bdd2eSBen Coburn } 198685bdd2eSBen Coburn ptln(' <tr>'); 199dde31035SAndreas Gohr ptln(' <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>'); 200685bdd2eSBen Coburn ptln(' <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>'); 201685bdd2eSBen Coburn ptln(' </tr>'); 202685bdd2eSBen Coburn } 203685bdd2eSBen Coburn ptln('</table>'); 204c7b28ffdSAnika Henke ptln('</div>'); 205685bdd2eSBen Coburn ptln('</fieldset>'); 206685bdd2eSBen Coburn } 207685bdd2eSBen Coburn 208685bdd2eSBen Coburn // finish up form 20910449332Schris ptln('<p>'); 21010449332Schris ptln(' <input type="hidden" name="do" value="admin" />'); 21110449332Schris ptln(' <input type="hidden" name="page" value="config" />'); 21210449332Schris 21310449332Schris if (!$this->_config->locked) { 21410449332Schris ptln(' <input type="hidden" name="save" value="1" />'); 21591f04971SAndreas Gohr ptln(' <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />'); 216e4a98f5cSAnika Henke ptln(' <input type="reset" class="button" value="'.$lang['btn_reset'].'" />'); 21710449332Schris } 21810449332Schris 21910449332Schris ptln('</p>'); 22010449332Schris 22110449332Schris ptln('</form>'); 22210449332Schris ptln('</div>'); 22310449332Schris } 22410449332Schris 22510449332Schris /** 22610449332Schris * @return boolean true - proceed with handle, false - don't proceed 22710449332Schris */ 22810449332Schris function _restore_session() { 22910449332Schris 23010449332Schris // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set, 23110449332Schris // however they can't be changed without starting the session again 23210449332Schris if (!headers_sent()) { 23310449332Schris session_start(); 23410449332Schris $this->_session_started = true; 23510449332Schris } 23610449332Schris 23710449332Schris if (!isset($_SESSION['PLUGIN_CONFIG'])) return true; 23810449332Schris 23910449332Schris $session = $_SESSION['PLUGIN_CONFIG']; 24010449332Schris unset($_SESSION['PLUGIN_CONFIG']); 24110449332Schris 24210449332Schris // still valid? 24310449332Schris if (time() - $session['time'] > 120) return true; 24410449332Schris 24510449332Schris switch ($session['state']) { 24610449332Schris case 'updated' : 24710449332Schris $this->_changed = true; 24810449332Schris return false; 24910449332Schris } 25010449332Schris 25110449332Schris return true; 25210449332Schris } 25310449332Schris 25410449332Schris function _close_session() { 25510449332Schris if ($this->_session_started) session_write_close(); 25610449332Schris } 25710449332Schris 258253d4b48SGerrit Uitslag /** 259253d4b48SGerrit Uitslag * @param bool $prompts 260253d4b48SGerrit Uitslag */ 26189d74a2fSchris function setupLocale($prompts=false) { 26289d74a2fSchris 26389d74a2fSchris parent::setupLocale(); 26489d74a2fSchris if (!$prompts || $this->_localised_prompts) return; 26589d74a2fSchris 26689d74a2fSchris $this->_setup_localised_plugin_prompts(); 26789d74a2fSchris $this->_localised_prompts = true; 26889d74a2fSchris 26989d74a2fSchris } 27089d74a2fSchris 271253d4b48SGerrit Uitslag /** 272253d4b48SGerrit Uitslag * @return bool 273253d4b48SGerrit Uitslag */ 274a954fae3SBen Coburn function _setup_localised_plugin_prompts() { 27589d74a2fSchris global $conf; 27689d74a2fSchris 277746855cfSBen Coburn $langfile = '/lang/'.$conf['lang'].'/settings.php'; 27889d74a2fSchris $enlangfile = '/lang/en/settings.php'; 27989d74a2fSchris 28089d74a2fSchris if ($dh = opendir(DOKU_PLUGIN)) { 28189d74a2fSchris while (false !== ($plugin = readdir($dh))) { 28289d74a2fSchris if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue; 28389d74a2fSchris if (is_file(DOKU_PLUGIN.$plugin)) continue; 28489d74a2fSchris 285*79e79377SAndreas Gohr if (file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){ 286f1f77134SEsther Brunner $lang = array(); 28789d74a2fSchris @include(DOKU_PLUGIN.$plugin.$enlangfile); 28889d74a2fSchris if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile); 289f1f77134SEsther Brunner foreach ($lang as $key => $value){ 290f1f77134SEsther Brunner $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value; 291f1f77134SEsther Brunner } 29289d74a2fSchris } 2934fa2dffcSBen Coburn 2944fa2dffcSBen Coburn // fill in the plugin name if missing (should exist for plugins with settings) 2954fa2dffcSBen Coburn if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) { 2964fa2dffcSBen Coburn $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] = 297cd3ed83cSMatthias Schulte ucwords(str_replace('_', ' ', $plugin)); 2984fa2dffcSBen Coburn } 29989d74a2fSchris } 30089d74a2fSchris closedir($dh); 30189d74a2fSchris } 30289d74a2fSchris 3034a778400SEsther Brunner // the same for the active template 3044a778400SEsther Brunner $tpl = $conf['template']; 3054a778400SEsther Brunner 306*79e79377SAndreas Gohr if (file_exists(tpl_incdir().$enlangfile)){ 3074a778400SEsther Brunner $lang = array(); 308bc9d46afSAndreas Gohr @include(tpl_incdir().$enlangfile); 309bc9d46afSAndreas Gohr if ($conf['lang'] != 'en') @include(tpl_incdir().$langfile); 3104a778400SEsther Brunner foreach ($lang as $key => $value){ 3114a778400SEsther Brunner $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value; 3124a778400SEsther Brunner } 3134a778400SEsther Brunner } 3144a778400SEsther Brunner 3154fa2dffcSBen Coburn // fill in the template name if missing (should exist for templates with settings) 3164fa2dffcSBen Coburn if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) { 3174fa2dffcSBen Coburn $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] = 318cd3ed83cSMatthias Schulte ucwords(str_replace('_', ' ', $tpl)); 3194fa2dffcSBen Coburn } 3204fa2dffcSBen Coburn 32189d74a2fSchris return true; 32289d74a2fSchris } 32389d74a2fSchris 3244fa2dffcSBen Coburn /** 3254fa2dffcSBen Coburn * Generates a two-level table of contents for the config plugin. 3264fa2dffcSBen Coburn * 3274fa2dffcSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 328253d4b48SGerrit Uitslag * 329253d4b48SGerrit Uitslag * @return array 3304fa2dffcSBen Coburn */ 331b8595a66SAndreas Gohr function getTOC() { 332b8595a66SAndreas Gohr if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 333b8595a66SAndreas Gohr $this->setupLocale(true); 334b8595a66SAndreas Gohr 335685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 336685bdd2eSBen Coburn 3374fa2dffcSBen Coburn // gather toc data 338685bdd2eSBen Coburn $has_undefined = false; 3394fa2dffcSBen Coburn $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null); 3404fa2dffcSBen Coburn foreach($this->_config->setting as $setting) { 3414fa2dffcSBen Coburn if (is_a($setting, 'setting_fieldset')) { 3424fa2dffcSBen Coburn if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { 3434fa2dffcSBen Coburn $toc['plugin'][] = $setting; 3444fa2dffcSBen Coburn } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { 3454fa2dffcSBen Coburn $toc['template'] = $setting; 3464fa2dffcSBen Coburn } else { 3474fa2dffcSBen Coburn $toc['conf'][] = $setting; 3484fa2dffcSBen Coburn } 349685bdd2eSBen Coburn } else if (!$has_undefined && is_a($setting, 'setting_undefined')) { 350685bdd2eSBen Coburn $has_undefined = true; 3514fa2dffcSBen Coburn } 3524fa2dffcSBen Coburn } 3534fa2dffcSBen Coburn 354e1b31a95SBen Coburn // build toc 355b8595a66SAndreas Gohr $t = array(); 356b8595a66SAndreas Gohr 357b8595a66SAndreas Gohr $t[] = html_mktocitem('configuration_manager', $this->getLang('_configuration_manager'), 1); 358b8595a66SAndreas Gohr $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1); 359253d4b48SGerrit Uitslag /** @var setting $setting */ 360e1b31a95SBen Coburn foreach($toc['conf'] as $setting) { 361e1b31a95SBen Coburn $name = $setting->prompt($this); 362b8595a66SAndreas Gohr $t[] = html_mktocitem($setting->_key, $name, 2); 363e1b31a95SBen Coburn } 364e1b31a95SBen Coburn if (!empty($toc['plugin'])) { 365b8595a66SAndreas Gohr $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1); 366e1b31a95SBen Coburn } 367e1b31a95SBen Coburn foreach($toc['plugin'] as $setting) { 368e1b31a95SBen Coburn $name = $setting->prompt($this); 369b8595a66SAndreas Gohr $t[] = html_mktocitem($setting->_key, $name, 2); 370e1b31a95SBen Coburn } 371e1b31a95SBen Coburn if (isset($toc['template'])) { 372b8595a66SAndreas Gohr $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1); 373e1b31a95SBen Coburn $setting = $toc['template']; 374e1b31a95SBen Coburn $name = $setting->prompt($this); 375b8595a66SAndreas Gohr $t[] = html_mktocitem($setting->_key, $name, 2); 376e1b31a95SBen Coburn } 377e1b31a95SBen Coburn if ($has_undefined && $allow_debug) { 378b8595a66SAndreas Gohr $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 379e1b31a95SBen Coburn } 380e1b31a95SBen Coburn 381b8595a66SAndreas Gohr return $t; 3824fa2dffcSBen Coburn } 3834fa2dffcSBen Coburn 384253d4b48SGerrit Uitslag /** 385253d4b48SGerrit Uitslag * @param string $id 386253d4b48SGerrit Uitslag * @param string $text 387253d4b48SGerrit Uitslag */ 3884fa2dffcSBen Coburn function _print_h1($id, $text) { 38963e967bdSAnika Henke ptln('<h1 id="'.$id.'">'.$text.'</h1>'); 3904fa2dffcSBen Coburn } 3914fa2dffcSBen Coburn 3924fa2dffcSBen Coburn 39310449332Schris} 394