xref: /dokuwiki/lib/plugins/config/admin.php (revision e260f93b6cea05bc39bbd77b9db5bdc0c2c424bf)
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
3510449332Schris    function getMenuSort() { return 100; }
3610449332Schris
3710449332Schris    /**
3810449332Schris     * handle user request
3910449332Schris     */
4010449332Schris    function handle() {
41400497e1Schris      global $ID;
4210449332Schris
4310449332Schris      if (!$this->_restore_session()) return $this->_close_session();
4410449332Schris      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
45aea87c78SAndreas Gohr      if (!checkSecurityToken()) return $this->_close_session();
4610449332Schris
4710449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
4810449332Schris
4910449332Schris      // don't go any further if the configuration is locked
5010449332Schris      if ($this->_config->_locked) return $this->_close_session();
5110449332Schris
5210449332Schris      $this->_input = $_REQUEST['config'];
5310449332Schris
5410449332Schris      while (list($key) = each($this->_config->setting)) {
5510449332Schris        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
5610449332Schris        if ($this->_config->setting[$key]->update($input)) {
5710449332Schris          $this->_changed = true;
5810449332Schris        }
5910449332Schris        if ($this->_config->setting[$key]->error()) $this->_error = true;
6010449332Schris      }
6110449332Schris
6210449332Schris      if ($this->_changed  && !$this->_error) {
6310449332Schris        $this->_config->save_settings($this->getPluginName());
6410449332Schris
6510449332Schris        // save state & force a page reload to get the new settings to take effect
6610449332Schris        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
6710449332Schris        $this->_close_session();
68b174aeaeSchris        header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
6910449332Schris        exit();
7010449332Schris      }
7110449332Schris
7210449332Schris      $this->_close_session();
7310449332Schris    }
7410449332Schris
7510449332Schris    /**
7610449332Schris     * output appropriate html
7710449332Schris     */
7810449332Schris    function html() {
79685bdd2eSBen Coburn      $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
8010449332Schris      global $lang;
81400497e1Schris      global $ID;
8210449332Schris
8310449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
8489d74a2fSchris      $this->setupLocale(true);
8510449332Schris
8610449332Schris      print $this->locale_xhtml('intro');
8710449332Schris
8824a33b42SAndreas Gohr      ptln('<div id="config__manager">');
8910449332Schris
9010449332Schris      if ($this->_config->locked)
91e4a98f5cSAnika Henke        ptln('<div class="info">'.$this->getLang('locked').'</div>');
9210449332Schris      elseif ($this->_error)
93e4a98f5cSAnika Henke        ptln('<div class="error">'.$this->getLang('error').'</div>');
9410449332Schris      elseif ($this->_changed)
95e4a98f5cSAnika Henke        ptln('<div class="success">'.$this->getLang('updated').'</div>');
9610449332Schris
975d85efc6SAdrian Lang      // POST to script() instead of wl($ID) so config manager still works if
985d85efc6SAdrian Lang      // rewrite config is broken. Add $ID as hidden field to remember
995d85efc6SAdrian Lang      // current ID in most cases.
100f338aa80SAndreas Gohr      ptln('<form action="'.script().'" method="post">');
101f24af591SAnika Henke      ptln('<div class="no"><input type="hidden" name="id" value="'.$ID.'" /></div>');
102634d7150SAndreas Gohr      formSecurityToken();
1034fa2dffcSBen Coburn      $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
10410449332Schris
105685bdd2eSBen Coburn      $undefined_settings = array();
1064fa2dffcSBen Coburn      $in_fieldset = false;
1074fa2dffcSBen Coburn      $first_plugin_fieldset = true;
1084fa2dffcSBen Coburn      $first_template_fieldset = true;
10910449332Schris      foreach($this->_config->setting as $setting) {
110685bdd2eSBen Coburn        if (is_a($setting, 'setting_hidden')) {
111685bdd2eSBen Coburn          // skip hidden (and undefined) settings
112685bdd2eSBen Coburn          if ($allow_debug && is_a($setting, 'setting_undefined')) {
113685bdd2eSBen Coburn            $undefined_settings[] = $setting;
114685bdd2eSBen Coburn          } else {
115685bdd2eSBen Coburn            continue;
116685bdd2eSBen Coburn          }
117685bdd2eSBen Coburn        } else if (is_a($setting, 'setting_fieldset')) {
1184fa2dffcSBen Coburn          // config setting group
1194fa2dffcSBen Coburn          if ($in_fieldset) {
1204fa2dffcSBen Coburn            ptln('  </table>');
121*e260f93bSAnika Henke            ptln('  </div>');
1224fa2dffcSBen Coburn            ptln('  </fieldset>');
1234fa2dffcSBen Coburn          } else {
1244fa2dffcSBen Coburn            $in_fieldset = true;
1254fa2dffcSBen Coburn          }
1264fa2dffcSBen Coburn          if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
1274fa2dffcSBen Coburn            $this->_print_h1('plugin_settings', $this->getLang('_header_plugin'));
1284fa2dffcSBen Coburn            $first_plugin_fieldset = false;
1294fa2dffcSBen Coburn          } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
1304fa2dffcSBen Coburn            $this->_print_h1('template_settings', $this->getLang('_header_template'));
1314fa2dffcSBen Coburn            $first_template_fieldset = false;
1324fa2dffcSBen Coburn          }
133685bdd2eSBen Coburn          ptln('  <fieldset id="'.$setting->_key.'">');
1344fa2dffcSBen Coburn          ptln('  <legend>'.$setting->prompt($this).'</legend>');
135c7b28ffdSAnika Henke          ptln('  <div class="table">');
1364fa2dffcSBen Coburn          ptln('  <table class="inline">');
1374fa2dffcSBen Coburn        } else {
1384fa2dffcSBen Coburn          // config settings
13910449332Schris          list($label,$input) = $setting->html($this, $this->_error);
14010449332Schris
14110449332Schris          $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
1424fa2dffcSBen Coburn          $error = $setting->error() ? ' class="value error"' : ' class="value"';
1437ed33b2aSAnika Henke          $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : '';
14410449332Schris
14510449332Schris          ptln('    <tr'.$class.'>');
146dde31035SAndreas Gohr          ptln('      <td class="label">');
1477ed33b2aSAnika Henke          ptln('        <span class="outkey">'.$setting->_out_key(true, true).'</span>');
148bccdba06SAndreas Gohr          ptln('        '.$icon.$label);
149dde31035SAndreas Gohr          ptln('      </td>');
15010449332Schris          ptln('      <td'.$error.'>'.$input.'</td>');
15110449332Schris          ptln('    </tr>');
15210449332Schris        }
1534fa2dffcSBen Coburn      }
15410449332Schris
15510449332Schris      ptln('  </table>');
156c7b28ffdSAnika Henke      ptln('  </div>');
1574fa2dffcSBen Coburn      if ($in_fieldset) {
1584fa2dffcSBen Coburn        ptln('  </fieldset>');
1594fa2dffcSBen Coburn      }
16010449332Schris
161685bdd2eSBen Coburn      // show undefined settings list
162685bdd2eSBen Coburn      if ($allow_debug && !empty($undefined_settings)) {
163685bdd2eSBen Coburn        function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); }
164685bdd2eSBen Coburn        usort($undefined_settings, '_setting_natural_comparison');
165685bdd2eSBen Coburn        $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
166685bdd2eSBen Coburn        ptln('<fieldset>');
167c7b28ffdSAnika Henke        ptln('<div class="table">');
168685bdd2eSBen Coburn        ptln('<table class="inline">');
169685bdd2eSBen Coburn        $undefined_setting_match = array();
170685bdd2eSBen Coburn        foreach($undefined_settings as $setting) {
171685bdd2eSBen Coburn          if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) {
172685bdd2eSBen Coburn            $undefined_setting_key = $undefined_setting_match[1];
173685bdd2eSBen Coburn          } else {
174685bdd2eSBen Coburn            $undefined_setting_key = $setting->_key;
175685bdd2eSBen Coburn          }
176685bdd2eSBen Coburn          ptln('  <tr>');
177dde31035SAndreas Gohr          ptln('    <td class="label"><span title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</span></td>');
178685bdd2eSBen Coburn          ptln('    <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
179685bdd2eSBen Coburn          ptln('  </tr>');
180685bdd2eSBen Coburn        }
181685bdd2eSBen Coburn        ptln('</table>');
182c7b28ffdSAnika Henke        ptln('</div>');
183685bdd2eSBen Coburn        ptln('</fieldset>');
184685bdd2eSBen Coburn      }
185685bdd2eSBen Coburn
186685bdd2eSBen Coburn      // finish up form
18710449332Schris      ptln('<p>');
18810449332Schris      ptln('  <input type="hidden" name="do"     value="admin" />');
18910449332Schris      ptln('  <input type="hidden" name="page"   value="config" />');
19010449332Schris
19110449332Schris      if (!$this->_config->locked) {
19210449332Schris        ptln('  <input type="hidden" name="save"   value="1" />');
19391f04971SAndreas Gohr        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
194e4a98f5cSAnika Henke        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
19510449332Schris      }
19610449332Schris
19710449332Schris      ptln('</p>');
19810449332Schris
19910449332Schris      ptln('</form>');
20010449332Schris      ptln('</div>');
20110449332Schris    }
20210449332Schris
20310449332Schris    /**
20410449332Schris     * @return boolean   true - proceed with handle, false - don't proceed
20510449332Schris     */
20610449332Schris    function _restore_session() {
20710449332Schris
20810449332Schris      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
20910449332Schris      // however they can't be changed without starting the session again
21010449332Schris      if (!headers_sent()) {
21110449332Schris        session_start();
21210449332Schris        $this->_session_started = true;
21310449332Schris      }
21410449332Schris
21510449332Schris      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
21610449332Schris
21710449332Schris      $session = $_SESSION['PLUGIN_CONFIG'];
21810449332Schris      unset($_SESSION['PLUGIN_CONFIG']);
21910449332Schris
22010449332Schris      // still valid?
22110449332Schris      if (time() - $session['time'] > 120) return true;
22210449332Schris
22310449332Schris      switch ($session['state']) {
22410449332Schris        case 'updated' :
22510449332Schris          $this->_changed = true;
22610449332Schris          return false;
22710449332Schris      }
22810449332Schris
22910449332Schris      return true;
23010449332Schris    }
23110449332Schris
23210449332Schris    function _close_session() {
23310449332Schris      if ($this->_session_started) session_write_close();
23410449332Schris    }
23510449332Schris
23689d74a2fSchris    function setupLocale($prompts=false) {
23789d74a2fSchris
23889d74a2fSchris      parent::setupLocale();
23989d74a2fSchris      if (!$prompts || $this->_localised_prompts) return;
24089d74a2fSchris
24189d74a2fSchris      $this->_setup_localised_plugin_prompts();
24289d74a2fSchris      $this->_localised_prompts = true;
24389d74a2fSchris
24489d74a2fSchris    }
24589d74a2fSchris
246a954fae3SBen Coburn    function _setup_localised_plugin_prompts() {
24789d74a2fSchris      global $conf;
24889d74a2fSchris
249746855cfSBen Coburn      $langfile   = '/lang/'.$conf['lang'].'/settings.php';
25089d74a2fSchris      $enlangfile = '/lang/en/settings.php';
25189d74a2fSchris
25289d74a2fSchris      if ($dh = opendir(DOKU_PLUGIN)) {
25389d74a2fSchris        while (false !== ($plugin = readdir($dh))) {
25489d74a2fSchris          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
25589d74a2fSchris          if (is_file(DOKU_PLUGIN.$plugin)) continue;
25689d74a2fSchris
25789d74a2fSchris          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
258f1f77134SEsther Brunner            $lang = array();
25989d74a2fSchris            @include(DOKU_PLUGIN.$plugin.$enlangfile);
26089d74a2fSchris            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
261f1f77134SEsther Brunner            foreach ($lang as $key => $value){
262f1f77134SEsther Brunner              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
263f1f77134SEsther Brunner            }
26489d74a2fSchris          }
2654fa2dffcSBen Coburn
2664fa2dffcSBen Coburn          // fill in the plugin name if missing (should exist for plugins with settings)
2674fa2dffcSBen Coburn          if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
2684fa2dffcSBen Coburn            $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
2694fa2dffcSBen Coburn              ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix');
2704fa2dffcSBen Coburn          }
27189d74a2fSchris        }
27289d74a2fSchris        closedir($dh);
27389d74a2fSchris      }
27489d74a2fSchris
2754a778400SEsther Brunner      // the same for the active template
2764a778400SEsther Brunner      $tpl = $conf['template'];
2774a778400SEsther Brunner
278bc9d46afSAndreas Gohr      if (@file_exists(tpl_incdir().$enlangfile)){
2794a778400SEsther Brunner        $lang = array();
280bc9d46afSAndreas Gohr        @include(tpl_incdir().$enlangfile);
281bc9d46afSAndreas Gohr        if ($conf['lang'] != 'en') @include(tpl_incdir().$langfile);
2824a778400SEsther Brunner        foreach ($lang as $key => $value){
2834a778400SEsther Brunner          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
2844a778400SEsther Brunner        }
2854a778400SEsther Brunner      }
2864a778400SEsther Brunner
2874fa2dffcSBen Coburn      // fill in the template name if missing (should exist for templates with settings)
2884fa2dffcSBen Coburn      if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
2894fa2dffcSBen Coburn        $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
2904fa2dffcSBen Coburn          ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix');
2914fa2dffcSBen Coburn      }
2924fa2dffcSBen Coburn
29389d74a2fSchris      return true;
29489d74a2fSchris    }
29589d74a2fSchris
2964fa2dffcSBen Coburn    /**
2974fa2dffcSBen Coburn     * Generates a two-level table of contents for the config plugin.
2984fa2dffcSBen Coburn     *
2994fa2dffcSBen Coburn     * @author Ben Coburn <btcoburn@silicodon.net>
3004fa2dffcSBen Coburn     */
301b8595a66SAndreas Gohr    function getTOC() {
302b8595a66SAndreas Gohr      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
303b8595a66SAndreas Gohr      $this->setupLocale(true);
304b8595a66SAndreas Gohr
305685bdd2eSBen Coburn      $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
306685bdd2eSBen Coburn
3074fa2dffcSBen Coburn      // gather toc data
308685bdd2eSBen Coburn      $has_undefined = false;
3094fa2dffcSBen Coburn      $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
3104fa2dffcSBen Coburn      foreach($this->_config->setting as $setting) {
3114fa2dffcSBen Coburn        if (is_a($setting, 'setting_fieldset')) {
3124fa2dffcSBen Coburn          if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
3134fa2dffcSBen Coburn            $toc['plugin'][] = $setting;
3144fa2dffcSBen Coburn          } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
3154fa2dffcSBen Coburn            $toc['template'] = $setting;
3164fa2dffcSBen Coburn          } else {
3174fa2dffcSBen Coburn            $toc['conf'][] = $setting;
3184fa2dffcSBen Coburn          }
319685bdd2eSBen Coburn        } else if (!$has_undefined && is_a($setting, 'setting_undefined')) {
320685bdd2eSBen Coburn          $has_undefined = true;
3214fa2dffcSBen Coburn        }
3224fa2dffcSBen Coburn      }
3234fa2dffcSBen Coburn
324e1b31a95SBen Coburn      // build toc
325b8595a66SAndreas Gohr      $t = array();
326b8595a66SAndreas Gohr
327b8595a66SAndreas Gohr      $t[] = html_mktocitem('configuration_manager', $this->getLang('_configuration_manager'), 1);
328b8595a66SAndreas Gohr      $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1);
329e1b31a95SBen Coburn      foreach($toc['conf'] as $setting) {
330e1b31a95SBen Coburn        $name = $setting->prompt($this);
331b8595a66SAndreas Gohr        $t[] = html_mktocitem($setting->_key, $name, 2);
332e1b31a95SBen Coburn      }
333e1b31a95SBen Coburn      if (!empty($toc['plugin'])) {
334b8595a66SAndreas Gohr        $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1);
335e1b31a95SBen Coburn      }
336e1b31a95SBen Coburn      foreach($toc['plugin'] as $setting) {
337e1b31a95SBen Coburn        $name = $setting->prompt($this);
338b8595a66SAndreas Gohr        $t[] = html_mktocitem($setting->_key, $name, 2);
339e1b31a95SBen Coburn      }
340e1b31a95SBen Coburn      if (isset($toc['template'])) {
341b8595a66SAndreas Gohr        $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1);
342e1b31a95SBen Coburn        $setting = $toc['template'];
343e1b31a95SBen Coburn        $name = $setting->prompt($this);
344b8595a66SAndreas Gohr        $t[] = html_mktocitem($setting->_key, $name, 2);
345e1b31a95SBen Coburn      }
346e1b31a95SBen Coburn      if ($has_undefined && $allow_debug) {
347b8595a66SAndreas Gohr        $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
348e1b31a95SBen Coburn      }
349e1b31a95SBen Coburn
350b8595a66SAndreas Gohr      return $t;
3514fa2dffcSBen Coburn    }
3524fa2dffcSBen Coburn
3534fa2dffcSBen Coburn    function _print_h1($id, $text) {
3544fa2dffcSBen Coburn      ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>');
3554fa2dffcSBen Coburn    }
3564fa2dffcSBen Coburn
3574fa2dffcSBen Coburn
35810449332Schris}
359