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 Sub directory of the config-files
43 const CONFIG_DIR = 'config';
46 * The Sub directory of the static config-files
50 const STATIC_DIR = 'static';
53 * The default name of the user defined ini file
57 const CONFIG_INI = 'local';
60 * The default name of the user defined legacy config file
64 const CONFIG_HTCONFIG = 'htconfig';
67 * The sample string inside the configs, which shouldn't get loaded
71 const SAMPLE_END = '-sample';
86 public function __construct(string $basePath)
88 $this->baseDir = $basePath;
89 $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
90 $this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
94 * Load the configuration files into an configuration cache
96 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
97 * expected local.config.php
99 * @param Cache $config The config cache to load to
100 * @param array $server The $_SERVER array
101 * @param bool $raw Setup the raw config format
105 public function setupCache(Cache $config, array $server = [], $raw = false)
107 // Load static config files first, the order is important
108 $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
109 $config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
111 // try to load the legacy config first
112 $config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
113 $config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
115 // Now load every other config you find inside the 'config/' directory
116 $this->loadCoreConfig($config);
118 $config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
120 // In case of install mode, add the found basepath (because there isn't a basepath set yet
121 if (!$raw && empty($config->get('system', 'basepath'))) {
122 // Setting at least the basepath we know
123 $config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
128 * Tries to load the static core-configuration and returns the config array.
130 * @param string $name The name of the configuration
132 * @return array The config array (empty if no config found)
134 * @throws Exception if the configuration file isn't readable
136 private function loadStaticConfig($name)
138 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
139 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
141 if (file_exists($configName)) {
142 return $this->loadConfigFile($configName);
143 } elseif (file_exists($iniName)) {
144 return $this->loadINIConfigFile($iniName);
151 * Tries to load the specified core-configuration into the config cache.
153 * @param Cache $config The Config cache
155 * @return array The config array (empty if no config found)
157 * @throws Exception if the configuration file isn't readable
159 private function loadCoreConfig(Cache $config)
161 // try to load legacy ini-files first
162 foreach ($this->getConfigFiles(true) as $configFile) {
163 $config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
166 // try to load supported config at last to overwrite it
167 foreach ($this->getConfigFiles() as $configFile) {
168 $config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
175 * Tries to load the specified addon-configuration and returns the config array.
177 * @param string $name The name of the configuration
179 * @return array The config array (empty if no config found)
181 * @throws Exception if the configuration file isn't readable
183 public function loadAddonConfig($name)
185 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
186 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
187 $name . DIRECTORY_SEPARATOR . // openstreetmap/
188 self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
189 $name . ".config.php"; // openstreetmap.config.php
191 if (file_exists($filepath)) {
192 return $this->loadConfigFile($filepath);
199 * Tries to load environment specific variables, based on the `env.config.php` mapping table
201 * @param array $server The $_SERVER variable
203 * @return array The config array (empty if no config was found)
205 * @throws Exception if the configuration file isn't readable
207 public function loadEnvConfig(array $server)
209 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
210 self::STATIC_DIR . DIRECTORY_SEPARATOR . // static/
211 "env.config.php"; // env.config.php
213 if (!file_exists($filepath)) {
217 $envConfig = $this->loadConfigFile($filepath);
221 foreach ($envConfig as $envKey => $configStructure) {
222 if (isset($server[$envKey])) {
223 $return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
231 * Get the config files of the config-directory
233 * @param bool $ini True, if scan for ini-files instead of config files
237 private function getConfigFiles(bool $ini = false)
239 $files = scandir($this->configDir);
242 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
244 // Don't load sample files
245 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
247 foreach ($files as $filename) {
248 if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
249 $found[] = $this->configDir . '/' . $filename;
257 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
259 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
261 * @return array The configuration array (empty if no config found)
263 * @deprecated since version 2018.09
265 private function loadLegacyConfig($name = '')
267 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
268 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
271 if (file_exists($fullName)) {
272 $a = new \stdClass();
276 $htConfigCategories = array_keys($a->config);
278 // map the legacy configuration structure to the current structure
279 foreach ($htConfigCategories as $htConfigCategory) {
280 if (is_array($a->config[$htConfigCategory])) {
281 $keys = array_keys($a->config[$htConfigCategory]);
283 foreach ($keys as $key) {
284 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
287 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
293 if (isset($db_host)) {
294 $config['database']['hostname'] = $db_host;
297 if (isset($db_user)) {
298 $config['database']['username'] = $db_user;
301 if (isset($db_pass)) {
302 $config['database']['password'] = $db_pass;
305 if (isset($db_data)) {
306 $config['database']['database'] = $db_data;
309 if (isset($config['system']['db_charset'])) {
310 $config['database']['charset'] = $config['system']['db_charset'];
312 if (isset($pidfile)) {
313 $config['system']['pidfile'] = $pidfile;
316 if (isset($default_timezone)) {
317 $config['system']['default_timezone'] = $default_timezone;
318 unset($default_timezone);
321 $config['system']['language'] = $lang;
330 * Tries to load the specified legacy configuration file and returns the config array.
332 * @param string $filepath
334 * @return array The configuration array
336 * @deprecated since version 2018.12
338 private function loadINIConfigFile($filepath)
340 $contents = include($filepath);
342 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
344 if ($config === false) {
345 throw new Exception('Error parsing INI config file ' . $filepath);
352 * Tries to load the specified configuration file and returns the config array.
354 * The config format is PHP array and the template for configuration files is the following:
362 * @param string $filepath The filepath of the
364 * @return array The config array0
366 * @throws Exception if the config cannot get loaded.
368 private function loadConfigFile($filepath)
370 $config = include($filepath);
372 if (!is_array($config)) {
373 throw new Exception('Error loading config file ' . $filepath);