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