3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
25 use Friendica\Core\Addon;
26 use Friendica\Core\Config\Cache;
29 * The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see Cache )
31 * It is capable of loading the following config files:
32 * - *.config.php (current)
33 * - *.ini.php (deprecated)
34 * - *.htconfig.php (deprecated)
36 class ConfigFileLoader
39 * The key of the $_SERVER variable to override the config directory
43 const CONFIG_DIR_ENV = 'FRIENDICA_CONFIG_DIR';
46 * The Sub directory of the config-files
50 const CONFIG_DIR = 'config';
53 * The Sub directory of the static config-files
57 const STATIC_DIR = 'static';
60 * The default name of the user defined ini file
64 const CONFIG_INI = 'local';
67 * The default name of the user defined legacy config file
71 const CONFIG_HTCONFIG = 'htconfig';
74 * The sample string inside the configs, which shouldn't get loaded
78 const SAMPLE_END = '-sample';
93 public function __construct(string $basePath, array $server)
95 $this->baseDir = $basePath;
96 if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
97 $this->configDir = $server[self::CONFIG_DIR_ENV];
99 $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
101 $this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
105 * Load the configuration files into an configuration cache
107 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
108 * expected local.config.php
110 * @param Cache $config The config cache to load to
111 * @param array $server The $_SERVER array
112 * @param bool $raw Setup the raw config format
116 public function setupCache(Cache $config, array $server = [], $raw = false)
118 // Load static config files first, the order is important
119 $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
120 $config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
122 // try to load the legacy config first
123 $config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
124 $config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
126 // Now load every other config you find inside the 'config/' directory
127 $this->loadCoreConfig($config);
129 $config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
131 // In case of install mode, add the found basepath (because there isn't a basepath set yet
132 if (!$raw && empty($config->get('system', 'basepath'))) {
133 // Setting at least the basepath we know
134 $config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
139 * Tries to load the static core-configuration and returns the config array.
141 * @param string $name The name of the configuration
143 * @return array The config array (empty if no config found)
145 * @throws Exception if the configuration file isn't readable
147 private function loadStaticConfig($name)
149 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
150 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
152 if (file_exists($configName)) {
153 return $this->loadConfigFile($configName);
154 } elseif (file_exists($iniName)) {
155 return $this->loadINIConfigFile($iniName);
162 * Tries to load the specified core-configuration into the config cache.
164 * @param Cache $config The Config cache
166 * @return array The config array (empty if no config found)
168 * @throws Exception if the configuration file isn't readable
170 private function loadCoreConfig(Cache $config)
172 // try to load legacy ini-files first
173 foreach ($this->getConfigFiles(true) as $configFile) {
174 $config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
177 // try to load supported config at last to overwrite it
178 foreach ($this->getConfigFiles() as $configFile) {
179 $config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
186 * Tries to load the specified addon-configuration and returns the config array.
188 * @param string $name The name of the configuration
190 * @return array The config array (empty if no config found)
192 * @throws Exception if the configuration file isn't readable
194 public function loadAddonConfig($name)
196 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
197 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
198 $name . DIRECTORY_SEPARATOR . // openstreetmap/
199 self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
200 $name . ".config.php"; // openstreetmap.config.php
202 if (file_exists($filepath)) {
203 return $this->loadConfigFile($filepath);
210 * Tries to load environment specific variables, based on the `env.config.php` mapping table
212 * @param array $server The $_SERVER variable
214 * @return array The config array (empty if no config was found)
216 * @throws Exception if the configuration file isn't readable
218 public function loadEnvConfig(array $server)
220 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
221 self::STATIC_DIR . DIRECTORY_SEPARATOR . // static/
222 "env.config.php"; // env.config.php
224 if (!file_exists($filepath)) {
228 $envConfig = $this->loadConfigFile($filepath);
232 foreach ($envConfig as $envKey => $configStructure) {
233 if (isset($server[$envKey])) {
234 $return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
242 * Get the config files of the config-directory
244 * @param bool $ini True, if scan for ini-files instead of config files
248 private function getConfigFiles(bool $ini = false)
250 $files = scandir($this->configDir);
253 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
255 // Don't load sample files
256 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
258 foreach ($files as $filename) {
259 if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
260 $found[] = $this->configDir . '/' . $filename;
268 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
270 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
272 * @return array The configuration array (empty if no config found)
274 * @deprecated since version 2018.09
276 private function loadLegacyConfig($name = '')
278 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
279 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
282 if (file_exists($fullName)) {
283 $a = new \stdClass();
287 $htConfigCategories = array_keys($a->config);
289 // map the legacy configuration structure to the current structure
290 foreach ($htConfigCategories as $htConfigCategory) {
291 if (is_array($a->config[$htConfigCategory])) {
292 $keys = array_keys($a->config[$htConfigCategory]);
294 foreach ($keys as $key) {
295 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
298 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
304 if (isset($db_host)) {
305 $config['database']['hostname'] = $db_host;
308 if (isset($db_user)) {
309 $config['database']['username'] = $db_user;
312 if (isset($db_pass)) {
313 $config['database']['password'] = $db_pass;
316 if (isset($db_data)) {
317 $config['database']['database'] = $db_data;
320 if (isset($config['system']['db_charset'])) {
321 $config['database']['charset'] = $config['system']['db_charset'];
323 if (isset($pidfile)) {
324 $config['system']['pidfile'] = $pidfile;
327 if (isset($default_timezone)) {
328 $config['system']['default_timezone'] = $default_timezone;
329 unset($default_timezone);
332 $config['system']['language'] = $lang;
341 * Tries to load the specified legacy configuration file and returns the config array.
343 * @param string $filepath
345 * @return array The configuration array
347 * @deprecated since version 2018.12
349 private function loadINIConfigFile($filepath)
351 $contents = include($filepath);
353 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
355 if ($config === false) {
356 throw new Exception('Error parsing INI config file ' . $filepath);
363 * Tries to load the specified configuration file and returns the config array.
365 * The config format is PHP array and the template for configuration files is the following:
373 * @param string $filepath The filepath of the
375 * @return array The config array0
377 * @throws Exception if the config cannot get loaded.
379 private function loadConfigFile($filepath)
381 $config = include($filepath);
383 if (!is_array($config)) {
384 throw new Exception('Error loading config file ' . $filepath);