3 * @copyright Copyright (C) 2010-2023, 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\Core\Config\Util;
24 use Friendica\Core\Addon;
25 use Friendica\Core\Config\Exception\ConfigFileException;
26 use Friendica\Core\Config\ValueObject\Cache;
29 * The ConfigFileLoader loads and saves 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 ConfigFileManager
39 * The default name of the user defined legacy config file
43 const CONFIG_HTCONFIG = 'htconfig';
46 * The config file, where overrides per admin page/console are saved at
50 const CONFIG_DATA_FILE = 'node.config.php';
53 * The sample string inside the configs, which shouldn't get loaded
57 const SAMPLE_END = '-sample';
78 * @param string $baseDir The base
79 * @param string $configDir
80 * @param string $staticDir
82 public function __construct(string $baseDir, string $configDir, string $staticDir, array $server = [])
84 $this->baseDir = $baseDir;
85 $this->configDir = $configDir;
86 $this->staticDir = $staticDir;
87 $this->server = $server;
91 * Load the configuration files into an configuration cache
93 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
94 * expected local.config.php
96 * @param Cache $configCache The config cache to load to
97 * @param bool $raw Set up the raw config format
99 * @throws ConfigFileException
101 public function setupCache(Cache $configCache, bool $raw = false)
103 // Load static config files first, the order is important
104 $configCache->load($this->loadStaticConfig('defaults'), Cache::SOURCE_STATIC);
105 $configCache->load($this->loadStaticConfig('settings'), Cache::SOURCE_STATIC);
107 // try to load the legacy config first
108 $configCache->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
109 $configCache->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
111 // Now load every other config you find inside the 'config/' directory
112 $this->loadCoreConfig($configCache);
114 $configCache->load($this->loadEnvConfig(), Cache::SOURCE_ENV);
116 // In case of install mode, add the found basepath (because there isn't a basepath set yet
117 if (!$raw && empty($configCache->get('system', 'basepath'))) {
118 // Setting at least the basepath we know
119 $configCache->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
124 * Tries to load the static core-configuration and returns the config array.
126 * @param string $name The name of the configuration
128 * @return array The config array (empty if no config found)
130 * @throws ConfigFileException if the configuration file isn't readable
132 private function loadStaticConfig(string $name): array
134 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
135 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
137 if (file_exists($configName)) {
138 return $this->loadConfigFile($configName);
139 } else if (file_exists($iniName)) {
140 return $this->loadINIConfigFile($iniName);
147 * Tries to load the specified core-configuration into the config cache.
149 * @param Cache $configCache The Config cache
151 * @throws ConfigFileException if the configuration file isn't readable
153 private function loadCoreConfig(Cache $configCache)
155 // try to load legacy ini-files first
156 foreach ($this->getConfigFiles(true) as $configFile) {
157 $configCache->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
160 // try to load supported config at last to overwrite it
161 foreach ($this->getConfigFiles() as $configFile) {
162 $configCache->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
167 * Tries to load the specified addon-configuration and returns the config array.
169 * @param string $name The name of the configuration
171 * @return array The config array (empty if no config found)
173 * @throws ConfigFileException if the configuration file isn't readable
175 public function loadAddonConfig(string $name): array
177 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
178 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
179 $name . DIRECTORY_SEPARATOR . // openstreetmap/
180 'config' . DIRECTORY_SEPARATOR . // config/
181 $name . ".config.php"; // openstreetmap.config.php
183 if (file_exists($filepath)) {
184 return $this->loadConfigFile($filepath);
191 * Tries to load environment specific variables, based on the `env.config.php` mapping table
193 * @return array The config array (empty if no config was found)
195 * @throws ConfigFileException if the configuration file isn't readable
197 protected function loadEnvConfig(): array
199 $filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
200 "env.config.php"; // env.config.php
202 if (!file_exists($filepath)) {
206 $envConfig = $this->loadConfigFile($filepath);
210 foreach ($envConfig as $envKey => $configStructure) {
211 if (isset($this->server[$envKey])) {
212 $return[$configStructure[0]][$configStructure[1]] = $this->server[$envKey];
220 * Get the config files of the config-directory
222 * @param bool $ini True, if scan for ini-files instead of config files
226 private function getConfigFiles(bool $ini = false): array
228 $files = scandir($this->configDir);
231 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
233 // Don't load sample files
234 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
236 foreach ($files as $filename) {
237 if (fnmatch($filePattern, $filename) &&
238 substr_compare($filename, $sampleEnd, -strlen($sampleEnd)) &&
239 $filename !== self::CONFIG_DATA_FILE) {
240 $found[] = $this->configDir . '/' . $filename;
248 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
250 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
252 * @return array The configuration array (empty if no config found)
254 * @deprecated since version 2018.09
256 private function loadLegacyConfig(string $name = ''): array
258 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
259 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
262 if (file_exists($fullName)) {
263 $a = new \stdClass();
267 $htConfigCategories = array_keys($a->config);
269 // map the legacy configuration structure to the current structure
270 foreach ($htConfigCategories as $htConfigCategory) {
271 if (is_array($a->config[$htConfigCategory])) {
272 $keys = array_keys($a->config[$htConfigCategory]);
274 foreach ($keys as $key) {
275 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
278 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
284 if (isset($db_host)) {
285 $config['database']['hostname'] = $db_host;
288 if (isset($db_user)) {
289 $config['database']['username'] = $db_user;
292 if (isset($db_pass)) {
293 $config['database']['password'] = $db_pass;
296 if (isset($db_data)) {
297 $config['database']['database'] = $db_data;
300 if (isset($config['system']['db_charset'])) {
301 $config['database']['charset'] = $config['system']['db_charset'];
303 if (isset($pidfile)) {
304 $config['system']['pidfile'] = $pidfile;
307 if (isset($default_timezone)) {
308 $config['system']['default_timezone'] = $default_timezone;
309 unset($default_timezone);
312 $config['system']['language'] = $lang;
321 * Tries to load the specified legacy configuration file and returns the config array.
323 * @param string $filepath
325 * @return array The configuration array
326 * @throws ConfigFileException
327 * @deprecated since version 2018.12
329 private function loadINIConfigFile(string $filepath): array
331 $contents = include($filepath);
333 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
335 if ($config === false) {
336 throw new ConfigFileException('Error parsing INI config file ' . $filepath);
343 * Tries to load the specified configuration file and returns the config array.
345 * The config format is PHP array and the template for configuration files is the following:
353 * @param string $filepath The filepath of the
355 * @return array The config array0
357 * @throws ConfigFileException if the config cannot get loaded.
359 private function loadConfigFile(string $filepath): array
361 if (file_exists($filepath)) {
362 $config = include $filepath;
364 if (!is_array($config)) {
365 throw new ConfigFileException('Error loading config file ' . $filepath);