3 * @copyright Copyright (C) 2020, Friendica
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 bool $raw Setup the raw config format
104 public function setupCache(Cache $config, $raw = false)
106 // Load static config files first, the order is important
107 $config->load($this->loadStaticConfig('defaults'));
108 $config->load($this->loadStaticConfig('settings'));
110 // try to load the legacy config first
111 $config->load($this->loadLegacyConfig('htpreconfig'), true);
112 $config->load($this->loadLegacyConfig('htconfig'), true);
114 // Now load every other config you find inside the 'config/' directory
115 $this->loadCoreConfig($config);
117 // In case of install mode, add the found basepath (because there isn't a basepath set yet
118 if (!$raw && empty($config->get('system', 'basepath'))) {
119 // Setting at least the basepath we know
120 $config->set('system', 'basepath', $this->baseDir);
125 * Tries to load the static core-configuration and returns the config array.
127 * @param string $name The name of the configuration
129 * @return array The config array (empty if no config found)
131 * @throws Exception if the configuration file isn't readable
133 private function loadStaticConfig($name)
135 $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
136 $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
138 if (file_exists($configName)) {
139 return $this->loadConfigFile($configName);
140 } elseif (file_exists($iniName)) {
141 return $this->loadINIConfigFile($iniName);
148 * Tries to load the specified core-configuration into the config cache.
150 * @param Cache $config The Config cache
152 * @return array The config array (empty if no config found)
154 * @throws Exception if the configuration file isn't readable
156 private function loadCoreConfig(Cache $config)
158 // try to load legacy ini-files first
159 foreach ($this->getConfigFiles(true) as $configFile) {
160 $config->load($this->loadINIConfigFile($configFile), true);
163 // try to load supported config at last to overwrite it
164 foreach ($this->getConfigFiles() as $configFile) {
165 $config->load($this->loadConfigFile($configFile), true);
172 * Tries to load the specified addon-configuration and returns the config array.
174 * @param string $name The name of the configuration
176 * @return array The config array (empty if no config found)
178 * @throws Exception if the configuration file isn't readable
180 public function loadAddonConfig($name)
182 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
183 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
184 $name . DIRECTORY_SEPARATOR . // openstreetmap/
185 self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
186 $name . ".config.php"; // openstreetmap.config.php
188 if (file_exists($filepath)) {
189 return $this->loadConfigFile($filepath);
196 * Get the config files of the config-directory
198 * @param bool $ini True, if scan for ini-files instead of config files
202 private function getConfigFiles(bool $ini = false)
204 $files = scandir($this->configDir);
207 $filePattern = ($ini ? '*.ini.php' : '*.config.php');
209 // Don't load sample files
210 $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
212 foreach ($files as $filename) {
213 if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
214 $found[] = $this->configDir . '/' . $filename;
222 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
224 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
226 * @return array The configuration array (empty if no config found)
228 * @deprecated since version 2018.09
230 private function loadLegacyConfig($name = '')
232 $name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
233 $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
236 if (file_exists($fullName)) {
237 $a = new \stdClass();
241 $htConfigCategories = array_keys($a->config);
243 // map the legacy configuration structure to the current structure
244 foreach ($htConfigCategories as $htConfigCategory) {
245 if (is_array($a->config[$htConfigCategory])) {
246 $keys = array_keys($a->config[$htConfigCategory]);
248 foreach ($keys as $key) {
249 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
252 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
258 if (isset($db_host)) {
259 $config['database']['hostname'] = $db_host;
262 if (isset($db_user)) {
263 $config['database']['username'] = $db_user;
266 if (isset($db_pass)) {
267 $config['database']['password'] = $db_pass;
270 if (isset($db_data)) {
271 $config['database']['database'] = $db_data;
274 if (isset($config['system']['db_charset'])) {
275 $config['database']['charset'] = $config['system']['db_charset'];
277 if (isset($pidfile)) {
278 $config['system']['pidfile'] = $pidfile;
281 if (isset($default_timezone)) {
282 $config['system']['default_timezone'] = $default_timezone;
283 unset($default_timezone);
286 $config['system']['language'] = $lang;
295 * Tries to load the specified legacy configuration file and returns the config array.
297 * @param string $filepath
299 * @return array The configuration array
301 * @deprecated since version 2018.12
303 private function loadINIConfigFile($filepath)
305 $contents = include($filepath);
307 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
309 if ($config === false) {
310 throw new Exception('Error parsing INI config file ' . $filepath);
317 * Tries to load the specified configuration file and returns the config array.
319 * The config format is PHP array and the template for configuration files is the following:
327 * @param string $filepath The filepath of the
329 * @return array The config array0
331 * @throws Exception if the config cannot get loaded.
333 private function loadConfigFile($filepath)
335 $config = include($filepath);
337 if (!is_array($config)) {
338 throw new Exception('Error loading config file ' . $filepath);