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