3 namespace Friendica\Util;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config\Cache\ConfigCache;
11 * The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
13 * It is capable of loading the following config files:
14 * - *.config.php (current)
15 * - *.ini.php (deprecated)
16 * - *.htconfig.php (deprecated)
18 class ConfigFileLoader
21 * The Sub directory of the config-files
25 const CONFIG_DIR = 'config';
28 * The Sub directory of the static config-files
32 const STATIC_DIR = 'static';
35 * The default name of the user defined ini file
39 const CONFIG_INI = 'local';
42 * The default name of the user defined legacy config file
46 const CONFIG_HTCONFIG = 'htconfig';
49 * The sample string inside the configs, which shouldn't get loaded
53 const SAMPLE_END = '-sample';
72 public function __construct($baseDir, App\Mode $mode)
74 $this->baseDir = $baseDir;
75 $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
76 $this->staticDir = $baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
77 $this->appMode = $mode;
81 * Load the configuration files into an configuration cache
83 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
84 * expected local.config.php
86 * @param ConfigCache $config The config cache to load to
87 * @param bool $raw Setup the raw config format
91 public function setupCache(ConfigCache $config, $raw = false)
93 // Load static config files first, the order is important
94 $config->load($this->loadStaticConfig('defaults'));
95 $config->load($this->loadStaticConfig('settings'));
97 // try to load the legacy config first
98 $config->load($this->loadLegacyConfig('htpreconfig'), true);
99 $config->load($this->loadLegacyConfig('htconfig'), true);
101 // Now load every other config you find inside the 'config/' directory
102 $this->loadCoreConfig($config);
104 // In case of install mode, add the found basepath (because there isn't a basepath set yet
105 if (!$raw && ($this->appMode->isInstall() || empty($config->get('system', 'basepath')))) {
106 // Setting at least the basepath we know
107 $config->set('system', 'basepath', $this->baseDir);
112 * Tries to load the static core-configuration and returns the config array.
114 * @param string $name The name of the configuration
116 * @return array The config array (empty if no config found)
118 * @throws Exception if the configuration file isn't readable
120 private function loadStaticConfig($name)
122 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
123 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
125 if (file_exists($configName)) {
126 return $this->loadConfigFile($configName);
127 } elseif (file_exists($iniName)) {
128 return $this->loadINIConfigFile($iniName);
135 * Tries to load the specified core-configuration into the config cache.
137 * @param ConfigCache $config The Config cache
139 * @return array The config array (empty if no config found)
141 * @throws Exception if the configuration file isn't readable
143 private function loadCoreConfig(ConfigCache $config)
145 // try to load legacy ini-files first
146 foreach ($this->getConfigFiles(true) as $configFile) {
147 $config->load($this->loadINIConfigFile($configFile), true);
150 // try to load supported config at last to overwrite it
151 foreach ($this->getConfigFiles() as $configFile) {
152 $config->load($this->loadConfigFile($configFile), true);
159 * Tries to load the specified addon-configuration and returns the config array.
161 * @param string $name The name of the configuration
163 * @return array The config array (empty if no config found)
165 * @throws Exception if the configuration file isn't readable
167 public function loadAddonConfig($name)
169 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
170 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
171 $name . DIRECTORY_SEPARATOR . // openstreetmap/
172 self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
173 $name . ".config.php"; // openstreetmap.config.php
175 if (file_exists($filepath)) {
176 return $this->loadConfigFile($filepath);
183 * Get the config files of the config-directory
185 * @param bool $ini True, if scan for ini-files instead of config files
189 private function getConfigFiles(bool $ini = false)
191 $files = scandir($this->configDir);
194 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
196 // Don't load sample files
197 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
199 foreach ($files as $filename) {
200 if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
201 $found[] = $this->configDir . '/' . $filename;
209 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
211 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
213 * @return array The configuration array (empty if no config found)
215 * @deprecated since version 2018.09
217 private function loadLegacyConfig($name = '')
219 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
220 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
223 if (file_exists($fullName)) {
224 $a = new \stdClass();
228 $htConfigCategories = array_keys($a->config);
230 // map the legacy configuration structure to the current structure
231 foreach ($htConfigCategories as $htConfigCategory) {
232 if (is_array($a->config[$htConfigCategory])) {
233 $keys = array_keys($a->config[$htConfigCategory]);
235 foreach ($keys as $key) {
236 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
239 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
245 if (isset($db_host)) {
246 $config['database']['hostname'] = $db_host;
249 if (isset($db_user)) {
250 $config['database']['username'] = $db_user;
253 if (isset($db_pass)) {
254 $config['database']['password'] = $db_pass;
257 if (isset($db_data)) {
258 $config['database']['database'] = $db_data;
261 if (isset($config['system']['db_charset'])) {
262 $config['database']['charset'] = $config['system']['db_charset'];
264 if (isset($pidfile)) {
265 $config['system']['pidfile'] = $pidfile;
268 if (isset($default_timezone)) {
269 $config['system']['default_timezone'] = $default_timezone;
270 unset($default_timezone);
273 $config['system']['language'] = $lang;
282 * Tries to load the specified legacy configuration file and returns the config array.
284 * @param string $filepath
286 * @return array The configuration array
288 * @deprecated since version 2018.12
290 private function loadINIConfigFile($filepath)
292 $contents = include($filepath);
294 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
296 if ($config === false) {
297 throw new Exception('Error parsing INI config file ' . $filepath);
304 * Tries to load the specified configuration file and returns the config array.
306 * The config format is PHP array and the template for configuration files is the following:
314 * @param string $filepath The filepath of the
316 * @return array The config array0
318 * @throws Exception if the config cannot get loaded.
320 private function loadConfigFile($filepath)
322 $config = include($filepath);
324 if (!is_array($config)) {
325 throw new Exception('Error loading config file ' . $filepath);