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> 710449332Schris */ 810449332Schris 910449332Schrisif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 1010449332Schrisif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 1110449332Schrisrequire_once(DOKU_PLUGIN.'admin.php'); 1210449332Schris 1310449332Schrisdefine('CM_KEYMARKER','____'); // used for settings with multiple dimensions of array indices 1410449332Schris 1510449332Schrisdefine('PLUGIN_SELF',dirname(__FILE__).'/'); 1610449332Schrisdefine('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php'); 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; 33*89d74a2fSchris var $_localised_prompts = false; 3410449332Schris 3510449332Schris /** 3610449332Schris * return some info 3710449332Schris */ 3810449332Schris function getInfo(){ 3910449332Schris 4010449332Schris return array( 4110449332Schris 'author' => 'Christopher Smith', 4210449332Schris 'email' => 'chris@jalakai.co.uk', 4310449332Schris 'date' => '2006-01-24', 4410449332Schris 'name' => 'Configuration Manager', 4510449332Schris 'desc' => "Manage Dokuwiki's Configuration Settings", 4610449332Schris 'url' => 'http://wiki.splitbrain.org/plugin:config', 4710449332Schris ); 4810449332Schris } 4910449332Schris 5010449332Schris function getMenuSort() { return 100; } 5110449332Schris 5210449332Schris /** 5310449332Schris * handle user request 5410449332Schris */ 5510449332Schris function handle() { 5610449332Schris 5710449332Schris if (!$this->_restore_session()) return $this->_close_session(); 5810449332Schris if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session(); 5910449332Schris 6010449332Schris if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 6110449332Schris 6210449332Schris // don't go any further if the configuration is locked 6310449332Schris if ($this->_config->_locked) return $this->_close_session(); 6410449332Schris 6510449332Schris $this->_input = $_REQUEST['config']; 6610449332Schris 6710449332Schris while (list($key) = each($this->_config->setting)) { 6810449332Schris $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL; 6910449332Schris if ($this->_config->setting[$key]->update($input)) { 7010449332Schris $this->_changed = true; 7110449332Schris } 7210449332Schris if ($this->_config->setting[$key]->error()) $this->_error = true; 7310449332Schris } 7410449332Schris 7510449332Schris if ($this->_changed && !$this->_error) { 7610449332Schris $this->_config->save_settings($this->getPluginName()); 7710449332Schris 7810449332Schris // save state & force a page reload to get the new settings to take effect 7910449332Schris $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time()); 8010449332Schris $this->_close_session(); 8110449332Schris header("Location: ".wl($ID)."?do=admin&page=config"); 8210449332Schris exit(); 8310449332Schris } 8410449332Schris 8510449332Schris $this->_close_session(); 8610449332Schris } 8710449332Schris 8810449332Schris /** 8910449332Schris * output appropriate html 9010449332Schris */ 9110449332Schris function html() { 9210449332Schris global $lang; 9310449332Schris 9410449332Schris if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 95*89d74a2fSchris $this->setupLocale(true); 9610449332Schris 9710449332Schris print $this->locale_xhtml('intro'); 9810449332Schris 9910449332Schris ptln('<div id="configmanager">'); 10010449332Schris 10110449332Schris if ($this->_config->locked) 10210449332Schris ptln('<p class="info">'.$this->getLang('locked').'</p>'); 10310449332Schris elseif ($this->_error) 10410449332Schris ptln('<p class="error">'.$this->getLang('error').'</p>'); 10510449332Schris elseif ($this->_changed) 10610449332Schris ptln('<p class="ok">'.$this->getLang('updated').'</p>'); 10710449332Schris 10810449332Schris ptln('<form action="'.wl($id).'" method="post">'); 10910449332Schris ptln(' <table class="inline">'); 11010449332Schris 11110449332Schris foreach($this->_config->setting as $setting) { 11210449332Schris 11310449332Schris list($label,$input) = $setting->html($this, $this->_error); 11410449332Schris 11510449332Schris $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : ''); 11610449332Schris $error = $setting->error() ? ' class="error"' : ''; 11710449332Schris 11810449332Schris ptln(' <tr'.$class.'>'); 11910449332Schris ptln(' <td>'.$label.'</td>'); 12010449332Schris ptln(' <td'.$error.'>'.$input.'</td>'); 12110449332Schris ptln(' </tr>'); 12210449332Schris } 12310449332Schris 12410449332Schris ptln(' </table>'); 12510449332Schris 12610449332Schris ptln('<p>'); 12710449332Schris ptln(' <input type="hidden" name="do" value="admin" />'); 12810449332Schris ptln(' <input type="hidden" name="page" value="config" />'); 12910449332Schris 13010449332Schris if (!$this->_config->locked) { 13110449332Schris ptln(' <input type="hidden" name="save" value="1" />'); 13210449332Schris ptln(' <input type="submit" name="submit" value="'.$lang['btn_save'].'" />'); 13310449332Schris ptln(' <input type="reset" value="'.$lang['btn_reset'].'" />'); 13410449332Schris } 13510449332Schris 13610449332Schris ptln('</p>'); 13710449332Schris 13810449332Schris ptln('</form>'); 13910449332Schris ptln('</div>'); 14010449332Schris } 14110449332Schris 14210449332Schris /** 14310449332Schris * @return boolean true - proceed with handle, false - don't proceed 14410449332Schris */ 14510449332Schris function _restore_session() { 14610449332Schris 14710449332Schris // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set, 14810449332Schris // however they can't be changed without starting the session again 14910449332Schris if (!headers_sent()) { 15010449332Schris session_start(); 15110449332Schris $this->_session_started = true; 15210449332Schris } 15310449332Schris 15410449332Schris if (!isset($_SESSION['PLUGIN_CONFIG'])) return true; 15510449332Schris 15610449332Schris $session = $_SESSION['PLUGIN_CONFIG']; 15710449332Schris unset($_SESSION['PLUGIN_CONFIG']); 15810449332Schris 15910449332Schris // still valid? 16010449332Schris if (time() - $session['time'] > 120) return true; 16110449332Schris 16210449332Schris switch ($session['state']) { 16310449332Schris case 'updated' : 16410449332Schris $this->_changed = true; 16510449332Schris return false; 16610449332Schris } 16710449332Schris 16810449332Schris return true; 16910449332Schris } 17010449332Schris 17110449332Schris function _close_session() { 17210449332Schris if ($this->_session_started) session_write_close(); 17310449332Schris } 17410449332Schris 175*89d74a2fSchris function setupLocale($prompts=false) { 176*89d74a2fSchris 177*89d74a2fSchris parent::setupLocale(); 178*89d74a2fSchris if (!$prompts || $this->_localised_prompts) return; 179*89d74a2fSchris 180*89d74a2fSchris $this->_setup_localised_plugin_prompts(); 181*89d74a2fSchris $this->_localised_prompts = true; 182*89d74a2fSchris 183*89d74a2fSchris } 184*89d74a2fSchris 185*89d74a2fSchris function _setup_localised_plugin_prompts() { 186*89d74a2fSchris global $conf; 187*89d74a2fSchris 188*89d74a2fSchris $langfile = '/lang/'.$conf[lang].'/settings.php'; 189*89d74a2fSchris $enlangfile = '/lang/en/settings.php'; 190*89d74a2fSchris 191*89d74a2fSchris $lang = array(); 192*89d74a2fSchris 193*89d74a2fSchris if ($dh = opendir(DOKU_PLUGIN)) { 194*89d74a2fSchris while (false !== ($plugin = readdir($dh))) { 195*89d74a2fSchris if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue; 196*89d74a2fSchris if (is_file(DOKU_PLUGIN.$plugin)) continue; 197*89d74a2fSchris 198*89d74a2fSchris if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){ 199*89d74a2fSchris @include(DOKU_PLUGIN.$plugin.$enlangfile); 200*89d74a2fSchris if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile); 201*89d74a2fSchris } 202*89d74a2fSchris } 203*89d74a2fSchris closedir($dh); 204*89d74a2fSchris 205*89d74a2fSchris $this->lang = array_merge($lang, $this->lang); 206*89d74a2fSchris } 207*89d74a2fSchris 208*89d74a2fSchris return true; 209*89d74a2fSchris } 210*89d74a2fSchris 21110449332Schris} 212