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 default name of the user defined ini file
43 const CONFIG_INI = 'local';
46 * The default name of the user defined legacy config file
50 const CONFIG_HTCONFIG = 'htconfig';
53 * The sample string inside the configs, which shouldn't get loaded
57 const SAMPLE_END = '-sample';
73 * @param string $baseDir The base
74 * @param string $configDir
75 * @param string $staticDir
77 public function __construct(string $baseDir, string $configDir, string $staticDir)
79 $this->baseDir = $baseDir;
80 $this->configDir = $configDir;
81 $this->staticDir = $staticDir;
85 * Load the configuration files into an configuration cache
87 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
88 * expected local.config.php
90 * @param Cache $config The config cache to load to
91 * @param array $server The $_SERVER array
92 * @param bool $raw Setup the raw config format
96 public function setupCache(Cache $config, array $server = [], bool $raw = false)
98 // Load static config files first, the order is important
99 $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
100 $config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
102 // try to load the legacy config first
103 $config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
104 $config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
106 // Now load every other config you find inside the 'config/' directory
107 $this->loadCoreConfig($config);
109 $config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
111 // In case of install mode, add the found basepath (because there isn't a basepath set yet
112 if (!$raw && empty($config->get('system', 'basepath'))) {
113 // Setting at least the basepath we know
114 $config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
119 * Tries to load the static core-configuration and returns the config array.
121 * @param string $name The name of the configuration
123 * @return array The config array (empty if no config found)
125 * @throws Exception if the configuration file isn't readable
127 private function loadStaticConfig($name)
129 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
130 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
132 if (file_exists($configName)) {
133 return $this->loadConfigFile($configName);
134 } elseif (file_exists($iniName)) {
135 return $this->loadINIConfigFile($iniName);
142 * Tries to load the specified core-configuration into the config cache.
144 * @param Cache $config The Config cache
146 * @return array The config array (empty if no config found)
148 * @throws Exception if the configuration file isn't readable
150 private function loadCoreConfig(Cache $config)
152 // try to load legacy ini-files first
153 foreach ($this->getConfigFiles(true) as $configFile) {
154 $config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
157 // try to load supported config at last to overwrite it
158 foreach ($this->getConfigFiles() as $configFile) {
159 $config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
166 * Tries to load the specified addon-configuration and returns the config array.
168 * @param string $name The name of the configuration
170 * @return array The config array (empty if no config found)
172 * @throws Exception if the configuration file isn't readable
174 public function loadAddonConfig($name)
176 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
177 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
178 $name . DIRECTORY_SEPARATOR . // openstreetmap/
179 'config'. DIRECTORY_SEPARATOR . // config/
180 $name . ".config.php"; // openstreetmap.config.php
182 if (file_exists($filepath)) {
183 return $this->loadConfigFile($filepath);
190 * Tries to load environment specific variables, based on the `env.config.php` mapping table
192 * @param array $server The $_SERVER variable
194 * @return array The config array (empty if no config was found)
196 * @throws Exception if the configuration file isn't readable
198 public function loadEnvConfig(array $server)
200 $filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
201 "env.config.php"; // env.config.php
203 if (!file_exists($filepath)) {
207 $envConfig = $this->loadConfigFile($filepath);
211 foreach ($envConfig as $envKey => $configStructure) {
212 if (isset($server[$envKey])) {
213 $return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
221 * Get the config files of the config-directory
223 * @param bool $ini True, if scan for ini-files instead of config files
227 private function getConfigFiles(bool $ini = false)
229 $files = scandir($this->configDir);
232 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
234 // Don't load sample files
235 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
237 foreach ($files as $filename) {
238 if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
239 $found[] = $this->configDir . '/' . $filename;
247 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
249 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
251 * @return array The configuration array (empty if no config found)
253 * @deprecated since version 2018.09
255 private function loadLegacyConfig($name = '')
257 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
258 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
261 if (file_exists($fullName)) {
262 $a = new \stdClass();
266 $htConfigCategories = array_keys($a->config);
268 // map the legacy configuration structure to the current structure
269 foreach ($htConfigCategories as $htConfigCategory) {
270 if (is_array($a->config[$htConfigCategory])) {
271 $keys = array_keys($a->config[$htConfigCategory]);
273 foreach ($keys as $key) {
274 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
277 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
283 if (isset($db_host)) {
284 $config['database']['hostname'] = $db_host;
287 if (isset($db_user)) {
288 $config['database']['username'] = $db_user;
291 if (isset($db_pass)) {
292 $config['database']['password'] = $db_pass;
295 if (isset($db_data)) {
296 $config['database']['database'] = $db_data;
299 if (isset($config['system']['db_charset'])) {
300 $config['database']['charset'] = $config['system']['db_charset'];
302 if (isset($pidfile)) {
303 $config['system']['pidfile'] = $pidfile;
306 if (isset($default_timezone)) {
307 $config['system']['default_timezone'] = $default_timezone;
308 unset($default_timezone);
311 $config['system']['language'] = $lang;
320 * Tries to load the specified legacy configuration file and returns the config array.
322 * @param string $filepath
324 * @return array The configuration array
326 * @deprecated since version 2018.12
328 private function loadINIConfigFile($filepath)
330 $contents = include($filepath);
332 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
334 if ($config === false) {
335 throw new Exception('Error parsing INI config file ' . $filepath);
342 * Tries to load the specified configuration file and returns the config array.
344 * The config format is PHP array and the template for configuration files is the following:
352 * @param string $filepath The filepath of the
354 * @return array The config array0
356 * @throws Exception if the config cannot get loaded.
358 private function loadConfigFile($filepath)
360 $config = include($filepath);
362 if (!is_array($config)) {
363 throw new Exception('Error loading config file ' . $filepath);