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 // Now load the node.config.php file with the node specific config values (based on admin gui/console actions)
115 $this->loadDataConfig($configCache);
117 $configCache->load($this->loadEnvConfig(), Cache::SOURCE_ENV);
119 // In case of install mode, add the found basepath (because there isn't a basepath set yet
120 if (!$raw && empty($configCache->get('system', 'basepath'))) {
121 // Setting at least the basepath we know
122 $configCache->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
127 * Tries to load the static core-configuration and returns the config array.
129 * @param string $name The name of the configuration
131 * @return array The config array (empty if no config found)
133 * @throws ConfigFileException if the configuration file isn't readable
135 private function loadStaticConfig(string $name): array
137 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
138 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
140 if (file_exists($configName)) {
141 return $this->loadConfigFile($configName);
142 } else if (file_exists($iniName)) {
143 return $this->loadINIConfigFile($iniName);
150 * Tries to load the specified core-configuration into the config cache.
152 * @param Cache $configCache The Config cache
154 * @throws ConfigFileException if the configuration file isn't readable
156 private function loadCoreConfig(Cache $configCache)
158 // try to load legacy ini-files first
159 foreach ($this->getConfigFiles(true) as $configFile) {
160 $configCache->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
163 // try to load supported config at last to overwrite it
164 foreach ($this->getConfigFiles() as $configFile) {
165 $configCache->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
170 * Tries to load the data config file with the overridden data
172 * @param Cache $configCache The Config cache
174 * @throws ConfigFileException In case the config file isn't loadable
176 private function loadDataConfig(Cache $configCache)
178 $filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
180 if (file_exists($filename)) {
182 // The fallback empty return content
183 $content = '<?php return [];';
186 * This code-block creates a readonly node.config.php content stream (fopen() with "r")
187 * The stream is locked shared (LOCK_SH), so not exclusively, but the OS knows that there's a lock
189 * Any exclusive locking (LOCK_EX) would need to wait until all LOCK_SHs are unlocked
191 $configStream = fopen($filename, 'r');
193 if (flock($configStream, LOCK_SH)) {
194 $content = fread($configStream, filesize($filename));
196 throw new ConfigFileException(sprintf('Couldn\'t read file %s', $filename));
200 // unlock and close the stream for every circumstances
201 flock($configStream, LOCK_UN);
202 fclose($configStream);
206 * Evaluate the fetched content
209 * Because `eval()` directly evaluates PHP content, we need to "close" the expected PHP content again with
210 * the prefixed "?>". Now we're in plain HTML again and can evaluate any PHP file :-)
212 $dataArray = eval('?>' . $content);
214 if (is_array($dataArray)) {
215 $configCache->load($dataArray, Cache::SOURCE_DATA);
221 * Saves overridden config entries back into the data.config.php
223 * @param Cache $configCache The config cache
225 * @throws ConfigFileException In case the config file isn't writeable or the data is invalid
227 public function saveData(Cache $configCache)
229 $filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
231 if (file_exists($filename)) {
238 * Creates a read-write stream
240 * @see https://www.php.net/manual/en/function.fopen.php
241 * @note Open the file for reading and writing. If the file does not exist, it is created.
242 * If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails
243 * (as is the case with 'x'). The file pointer is positioned on the beginning of the file.
246 $configStream = fopen($filename, 'c+');
249 // We do want an exclusive lock, so we wait until every LOCK_SH (config reading) is unlocked
250 if (flock($configStream, LOCK_EX)) {
253 * If the file exists, we read the whole file again to avoid a race condition with concurrent threads that could have modified the file between the first config read of this thread and now
254 * Since we're currently exclusive locked, no other process can now change the config again
257 // When reading the config file too fast, we get a wrong filesize, "clearstatcache" prevents that
258 clearstatcache(true, $filename);
259 $content = fread($configStream, filesize($filename));
260 // Event truncating the whole content wouldn't automatically rewind the stream,
261 // so we need to do it manually
262 rewind($configStream);
264 throw new ConfigFileException(sprintf('Cannot read file %s', $filename));
267 $dataArray = eval('?>' . $content);
269 // Merge the new content into the existing file based config cache and use it
270 // as the new config cache
271 if (is_array($dataArray)) {
272 $fileConfigCache = new Cache();
273 $fileConfigCache->load($dataArray, Cache::SOURCE_DATA);
274 $configCache = $fileConfigCache->merge($configCache);
278 // Only SOURCE_DATA is wanted, the rest isn't part of the node.config.php file
279 $data = $configCache->getDataBySource(Cache::SOURCE_DATA);
281 $encodedData = ConfigFileTransformer::encode($data);
284 throw new ConfigFileException('config source cannot get encoded');
287 // Once again to avoid wrong, implicit "filesize" calls during the fwrite() or ftruncate() call
288 clearstatcache(true, $filename);
289 if (!ftruncate($configStream, 0) ||
290 !fwrite($configStream, $encodedData) ||
291 !fflush($configStream)) {
292 throw new ConfigFileException(sprintf('Cannot modify locked file %s', $filename));
296 // unlock and close the stream for every circumstances
297 flock($configStream, LOCK_UN);
298 fclose($configStream);
303 * Tries to load the specified addon-configuration and returns the config array.
305 * @param string $name The name of the configuration
307 * @return array The config array (empty if no config found)
309 * @throws ConfigFileException if the configuration file isn't readable
311 public function loadAddonConfig(string $name): array
313 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
314 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
315 $name . DIRECTORY_SEPARATOR . // openstreetmap/
316 'config' . DIRECTORY_SEPARATOR . // config/
317 $name . ".config.php"; // openstreetmap.config.php
319 if (file_exists($filepath)) {
320 return $this->loadConfigFile($filepath);
327 * Tries to load environment specific variables, based on the `env.config.php` mapping table
329 * @return array The config array (empty if no config was found)
331 * @throws ConfigFileException if the configuration file isn't readable
333 protected function loadEnvConfig(): array
335 $filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
336 "env.config.php"; // env.config.php
338 if (!file_exists($filepath)) {
342 $envConfig = $this->loadConfigFile($filepath);
346 foreach ($envConfig as $envKey => $configStructure) {
347 if (isset($this->server[$envKey])) {
348 $return[$configStructure[0]][$configStructure[1]] = $this->server[$envKey];
356 * Get the config files of the config-directory
358 * @param bool $ini True, if scan for ini-files instead of config files
362 private function getConfigFiles(bool $ini = false): array
364 $files = scandir($this->configDir);
367 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
369 // Don't load sample files
370 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
372 foreach ($files as $filename) {
373 if (fnmatch($filePattern, $filename) &&
374 substr_compare($filename, $sampleEnd, -strlen($sampleEnd)) &&
375 $filename !== self::CONFIG_DATA_FILE) {
376 $found[] = $this->configDir . '/' . $filename;
384 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
386 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
388 * @return array The configuration array (empty if no config found)
390 * @deprecated since version 2018.09
392 private function loadLegacyConfig(string $name = ''): array
394 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
395 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
398 if (file_exists($fullName)) {
399 $a = new \stdClass();
403 $htConfigCategories = array_keys($a->config);
405 // map the legacy configuration structure to the current structure
406 foreach ($htConfigCategories as $htConfigCategory) {
407 if (is_array($a->config[$htConfigCategory])) {
408 $keys = array_keys($a->config[$htConfigCategory]);
410 foreach ($keys as $key) {
411 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
414 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
420 if (isset($db_host)) {
421 $config['database']['hostname'] = $db_host;
424 if (isset($db_user)) {
425 $config['database']['username'] = $db_user;
428 if (isset($db_pass)) {
429 $config['database']['password'] = $db_pass;
432 if (isset($db_data)) {
433 $config['database']['database'] = $db_data;
436 if (isset($config['system']['db_charset'])) {
437 $config['database']['charset'] = $config['system']['db_charset'];
439 if (isset($pidfile)) {
440 $config['system']['pidfile'] = $pidfile;
443 if (isset($default_timezone)) {
444 $config['system']['default_timezone'] = $default_timezone;
445 unset($default_timezone);
448 $config['system']['language'] = $lang;
457 * Tries to load the specified legacy configuration file and returns the config array.
459 * @param string $filepath
461 * @return array The configuration array
462 * @throws ConfigFileException
463 * @deprecated since version 2018.12
465 private function loadINIConfigFile(string $filepath): array
467 $contents = include($filepath);
469 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
471 if ($config === false) {
472 throw new ConfigFileException('Error parsing INI config file ' . $filepath);
479 * Tries to load the specified configuration file and returns the config array.
481 * The config format is PHP array and the template for configuration files is the following:
489 * @param string $filepath The filepath of the
491 * @return array The config array0
493 * @throws ConfigFileException if the config cannot get loaded.
495 private function loadConfigFile(string $filepath): array
497 if (file_exists($filepath)) {
498 $config = include $filepath;
500 if (!is_array($config)) {
501 throw new ConfigFileException('Error loading config file ' . $filepath);