1<?php 2 3namespace dokuwiki\plugin\config\core; 4 5use dokuwiki\Extension\Event; 6 7/** 8 * Configuration loader 9 * 10 * Loads configuration meta data and settings from the various files. Honors the 11 * configuration cascade and installed plugins. 12 */ 13class Loader 14{ 15 /** @var ConfigParser */ 16 protected $parser; 17 18 /** @var string[] list of enabled plugins */ 19 protected $plugins; 20 /** @var string current template */ 21 protected $template; 22 23 /** 24 * Loader constructor. 25 * @param ConfigParser $parser 26 * @triggers PLUGIN_CONFIG_PLUGINLIST 27 */ 28 public function __construct(ConfigParser $parser) { 29 global $conf; 30 $this->parser = $parser; 31 $this->plugins = plugin_list(); 32 $this->template = $conf['template']; 33 // allow plugins to remove configurable plugins 34 Event::createAndTrigger('PLUGIN_CONFIG_PLUGINLIST', $this->plugins); 35 } 36 37 /** 38 * Read the settings meta data 39 * 40 * Reads the main file, plugins and template settings meta data 41 * 42 * @return array 43 */ 44 public function loadMeta() { 45 // load main file 46 $meta = []; 47 include DOKU_PLUGIN . 'config/settings/config.metadata.php'; 48 49 // plugins 50 foreach($this->plugins as $plugin) { 51 $meta = array_merge( 52 $meta, 53 $this->loadExtensionMeta( 54 DOKU_PLUGIN . $plugin . '/conf/metadata.php', 55 'plugin', 56 $plugin 57 ) 58 ); 59 } 60 61 // current template 62 $meta = array_merge( 63 $meta, 64 $this->loadExtensionMeta( 65 tpl_incdir() . '/conf/metadata.php', 66 'tpl', 67 $this->template 68 ) 69 ); 70 71 return $meta; 72 } 73 74 /** 75 * Read the default values 76 * 77 * Reads the main file, plugins and template defaults 78 * 79 * @return array 80 */ 81 public function loadDefaults() 82 { 83 84 // initialize array 85 $conf = []; 86 87 // plugins 88 foreach ($this->plugins as $plugin) { 89 $conf = array_merge( 90 $conf, 91 $this->loadExtensionConf( 92 DOKU_PLUGIN . $plugin . '/conf/default.php', 93 'plugin', 94 $plugin 95 ) 96 ); 97 } 98 99 // current template 100 $conf = array_merge( 101 $conf, 102 $this->loadExtensionConf( 103 tpl_incdir() . '/conf/default.php', 104 'tpl', 105 $this->template 106 ) 107 ); 108 109 // load main files 110 global $config_cascade; 111 return array_merge( 112 $conf, 113 $this->loadConfigs($config_cascade['main']['default']) 114 ); 115 } 116 117 /** 118 * Reads the language strings 119 * 120 * Only reads extensions, main one is loaded the usual way 121 * 122 * @return array 123 */ 124 public function loadLangs() { 125 $lang = []; 126 127 // plugins 128 foreach($this->plugins as $plugin) { 129 $lang = array_merge( 130 $lang, 131 $this->loadExtensionLang( 132 DOKU_PLUGIN . $plugin . '/', 133 'plugin', 134 $plugin 135 ) 136 ); 137 } 138 139 // current template 140 $lang = array_merge( 141 $lang, 142 $this->loadExtensionLang( 143 tpl_incdir() . '/', 144 'tpl', 145 $this->template 146 ) 147 ); 148 149 return $lang; 150 } 151 152 /** 153 * Read the local settings 154 * 155 * @return array 156 */ 157 public function loadLocal() { 158 global $config_cascade; 159 return $this->loadConfigs($config_cascade['main']['local']); 160 } 161 162 /** 163 * Read the protected settings 164 * 165 * @return array 166 */ 167 public function loadProtected() { 168 global $config_cascade; 169 return $this->loadConfigs($config_cascade['main']['protected']); 170 } 171 172 /** 173 * Read the config values from the given files 174 * 175 * @param string[] $files paths to config php's 176 * @return array 177 */ 178 protected function loadConfigs($files) { 179 $conf = []; 180 foreach($files as $file) { 181 $conf = array_merge($conf, $this->parser->parse($file)); 182 } 183 return $conf; 184 } 185 186 /** 187 * Read settings file from an extension 188 * 189 * This is used to read the settings.php files of plugins and templates 190 * 191 * @param string $file php file to read 192 * @param string $type should be 'plugin' or 'tpl' 193 * @param string $extname name of the extension 194 * @return array 195 */ 196 protected function loadExtensionMeta($file, $type, $extname) { 197 if(!file_exists($file)) return []; 198 $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER; 199 200 // include file 201 $meta = []; 202 include $file; 203 if($meta === []) return []; 204 205 // read data 206 $data = []; 207 $data[$prefix . $type . '_settings_name'] = ['fieldset']; 208 foreach($meta as $key => $value) { 209 if($value[0] == 'fieldset') continue; //plugins only get one fieldset 210 $data[$prefix . $key] = $value; 211 } 212 213 return $data; 214 } 215 216 /** 217 * Read a default file from an extension 218 * 219 * This is used to read the default.php files of plugins and templates 220 * 221 * @param string $file php file to read 222 * @param string $type should be 'plugin' or 'tpl' 223 * @param string $extname name of the extension 224 * @return array 225 */ 226 protected function loadExtensionConf($file, $type, $extname) { 227 if(!file_exists($file)) return []; 228 $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER; 229 230 // parse file 231 $conf = $this->parser->parse($file); 232 if(empty($conf)) return []; 233 234 // read data 235 $data = []; 236 foreach($conf as $key => $value) { 237 $data[$prefix . $key] = $value; 238 } 239 240 return $data; 241 } 242 243 /** 244 * Read the language file of an extension 245 * 246 * @param string $dir directory of the extension 247 * @param string $type should be 'plugin' or 'tpl' 248 * @param string $extname name of the extension 249 * @return array 250 */ 251 protected function loadExtensionLang($dir, $type, $extname) { 252 global $conf; 253 $ll = $conf['lang']; 254 $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER; 255 256 // include files 257 $lang = []; 258 if(file_exists($dir . 'lang/en/settings.php')) { 259 include $dir . 'lang/en/settings.php'; 260 } 261 if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) { 262 include $dir . 'lang/' . $ll . '/settings.php'; 263 } 264 265 // set up correct keys 266 $strings = []; 267 foreach($lang as $key => $val) { 268 $strings[$prefix . $key] = $val; 269 } 270 271 // add fieldset key 272 $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname)); 273 274 return $strings; 275 } 276} 277