xref: /dokuwiki/lib/plugins/config/admin.php (revision 746855cf48e51565e70ed332fac9025865c52d8d)
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
1210449332Schrisif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
1310449332Schrisrequire_once(DOKU_PLUGIN.'admin.php');
1410449332Schris
1510449332Schrisdefine('CM_KEYMARKER','____');            // used for settings with multiple dimensions of array indices
1610449332Schris
1710449332Schrisdefine('PLUGIN_SELF',dirname(__FILE__).'/');
1810449332Schrisdefine('PLUGIN_METADATA',PLUGIN_SELF.'settings/config.metadata.php');
1910449332Schris
2010449332Schrisrequire_once(PLUGIN_SELF.'settings/config.class.php');  // main configuration class and generic settings classes
2110449332Schrisrequire_once(PLUGIN_SELF.'settings/extra.class.php');   // settings classes specific to these settings
2210449332Schris
2310449332Schris/**
2410449332Schris * All DokuWiki plugins to extend the admin function
2510449332Schris * need to inherit from this class
2610449332Schris */
2710449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin {
2810449332Schris
2910449332Schris    var $_file = PLUGIN_METADATA;
3010449332Schris    var $_config = null;
3110449332Schris    var $_input = null;
3210449332Schris    var $_changed = false;          // set to true if configuration has altered
3310449332Schris    var $_error = false;
3410449332Schris    var $_session_started = false;
3589d74a2fSchris        var $_localised_prompts = false;
3610449332Schris
3710449332Schris    /**
3810449332Schris     * return some info
3910449332Schris     */
4010449332Schris    function getInfo(){
4110449332Schris
4210449332Schris      return array(
4310449332Schris        'author' => 'Christopher Smith',
4410449332Schris        'email'  => 'chris@jalakai.co.uk',
4510449332Schris        'date'   => '2006-01-24',
4610449332Schris        'name'   => 'Configuration Manager',
4710449332Schris        'desc'   => "Manage Dokuwiki's Configuration Settings",
4810449332Schris        'url'    => 'http://wiki.splitbrain.org/plugin:config',
4910449332Schris      );
5010449332Schris    }
5110449332Schris
5210449332Schris    function getMenuSort() { return 100; }
5310449332Schris
5410449332Schris    /**
5510449332Schris     * handle user request
5610449332Schris     */
5710449332Schris    function handle() {
58400497e1Schris      global $ID;
5910449332Schris
6010449332Schris      if (!$this->_restore_session()) return $this->_close_session();
6110449332Schris      if (!isset($_REQUEST['save']) || ($_REQUEST['save'] != 1)) return $this->_close_session();
6210449332Schris
6310449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
6410449332Schris
6510449332Schris      // don't go any further if the configuration is locked
6610449332Schris      if ($this->_config->_locked) return $this->_close_session();
6710449332Schris
6810449332Schris      $this->_input = $_REQUEST['config'];
6910449332Schris
7010449332Schris      while (list($key) = each($this->_config->setting)) {
7110449332Schris        $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
7210449332Schris        if ($this->_config->setting[$key]->update($input)) {
7310449332Schris          $this->_changed = true;
7410449332Schris        }
7510449332Schris        if ($this->_config->setting[$key]->error()) $this->_error = true;
7610449332Schris      }
7710449332Schris
7810449332Schris      if ($this->_changed  && !$this->_error) {
7910449332Schris        $this->_config->save_settings($this->getPluginName());
8010449332Schris
8110449332Schris        // save state & force a page reload to get the new settings to take effect
8210449332Schris        $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time());
8310449332Schris        $this->_close_session();
84b174aeaeSchris        header("Location: ".wl($ID,array('do'=>'admin','page'=>'config'),true,'&'));
8510449332Schris        exit();
8610449332Schris      }
8710449332Schris
8810449332Schris      $this->_close_session();
8910449332Schris    }
9010449332Schris
9110449332Schris    /**
9210449332Schris     * output appropriate html
9310449332Schris     */
9410449332Schris    function html() {
95685bdd2eSBen Coburn      $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
9610449332Schris      global $lang;
97400497e1Schris      global $ID;
9810449332Schris
9910449332Schris      if (is_null($this->_config)) { $this->_config = new configuration($this->_file); }
10089d74a2fSchris      $this->setupLocale(true);
10110449332Schris
1024fa2dffcSBen Coburn      $this->_print_config_toc();
10310449332Schris      print $this->locale_xhtml('intro');
10410449332Schris
10524a33b42SAndreas Gohr      ptln('<div id="config__manager">');
10610449332Schris
10710449332Schris      if ($this->_config->locked)
108e4a98f5cSAnika Henke        ptln('<div class="info">'.$this->getLang('locked').'</div>');
10910449332Schris      elseif ($this->_error)
110e4a98f5cSAnika Henke        ptln('<div class="error">'.$this->getLang('error').'</div>');
11110449332Schris      elseif ($this->_changed)
112e4a98f5cSAnika Henke        ptln('<div class="success">'.$this->getLang('updated').'</div>');
11310449332Schris
114400497e1Schris      ptln('<form action="'.wl($ID).'" method="post">');
1154fa2dffcSBen Coburn      $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
11610449332Schris
117685bdd2eSBen Coburn      $undefined_settings = array();
1184fa2dffcSBen Coburn      $in_fieldset = false;
1194fa2dffcSBen Coburn      $first_plugin_fieldset = true;
1204fa2dffcSBen Coburn      $first_template_fieldset = true;
12110449332Schris      foreach($this->_config->setting as $setting) {
122685bdd2eSBen Coburn        if (is_a($setting, 'setting_hidden')) {
123685bdd2eSBen Coburn          // skip hidden (and undefined) settings
124685bdd2eSBen Coburn          if ($allow_debug && is_a($setting, 'setting_undefined')) {
125685bdd2eSBen Coburn            $undefined_settings[] = $setting;
126685bdd2eSBen Coburn          } else {
127685bdd2eSBen Coburn            continue;
128685bdd2eSBen Coburn          }
129685bdd2eSBen Coburn        } else if (is_a($setting, 'setting_fieldset')) {
1304fa2dffcSBen Coburn          // config setting group
1314fa2dffcSBen Coburn          if ($in_fieldset) {
1324fa2dffcSBen Coburn            ptln('  </table>');
1334fa2dffcSBen Coburn            ptln('  </fieldset>');
1344fa2dffcSBen Coburn          } else {
1354fa2dffcSBen Coburn            $in_fieldset = true;
1364fa2dffcSBen Coburn          }
1374fa2dffcSBen Coburn          if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
1384fa2dffcSBen Coburn            $this->_print_h1('plugin_settings', $this->getLang('_header_plugin'));
1394fa2dffcSBen Coburn            $first_plugin_fieldset = false;
1404fa2dffcSBen Coburn          } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
1414fa2dffcSBen Coburn            $this->_print_h1('template_settings', $this->getLang('_header_template'));
1424fa2dffcSBen Coburn            $first_template_fieldset = false;
1434fa2dffcSBen Coburn          }
144685bdd2eSBen Coburn          ptln('  <fieldset id="'.$setting->_key.'">');
1454fa2dffcSBen Coburn          ptln('  <legend>'.$setting->prompt($this).'</legend>');
1464fa2dffcSBen Coburn          ptln('  <table class="inline">');
1474fa2dffcSBen Coburn        } else {
1484fa2dffcSBen Coburn          // config settings
14910449332Schris          list($label,$input) = $setting->html($this, $this->_error);
15010449332Schris
15110449332Schris          $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
1524fa2dffcSBen Coburn          $error = $setting->error() ? ' class="value error"' : ' class="value"';
15310449332Schris
15410449332Schris          ptln('    <tr'.$class.'>');
1554fa2dffcSBen Coburn          ptln('      <td><a class="nolink" title="$'.$this->_config->_name.'[\''.$setting->_out_key().'\']">'.$label.'</a></td>');
15610449332Schris          ptln('      <td'.$error.'>'.$input.'</td>');
15710449332Schris          ptln('    </tr>');
15810449332Schris        }
1594fa2dffcSBen Coburn      }
16010449332Schris
16110449332Schris      ptln('  </table>');
1624fa2dffcSBen Coburn      if ($in_fieldset) {
1634fa2dffcSBen Coburn        ptln('  </fieldset>');
1644fa2dffcSBen Coburn      }
16510449332Schris
166685bdd2eSBen Coburn      // show undefined settings list
167685bdd2eSBen Coburn      if ($allow_debug && !empty($undefined_settings)) {
168685bdd2eSBen Coburn        function _setting_natural_comparison($a, $b) { return strnatcmp($a->_key, $b->_key); }
169685bdd2eSBen Coburn        usort($undefined_settings, '_setting_natural_comparison');
170685bdd2eSBen Coburn        $this->_print_h1('undefined_settings', $this->getLang('_header_undefined'));
171685bdd2eSBen Coburn        ptln('<fieldset>');
172685bdd2eSBen Coburn        ptln('<table class="inline">');
173685bdd2eSBen Coburn        $undefined_setting_match = array();
174685bdd2eSBen Coburn        foreach($undefined_settings as $setting) {
175685bdd2eSBen Coburn          if (preg_match('/^(?:plugin|tpl)'.CM_KEYMARKER.'.*?'.CM_KEYMARKER.'(.*)$/', $setting->_key, $undefined_setting_match)) {
176685bdd2eSBen Coburn            $undefined_setting_key = $undefined_setting_match[1];
177685bdd2eSBen Coburn          } else {
178685bdd2eSBen Coburn            $undefined_setting_key = $setting->_key;
179685bdd2eSBen Coburn          }
180685bdd2eSBen Coburn          ptln('  <tr>');
181685bdd2eSBen Coburn          ptln('    <td><a class="nolink" title="$meta[\''.$undefined_setting_key.'\']">$'.$this->_config->_name.'[\''.$setting->_out_key().'\']</a></td>');
182685bdd2eSBen Coburn          ptln('    <td>'.$this->getLang('_msg_'.get_class($setting)).'</td>');
183685bdd2eSBen Coburn          ptln('  </tr>');
184685bdd2eSBen Coburn        }
185685bdd2eSBen Coburn        ptln('</table>');
186685bdd2eSBen Coburn        ptln('</fieldset>');
187685bdd2eSBen Coburn      }
188685bdd2eSBen Coburn
189685bdd2eSBen Coburn      // finish up form
19010449332Schris      ptln('<p>');
19110449332Schris      ptln('  <input type="hidden" name="do"     value="admin" />');
19210449332Schris      ptln('  <input type="hidden" name="page"   value="config" />');
19310449332Schris
19410449332Schris      if (!$this->_config->locked) {
19510449332Schris        ptln('  <input type="hidden" name="save"   value="1" />');
19691f04971SAndreas Gohr        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />');
197e4a98f5cSAnika Henke        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />');
19810449332Schris      }
19910449332Schris
20010449332Schris      ptln('</p>');
20110449332Schris
20210449332Schris      ptln('</form>');
20310449332Schris      ptln('</div>');
20410449332Schris    }
20510449332Schris
20610449332Schris    /**
20710449332Schris     * @return boolean   true - proceed with handle, false - don't proceed
20810449332Schris     */
20910449332Schris    function _restore_session() {
21010449332Schris
21110449332Schris      // dokuwiki closes the session before act_dispatch. $_SESSION variables are all set,
21210449332Schris      // however they can't be changed without starting the session again
21310449332Schris      if (!headers_sent()) {
21410449332Schris        session_start();
21510449332Schris        $this->_session_started = true;
21610449332Schris      }
21710449332Schris
21810449332Schris      if (!isset($_SESSION['PLUGIN_CONFIG'])) return true;
21910449332Schris
22010449332Schris      $session = $_SESSION['PLUGIN_CONFIG'];
22110449332Schris      unset($_SESSION['PLUGIN_CONFIG']);
22210449332Schris
22310449332Schris      // still valid?
22410449332Schris      if (time() - $session['time'] > 120) return true;
22510449332Schris
22610449332Schris      switch ($session['state']) {
22710449332Schris        case 'updated' :
22810449332Schris          $this->_changed = true;
22910449332Schris          return false;
23010449332Schris      }
23110449332Schris
23210449332Schris      return true;
23310449332Schris    }
23410449332Schris
23510449332Schris    function _close_session() {
23610449332Schris      if ($this->_session_started) session_write_close();
23710449332Schris    }
23810449332Schris
23989d74a2fSchris    function setupLocale($prompts=false) {
24089d74a2fSchris
24189d74a2fSchris      parent::setupLocale();
24289d74a2fSchris      if (!$prompts || $this->_localised_prompts) return;
24389d74a2fSchris
24489d74a2fSchris      $this->_setup_localised_plugin_prompts();
24589d74a2fSchris      $this->_localised_prompts = true;
24689d74a2fSchris
24789d74a2fSchris    }
24889d74a2fSchris
249a954fae3SBen Coburn    function _setup_localised_plugin_prompts() {
25089d74a2fSchris      global $conf;
25189d74a2fSchris
252*746855cfSBen Coburn      $langfile   = '/lang/'.$conf['lang'].'/settings.php';
25389d74a2fSchris      $enlangfile = '/lang/en/settings.php';
25489d74a2fSchris
25589d74a2fSchris      if ($dh = opendir(DOKU_PLUGIN)) {
25689d74a2fSchris        while (false !== ($plugin = readdir($dh))) {
25789d74a2fSchris          if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
25889d74a2fSchris          if (is_file(DOKU_PLUGIN.$plugin)) continue;
25989d74a2fSchris
26089d74a2fSchris          if (@file_exists(DOKU_PLUGIN.$plugin.$enlangfile)){
261f1f77134SEsther Brunner            $lang = array();
26289d74a2fSchris            @include(DOKU_PLUGIN.$plugin.$enlangfile);
26389d74a2fSchris            if ($conf['lang'] != 'en') @include(DOKU_PLUGIN.$plugin.$langfile);
264f1f77134SEsther Brunner            foreach ($lang as $key => $value){
265f1f77134SEsther Brunner              $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value;
266f1f77134SEsther Brunner            }
26789d74a2fSchris          }
2684fa2dffcSBen Coburn
2694fa2dffcSBen Coburn          // fill in the plugin name if missing (should exist for plugins with settings)
2704fa2dffcSBen Coburn          if (!isset($this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'])) {
2714fa2dffcSBen Coburn            $this->lang['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] =
2724fa2dffcSBen Coburn              ucwords(str_replace('_', ' ', $plugin)).' '.$this->getLang('_plugin_sufix');
2734fa2dffcSBen Coburn          }
27489d74a2fSchris        }
27589d74a2fSchris        closedir($dh);
27689d74a2fSchris      }
27789d74a2fSchris
2784a778400SEsther Brunner      // the same for the active template
2794a778400SEsther Brunner      $tpl = $conf['template'];
2804a778400SEsther Brunner
2814a778400SEsther Brunner      if (@file_exists(DOKU_TPLINC.$enlangfile)){
2824a778400SEsther Brunner        $lang = array();
2834a778400SEsther Brunner        @include(DOKU_TPLINC.$enlangfile);
2844a778400SEsther Brunner        if ($conf['lang'] != 'en') @include(DOKU_TPLINC.$langfile);
2854a778400SEsther Brunner        foreach ($lang as $key => $value){
2864a778400SEsther Brunner          $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value;
2874a778400SEsther Brunner        }
2884a778400SEsther Brunner      }
2894a778400SEsther Brunner
2904fa2dffcSBen Coburn      // fill in the template name if missing (should exist for templates with settings)
2914fa2dffcSBen Coburn      if (!isset($this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'])) {
2924fa2dffcSBen Coburn        $this->lang['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] =
2934fa2dffcSBen Coburn          ucwords(str_replace('_', ' ', $tpl)).' '.$this->getLang('_template_sufix');
2944fa2dffcSBen Coburn      }
2954fa2dffcSBen Coburn
29689d74a2fSchris      return true;
29789d74a2fSchris    }
29889d74a2fSchris
2994fa2dffcSBen Coburn    /**
3004fa2dffcSBen Coburn    * Generates a two-level table of contents for the config plugin.
3014fa2dffcSBen Coburn    * Uses inc/parser/xhtml.php#render_TOC to format the output.
3024fa2dffcSBen Coburn    * Relies on internal data structures in the Doku_Renderer_xhtml class.
3034fa2dffcSBen Coburn    *
3044fa2dffcSBen Coburn    * @author Ben Coburn <btcoburn@silicodon.net>
3054fa2dffcSBen Coburn    */
3064fa2dffcSBen Coburn    function _print_config_toc() {
307685bdd2eSBen Coburn      $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
308685bdd2eSBen Coburn
3094fa2dffcSBen Coburn      // gather toc data
310685bdd2eSBen Coburn      $has_undefined = false;
3114fa2dffcSBen Coburn      $toc = array('conf'=>array(), 'plugin'=>array(), 'template'=>null);
3124fa2dffcSBen Coburn      foreach($this->_config->setting as $setting) {
3134fa2dffcSBen Coburn        if (is_a($setting, 'setting_fieldset')) {
3144fa2dffcSBen Coburn          if (substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) {
3154fa2dffcSBen Coburn            $toc['plugin'][] = $setting;
3164fa2dffcSBen Coburn          } else if (substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) {
3174fa2dffcSBen Coburn            $toc['template'] = $setting;
3184fa2dffcSBen Coburn          } else {
3194fa2dffcSBen Coburn            $toc['conf'][] = $setting;
3204fa2dffcSBen Coburn          }
321685bdd2eSBen Coburn        } else if (!$has_undefined && is_a($setting, 'setting_undefined')) {
322685bdd2eSBen Coburn          $has_undefined = true;
3234fa2dffcSBen Coburn        }
3244fa2dffcSBen Coburn      }
3254fa2dffcSBen Coburn
3264fa2dffcSBen Coburn      // build toc list
3274fa2dffcSBen Coburn      $xhtml_toc = array();
3284fa2dffcSBen Coburn      $xhtml_toc[] = array('hid' => 'configuration_manager',
3294fa2dffcSBen Coburn          'title' => $this->getLang('_configuration_manager'),
3304fa2dffcSBen Coburn          'type'  => 'ul',
3314fa2dffcSBen Coburn          'level' => 1);
3324fa2dffcSBen Coburn      $xhtml_toc[] = array('hid' => 'dokuwiki_settings',
3334fa2dffcSBen Coburn          'title' => $this->getLang('_header_dokuwiki'),
3344fa2dffcSBen Coburn          'type'  => 'ul',
3354fa2dffcSBen Coburn          'level' => 1);
3364fa2dffcSBen Coburn      foreach($toc['conf'] as $setting) {
3374fa2dffcSBen Coburn        $name = $setting->prompt($this);
3384fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => $setting->_key,
3394fa2dffcSBen Coburn            'title' => $name,
3404fa2dffcSBen Coburn            'type'  => 'ul',
3414fa2dffcSBen Coburn            'level' => 2);
3424fa2dffcSBen Coburn      }
3434fa2dffcSBen Coburn      if (!empty($toc['plugin'])) {
3444fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => 'plugin_settings',
3454fa2dffcSBen Coburn            'title' => $this->getLang('_header_plugin'),
3464fa2dffcSBen Coburn            'type'  => 'ul',
3474fa2dffcSBen Coburn            'level' => 1);
3484fa2dffcSBen Coburn      }
3494fa2dffcSBen Coburn      foreach($toc['plugin'] as $setting) {
3504fa2dffcSBen Coburn        $name = $setting->prompt($this);
3514fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => $setting->_key,
3524fa2dffcSBen Coburn            'title' => $name,
3534fa2dffcSBen Coburn            'type'  => 'ul',
3544fa2dffcSBen Coburn            'level' => 2);
3554fa2dffcSBen Coburn      }
3564fa2dffcSBen Coburn      if (isset($toc['template'])) {
3574fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => 'template_settings',
3584fa2dffcSBen Coburn            'title' => $this->getLang('_header_template'),
3594fa2dffcSBen Coburn            'type'  => 'ul',
3604fa2dffcSBen Coburn            'level' => 1);
3614fa2dffcSBen Coburn        $setting = $toc['template'];
3624fa2dffcSBen Coburn        $name = $setting->prompt($this);
3634fa2dffcSBen Coburn        $xhtml_toc[] = array('hid' => $setting->_key,
3644fa2dffcSBen Coburn            'title' => $name,
3654fa2dffcSBen Coburn            'type'  => 'ul',
3664fa2dffcSBen Coburn            'level' => 2);
3674fa2dffcSBen Coburn      }
368685bdd2eSBen Coburn      if ($has_undefined && $allow_debug) {
369685bdd2eSBen Coburn        $xhtml_toc[] = array('hid' => 'undefined_settings',
370685bdd2eSBen Coburn            'title' => $this->getLang('_header_undefined'),
371685bdd2eSBen Coburn            'type'  => 'ul',
372685bdd2eSBen Coburn            'level' => 1);
373685bdd2eSBen Coburn      }
3744fa2dffcSBen Coburn
3754fa2dffcSBen Coburn      // use the xhtml renderer to make the toc
3764fa2dffcSBen Coburn      require_once(DOKU_INC.'inc/parser/xhtml.php');
3774fa2dffcSBen Coburn      $r = new Doku_Renderer_xhtml;
3784fa2dffcSBen Coburn      $r->toc = $xhtml_toc;
3794fa2dffcSBen Coburn      print $r->render_TOC();
3804fa2dffcSBen Coburn    }
3814fa2dffcSBen Coburn
3824fa2dffcSBen Coburn    function _print_h1($id, $text) {
3834fa2dffcSBen Coburn      ptln('<h1><a name="'.$id.'" id="'.$id.'">'.$text.'</a></h1>');
3844fa2dffcSBen Coburn    }
3854fa2dffcSBen Coburn
3864fa2dffcSBen Coburn
38710449332Schris}
388