1*10449332Schris<?php 2*10449332Schris/** 3*10449332Schris * Configuration Manager admin plugin 4*10449332Schris * 5*10449332Schris * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6*10449332Schris * @author Christopher Smith <chris@jalakai.co.uk> 7*10449332Schris */ 8*10449332Schris 9*10449332Schrisif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 10*10449332Schrisif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 11*10449332Schrisrequire_once(DOKU_PLUGIN.'admin.php'); 12*10449332Schris 13*10449332Schrisdefine('CM_KEYMARKER','____'); // used for settings with multiple dimensions of array indices 14*10449332Schris 15*10449332Schrisdefine('PLUGIN_SELF',dirname(__FILE__).'/'); 16*10449332Schrisdefine('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php'); 17*10449332Schris 18*10449332Schrisrequire_once(PLUGIN_SELF.'settings/config.class.php'); // main configuration class and generic settings classes 19*10449332Schrisrequire_once(PLUGIN_SELF.'settings/extra.class.php'); // settings classes specific to these settings 20*10449332Schris 21*10449332Schris/** 22*10449332Schris * All DokuWiki plugins to extend the admin function 23*10449332Schris * need to inherit from this class 24*10449332Schris */ 25*10449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin { 26*10449332Schris 27*10449332Schris var $_file = PLUGIN_METADATA; 28*10449332Schris var $_config = null; 29*10449332Schris var $_input = null; 30*10449332Schris var $_changed = false; // set to true if configuration has altered 31*10449332Schris var $_error = false; 32*10449332Schris var $_session_started = false; 33*10449332Schris 34*10449332Schris /** 35*10449332Schris * return some info 36*10449332Schris */ 37*10449332Schris function getInfo(){ 38*10449332Schris 39*10449332Schris return array( 40*10449332Schris 'author' => 'Christopher Smith', 41*10449332Schris 'email' => 'chris@jalakai.co.uk', 42*10449332Schris 'date' => '2006-01-24', 43*10449332Schris 'name' => 'Configuration Manager', 44*10449332Schris 'desc' => "Manage Dokuwiki's Configuration Settings", 45*10449332Schris 'url' => 'http://wiki.splitbrain.org/plugin:config', 46*10449332Schris ); 47*10449332Schris } 48*10449332Schris 49*10449332Schris function getMenuSort() { return 100; } 50*10449332Schris 51*10449332Schris /** 52*10449332Schris * handle user request 53*10449332Schris */ 54*10449332Schris function handle() { 55*10449332Schris 56*10449332Schris if (!$this->_restore_session()) return $this->_close_session(); 57*10449332Schris if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session(); 58*10449332Schris 59*10449332Schris if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 60*10449332Schris 61*10449332Schris // don't go any further if the configuration is locked 62*10449332Schris if ($this->_config->_locked) return $this->_close_session(); 63*10449332Schris 64*10449332Schris $this->_input = $_REQUEST['config']; 65*10449332Schris 66*10449332Schris while (list($key) = each($this->_config->setting)) { 67*10449332Schris $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL; 68*10449332Schris if ($this->_config->setting[$key]->update($input)) { 69*10449332Schris $this->_changed = true; 70*10449332Schris } 71*10449332Schris if ($this->_config->setting[$key]->error()) $this->_error = true; 72*10449332Schris } 73*10449332Schris 74*10449332Schris if ($this->_changed && !$this->_error) { 75*10449332Schris $this->_config->save_settings($this->getPluginName()); 76*10449332Schris 77*10449332Schris // save state & force a page reload to get the new settings to take effect 78*10449332Schris $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time()); 79*10449332Schris $this->_close_session(); 80*10449332Schris header("Location: ".wl($ID)."?do=admin&page=config"); 81*10449332Schris exit(); 82*10449332Schris } 83*10449332Schris 84*10449332Schris $this->_close_session(); 85*10449332Schris } 86*10449332Schris 87*10449332Schris /** 88*10449332Schris * output appropriate html 89*10449332Schris */ 90*10449332Schris function html() { 91*10449332Schris global $lang; 92*10449332Schris 93*10449332Schris if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } 94*10449332Schris 95*10449332Schris print $this->locale_xhtml('intro'); 96*10449332Schris 97*10449332Schris ptln('<div id="configmanager">'); 98*10449332Schris 99*10449332Schris if ($this->_config->locked) 100*10449332Schris ptln('<p class="info">'.$this->getLang('locked').'</p>'); 101*10449332Schris elseif ($this->_error) 102*10449332Schris ptln('<p class="error">'.$this->getLang('error').'</p>'); 103*10449332Schris elseif ($this->_changed) 104*10449332Schris ptln('<p class="ok">'.$this->getLang('updated').'</p>'); 105*10449332Schris 106*10449332Schris ptln('<form action="'.wl($id).'" method="post">'); 107*10449332Schris ptln(' <table class="inline">'); 108*10449332Schris 109*10449332Schris foreach($this->_config->setting as $setting) { 110*10449332Schris 111*10449332Schris list($label,$input) = $setting->html($this, $this->_error); 112*10449332Schris 113*10449332Schris $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : ''); 114*10449332Schris $error = $setting->error() ? ' class="error"' : ''; 115*10449332Schris 116*10449332Schris ptln(' <tr'.$class.'>'); 117*10449332Schris ptln(' <td>'.$label.'</td>'); 118*10449332Schris ptln(' <td'.$error.'>'.$input.'</td>'); 119*10449332Schris ptln(' </tr>'); 120*10449332Schris } 121*10449332Schris 122*10449332Schris ptln(' </table>'); 123*10449332Schris 124*10449332Schris ptln('<p>'); 125*10449332Schris ptln(' <input type="hidden" name="do" value="admin" />'); 126*10449332Schris ptln(' <input type="hidden" name="page" value="config" />'); 127*10449332Schris 128*10449332Schris if (!$this->_config->locked) { 129*10449332Schris ptln(' <input type="hidden" name="save" value="1" />'); 130*10449332Schris ptln(' <input type="submit" name="submit" value="'.$lang['btn_save'].'" />'); 131*10449332Schris ptln(' <input type="reset" value="'.$lang['btn_reset'].'" />'); 132*10449332Schris } 133*10449332Schris 134*10449332Schris ptln('</p>'); 135*10449332Schris 136*10449332Schris ptln('</form>'); 137*10449332Schris ptln('</div>'); 138*10449332Schris } 139*10449332Schris 140*10449332Schris /** 141*10449332Schris * @return boolean true - proceed with handle, false - don't proceed 142*10449332Schris */ 143*10449332Schris function _restore_session() { 144*10449332Schris 145*10449332Schris // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set, 146*10449332Schris // however they can't be changed without starting the session again 147*10449332Schris if (!headers_sent()) { 148*10449332Schris session_start(); 149*10449332Schris $this->_session_started = true; 150*10449332Schris } 151*10449332Schris 152*10449332Schris if (!isset($_SESSION['PLUGIN_CONFIG'])) return true; 153*10449332Schris 154*10449332Schris $session = $_SESSION['PLUGIN_CONFIG']; 155*10449332Schris unset($_SESSION['PLUGIN_CONFIG']); 156*10449332Schris 157*10449332Schris // still valid? 158*10449332Schris if (time() - $session['time'] > 120) return true; 159*10449332Schris 160*10449332Schris switch ($session['state']) { 161*10449332Schris case 'updated' : 162*10449332Schris $this->_changed = true; 163*10449332Schris return false; 164*10449332Schris } 165*10449332Schris 166*10449332Schris return true; 167*10449332Schris } 168*10449332Schris 169*10449332Schris function _close_session() { 170*10449332Schris if ($this->_session_started) session_write_close(); 171*10449332Schris } 172*10449332Schris 173*10449332Schris} 174