xref: /dokuwiki/lib/plugins/config/admin.php (revision e04f1f162ee7d5abb22d29834ecf410602759b95)
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 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'admin.php');
13
14define('CM_KEYMARKER','____');            // used for settings with multiple dimensions of array indices
15
16define('PLUGIN_SELF',dirname(__FILE__).'/');
17define('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
18
19require_once(PLUGIN_SELF.'settings/config.class.php');  // main configuration class and generic settings classes
20require_once(PLUGIN_SELF.'settings/extra.class.php');   // settings classes specific to these settings
21
22/**
23 * All DokuWiki plugins to extend the admin function
24 * need to inherit from this class
25 */
26class admin_plugin_config extends DokuWiki_Admin_Plugin {
27
28    var $_file = PLUGIN_METADATA;
29    var $_config = null;
30    var $_input = null;
31    var $_changed = false;          // set to true if configuration has altered
32    var $_error = false;
33    var $_session_started = false;
34        var $_localised_prompts = false;
35
36    /**
37     * return some info
38     */
39    function getInfo(){
40
41      return array(
42        'author' => 'Christopher Smith',
43        'email'  => 'chris@jalakai.co.uk',
44        'date'   => '2006-01-24',
45        'name'   => 'Configuration Manager',
46        'desc'   => "Manage Dokuwiki's Configuration Settings",
47        'url'    => 'http://wiki.splitbrain.org/plugin:config',
48      );
49    }
50
51    function getMenuSort() { return 100; }
52
53    /**
54     * handle user request
55     */
56    function handle() {
57      global $ID;
58
59      if (!$this->_restore_session()) return $this->_close_session();
60      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
61
62      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
63
64      // don't go any further if the configuration is locked
65      if ($this->_config->_locked) return $this->_close_session();
66
67      $this->_input = $_REQUEST['config'];
68
69      while (list($key) = each($this->_config->setting)) {
70        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
71        if ($this->_config->setting[$key]->update($input)) {
72          $this->_changed = true;
73        }
74        if ($this->_config->setting[$key]->error()) $this->_error = true;
75      }
76
77      if ($this->_changed  && !$this->_error) {
78        $this->_config->save_settings($this->getPluginName());
79
80        // save state & force a page reload to get the new settings to take effect
81        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
82        $this->_close_session();
83        header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
84        exit();
85      }
86
87      $this->_close_session();
88    }
89
90    /**
91     * output appropriate html
92     */
93    function html() {
94      global $lang;
95      global $ID;
96
97      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
98      $this->setupLocale(true);
99
100      print $this->locale_xhtml('intro');
101
102      ptln('<div id="config__manager">');
103
104      if ($this->_config->locked)
105        ptln('<div class="info">'.$this->getLang('locked').'</div>');
106      elseif ($this->_error)
107        ptln('<div class="error">'.$this->getLang('error').'</div>');
108      elseif ($this->_changed)
109        ptln('<div class="success">'.$this->getLang('updated').'</div>');
110
111      ptln('<form action="'.wl($ID).'" method="post">');
112      ptln('  <table class="inline">');
113
114      foreach($this->_config->setting as $setting) {
115
116        list($label,$input) = $setting->html($this, $this->_error);
117
118        $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
119        $error = $setting->error() ? ' class="error"' : '';
120
121        ptln('    <tr'.$class.'>');
122        ptln('      <td>'.$label.'</td>');
123        ptln('      <td'.$error.'>'.$input.'</td>');
124        ptln('    </tr>');
125      }
126
127      ptln('  </table>');
128
129      ptln('<p>');
130      ptln('  <input type="hidden" name="do"     value="admin" />');
131      ptln('  <input type="hidden" name="page"   value="config" />');
132
133      if (!$this->_config->locked) {
134        ptln('  <input type="hidden" name="save"   value="1" />');
135        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
136        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
137      }
138
139      ptln('</p>');
140
141      ptln('</form>');
142      ptln('</div>');
143    }
144
145    /**
146     * @return boolean   true - proceed with handle, false - don't proceed
147     */
148    function _restore_session() {
149
150      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
151      // however they can't be changed without starting the session again
152      if (!headers_sent()) {
153        session_start();
154        $this->_session_started = true;
155      }
156
157      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
158
159      $session = $_SESSION['PLUGIN_CONFIG'];
160      unset($_SESSION['PLUGIN_CONFIG']);
161
162      // still valid?
163      if (time() - $session['time'] > 120) return true;
164
165      switch ($session['state']) {
166        case 'updated' :
167          $this->_changed = true;
168          return false;
169      }
170
171      return true;
172    }
173
174    function _close_session() {
175      if ($this->_session_started) session_write_close();
176    }
177
178    function setupLocale($prompts=false) {
179
180      parent::setupLocale();
181      if (!$prompts || $this->_localised_prompts) return;
182
183      $this->_setup_localised_plugin_prompts();
184      $this->_localised_prompts = true;
185
186    }
187
188    function _setup_localised_plugin_prompts() {
189      global $conf;
190
191      $langfile   = '/lang/'.$conf[lang].'/settings.php';
192      $enlangfile = '/lang/en/settings.php';
193
194      if ($dh = opendir(DOKU_PLUGIN)) {
195        while (false !== ($plugin = readdir($dh))) {
196          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
197          if (is_file(DOKU_PLUGIN.$plugin)) continue;
198
199          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
200            $lang = array();
201            @include(DOKU_PLUGIN.$plugin.$enlangfile);
202            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
203            foreach ($lang as $key => $value){
204              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
205            }
206          }
207        }
208        closedir($dh);
209      }
210
211      // the same for the active template
212      $tpl = $conf['template'];
213
214      if (@file_exists(DOKU_TPLINC.$enlangfile)){
215        $lang = array();
216        @include(DOKU_TPLINC.$enlangfile);
217        if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
218        foreach ($lang as $key => $value){
219          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
220        }
221      }
222
223      return true;
224    }
225
226}
227