3 namespace Friendica\Util;
6 use Friendica\Core\Addon;
7 use Friendica\Core\Config\Cache;
10 * The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see Cache )
12 * It is capable of loading the following config files:
13 * - *.config.php (current)
14 * - *.ini.php (deprecated)
15 * - *.htconfig.php (deprecated)
17 class ConfigFileLoader
20 * The Sub directory of the config-files
24 const CONFIG_DIR = 'config';
27 * The Sub directory of the static config-files
31 const STATIC_DIR = 'static';
34 * The default name of the user defined ini file
38 const CONFIG_INI = 'local';
41 * The default name of the user defined legacy config file
45 const CONFIG_HTCONFIG = 'htconfig';
48 * The sample string inside the configs, which shouldn't get loaded
52 const SAMPLE_END = '-sample';
67 public function __construct(string $basePath)
69 $this->baseDir = $basePath;
70 $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
71 $this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
75 * Load the configuration files into an configuration cache
77 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
78 * expected local.config.php
80 * @param Cache $config The config cache to load to
81 * @param bool $raw Setup the raw config format
85 public function setupCache(Cache $config, $raw = false)
87 // Load static config files first, the order is important
88 $config->load($this->loadStaticConfig('defaults'));
89 $config->load($this->loadStaticConfig('settings'));
91 // try to load the legacy config first
92 $config->load($this->loadLegacyConfig('htpreconfig'), true);
93 $config->load($this->loadLegacyConfig('htconfig'), true);
95 // Now load every other config you find inside the 'config/' directory
96 $this->loadCoreConfig($config);
98 // In case of install mode, add the found basepath (because there isn't a basepath set yet
99 if (!$raw && empty($config->get('system', 'basepath'))) {
100 // Setting at least the basepath we know
101 $config->set('system', 'basepath', $this->baseDir);
106 * Tries to load the static core-configuration and returns the config array.
108 * @param string $name The name of the configuration
110 * @return array The config array (empty if no config found)
112 * @throws Exception if the configuration file isn't readable
114 private function loadStaticConfig($name)
116 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
117 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
119 if (file_exists($configName)) {
120 return $this->loadConfigFile($configName);
121 } elseif (file_exists($iniName)) {
122 return $this->loadINIConfigFile($iniName);
129 * Tries to load the specified core-configuration into the config cache.
131 * @param Cache $config The Config cache
133 * @return array The config array (empty if no config found)
135 * @throws Exception if the configuration file isn't readable
137 private function loadCoreConfig(Cache $config)
139 // try to load legacy ini-files first
140 foreach ($this->getConfigFiles(true) as $configFile) {
141 $config->load($this->loadINIConfigFile($configFile), true);
144 // try to load supported config at last to overwrite it
145 foreach ($this->getConfigFiles() as $configFile) {
146 $config->load($this->loadConfigFile($configFile), true);
153 * Tries to load the specified addon-configuration and returns the config array.
155 * @param string $name The name of the configuration
157 * @return array The config array (empty if no config found)
159 * @throws Exception if the configuration file isn't readable
161 public function loadAddonConfig($name)
163 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
164 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
165 $name . DIRECTORY_SEPARATOR . // openstreetmap/
166 self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
167 $name . ".config.php"; // openstreetmap.config.php
169 if (file_exists($filepath)) {
170 return $this->loadConfigFile($filepath);
177 * Get the config files of the config-directory
179 * @param bool $ini True, if scan for ini-files instead of config files
183 private function getConfigFiles(bool $ini = false)
185 $files = scandir($this->configDir);
188 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
190 // Don't load sample files
191 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
193 foreach ($files as $filename) {
194 if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
195 $found[] = $this->configDir . '/' . $filename;
203 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
205 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
207 * @return array The configuration array (empty if no config found)
209 * @deprecated since version 2018.09
211 private function loadLegacyConfig($name = '')
213 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
214 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
217 if (file_exists($fullName)) {
218 $a = new \stdClass();
222 $htConfigCategories = array_keys($a->config);
224 // map the legacy configuration structure to the current structure
225 foreach ($htConfigCategories as $htConfigCategory) {
226 if (is_array($a->config[$htConfigCategory])) {
227 $keys = array_keys($a->config[$htConfigCategory]);
229 foreach ($keys as $key) {
230 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
233 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
239 if (isset($db_host)) {
240 $config['database']['hostname'] = $db_host;
243 if (isset($db_user)) {
244 $config['database']['username'] = $db_user;
247 if (isset($db_pass)) {
248 $config['database']['password'] = $db_pass;
251 if (isset($db_data)) {
252 $config['database']['database'] = $db_data;
255 if (isset($config['system']['db_charset'])) {
256 $config['database']['charset'] = $config['system']['db_charset'];
258 if (isset($pidfile)) {
259 $config['system']['pidfile'] = $pidfile;
262 if (isset($default_timezone)) {
263 $config['system']['default_timezone'] = $default_timezone;
264 unset($default_timezone);
267 $config['system']['language'] = $lang;
276 * Tries to load the specified legacy configuration file and returns the config array.
278 * @param string $filepath
280 * @return array The configuration array
282 * @deprecated since version 2018.12
284 private function loadINIConfigFile($filepath)
286 $contents = include($filepath);
288 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
290 if ($config === false) {
291 throw new Exception('Error parsing INI config file ' . $filepath);
298 * Tries to load the specified configuration file and returns the config array.
300 * The config format is PHP array and the template for configuration files is the following:
308 * @param string $filepath The filepath of the
310 * @return array The config array0
312 * @throws Exception if the config cannot get loaded.
314 private function loadConfigFile($filepath)
316 $config = include($filepath);
318 if (!is_array($config)) {
319 throw new Exception('Error loading config file ' . $filepath);