1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
<?php
/**
* Configuration Manager admin plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Christopher Smith <chris@jalakai.co.uk>
* @author Ben Coburn <btcoburn@silicodon.net>
*/
use dokuwiki\Extension\AdminPlugin;
use dokuwiki\plugin\config\core\Configuration;
use dokuwiki\plugin\config\core\Setting\Setting;
use dokuwiki\plugin\config\core\Setting\SettingFieldset;
use dokuwiki\plugin\config\core\Setting\SettingHidden;
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_config extends AdminPlugin
{
protected const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/';
/** @var Configuration */
protected $configuration;
/** @var bool were there any errors in the submitted data? */
protected $hasErrors = false;
/** @var bool have the settings translations been loaded? */
protected $promptsLocalized = false;
/**
* handle user request
*/
public function handle()
{
global $ID, $INPUT;
// always initialize the configuration
$this->configuration = new Configuration();
if (!$INPUT->bool('save') || !checkSecurityToken()) {
return;
}
// don't go any further if the configuration is locked
if ($this->configuration->isLocked()) return;
// update settings and redirect of successful
$ok = $this->configuration->updateSettings($INPUT->arr('config'));
if ($ok) { // no errors
try {
if ($this->configuration->hasChanged()) {
$this->configuration->save();
} else {
$this->configuration->touch();
}
msg($this->getLang('updated'), 1);
} catch (Exception $e) {
msg($this->getLang('error'), -1);
}
send_redirect(wl($ID, ['do' => 'admin', 'page' => 'config'], true, '&'));
} else {
$this->hasErrors = true;
msg($this->getLang('error'), -1);
}
}
/**
* output appropriate html
*/
public function html()
{
$allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
global $lang;
global $ID;
$this->setupLocale(true);
echo $this->locale_xhtml('intro');
echo '<div id="config__manager">';
if ($this->configuration->isLocked()) {
echo '<div class="info">' . $this->getLang('locked') . '</div>';
}
// POST to script() instead of wl($ID) so config manager still works if
// rewrite config is broken. Add $ID as hidden field to remember
// current ID in most cases.
echo '<form id="dw__configform" action="' . script() . '" method="post">';
echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>';
formSecurityToken();
$this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
$in_fieldset = false;
$first_plugin_fieldset = true;
$first_template_fieldset = true;
foreach ($this->configuration->getSettings() as $setting) {
if ($setting instanceof SettingHidden) {
continue;
} elseif ($setting instanceof SettingFieldset) {
// config setting group
if ($in_fieldset) {
echo '</table>';
echo '</div>';
echo '</fieldset>';
} else {
$in_fieldset = true;
}
if ($first_plugin_fieldset && $setting->getType() == 'plugin') {
$this->printH1('plugin_settings', $this->getLang('_header_plugin'));
$first_plugin_fieldset = false;
} elseif ($first_template_fieldset && $setting->getType() == 'template') {
$this->printH1('template_settings', $this->getLang('_header_template'));
$first_template_fieldset = false;
}
echo '<fieldset id="' . $setting->getKey() . '">';
echo '<legend>' . $setting->prompt($this) . '</legend>';
echo '<div class="table">';
echo '<table class="inline">';
} else {
// config settings
[$label, $input] = $setting->html($this, $this->hasErrors);
$class = $setting->isDefault()
? ' class="default"'
: ($setting->isProtected() ? ' class="protected"' : '');
$error = $setting->hasError()
? ' class="value error"'
: ' class="value"';
$icon = $setting->caution()
? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' .
'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />'
: '';
echo '<tr' . $class . '>';
echo '<td class="label">';
echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>';
echo $icon . $label;
echo '</td>';
echo '<td' . $error . '>' . $input . '</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
if ($in_fieldset) {
echo '</fieldset>';
}
// show undefined settings list
$undefined_settings = $this->configuration->getUndefined();
if ($allow_debug && !empty($undefined_settings)) {
/**
* Callback for sorting settings
*
* @param Setting $a
* @param Setting $b
* @return int if $a is lower/equal/higher than $b
*/
function settingNaturalComparison($a, $b)
{
return strnatcmp($a->getKey(), $b->getKey());
}
usort($undefined_settings, 'settingNaturalComparison');
$this->printH1('undefined_settings', $this->getLang('_header_undefined'));
echo '<fieldset>';
echo '<div class="table">';
echo '<table class="inline">';
foreach ($undefined_settings as $setting) {
[$label, $input] = $setting->html($this);
echo '<tr>';
echo '<td class="label">' . $label . '</td>';
echo '<td>' . $input . '</td>';
echo '</tr>';
}
echo '</table>';
echo '</div>';
echo '</fieldset>';
}
// finish up form
echo '<p>';
echo '<input type="hidden" name="do" value="admin" />';
echo '<input type="hidden" name="page" value="config" />';
if (!$this->configuration->isLocked()) {
echo '<input type="hidden" name="save" value="1" />';
echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>';
echo '<button type="reset">' . $lang['btn_reset'] . '</button>';
}
echo '</p>';
echo '</form>';
echo '</div>';
}
/**
* @param bool $prompts
*/
public function setupLocale($prompts = false)
{
parent::setupLocale();
if (!$prompts || $this->promptsLocalized) return;
$this->lang = array_merge($this->lang, $this->configuration->getLangs());
$this->promptsLocalized = true;
}
/**
* Generates a two-level table of contents for the config plugin.
*
* @author Ben Coburn <btcoburn@silicodon.net>
*
* @return array
*/
public function getTOC()
{
$this->setupLocale(true);
$allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
$toc = [];
$check = false;
// gather settings data into three sub arrays
$labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []];
foreach ($this->configuration->getSettings() as $setting) {
if ($setting instanceof SettingFieldset) {
$labels[$setting->getType()][] = $setting;
}
}
// top header
$title = $this->getLang('_configuration_manager');
$toc[] = html_mktocitem(sectionID($title, $check), $title, 1);
// main entries
foreach (['dokuwiki', 'plugin', 'template'] as $section) {
if (empty($labels[$section])) continue; // no entries, skip
// create main header
$toc[] = html_mktocitem(
$section . '_settings',
$this->getLang('_header_' . $section),
1
);
// create sub headers
foreach ($labels[$section] as $setting) {
/** @var SettingFieldset $setting */
$name = $setting->prompt($this);
$toc[] = html_mktocitem($setting->getKey(), $name, 2);
}
}
// undefined settings if allowed
if (count($this->configuration->getUndefined()) && $allow_debug) {
$toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
}
return $toc;
}
/**
* @param string $id
* @param string $text
*/
protected function printH1($id, $text)
{
echo '<h1 id="' . $id . '">' . $text . '</h1>';
}
/**
* Adds a translation to this plugin's language array
*
* Used by some settings to set up dynamic translations
*
* @param string $key
* @param string $value
*/
public function addLang($key, $value)
{
if (!$this->localised) $this->setupLocale();
$this->lang[$key] = $value;
}
}
|