3 namespace Friendica\Util\Config;
6 use Friendica\Core\Addon;
7 use Friendica\Core\Config\Cache\IConfigCache;
10 * The ConfigFileLoader loads config-files and stores them in a IConfigCache ( @see IConfigCache )
12 * It is capable of loading the following config files:
13 * - *.config.php (current)
14 * - *.ini.php (deprecated)
15 * - *.htconfig.php (deprecated)
17 class ConfigFileLoader extends ConfigFileManager
24 public function __construct($baseDir, App\Mode $mode)
26 parent::__construct($baseDir);
27 $this->appMode = $mode;
31 * Load the configuration files into an configuration cache
33 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
34 * expected local.config.php
36 * @param IConfigCache $config The config cache to load to
37 * @param bool $raw Setup the raw config format
41 public function setupCache(IConfigCache $config, $raw = false)
43 $config->load($this->loadCoreConfig('defaults'));
44 $config->load($this->loadCoreConfig('settings'));
46 $config->load($this->loadLegacyConfig('htpreconfig'), true);
47 $config->load($this->loadLegacyConfig('htconfig'), true);
49 $config->load($this->loadCoreConfig('local'), true);
51 // In case of install mode, add the found basepath (because there isn't a basepath set yet
52 if (!$raw && ($this->appMode->isInstall() || empty($config->get('system', 'basepath')))) {
53 // Setting at least the basepath we know
54 $config->set('system', 'basepath', $this->baseDir);
59 * Tries to load the specified core-configuration and returns the config array.
61 * @param string $name The name of the configuration (default is empty, which means 'local')
63 * @return array The config array (empty if no config found)
65 * @throws \Exception if the configuration file isn't readable
67 public function loadCoreConfig($name = '')
69 if (!empty($this->getConfigFullName($name))) {
70 return $this->loadConfigFile($this->getConfigFullName($name));
71 } elseif (!empty($this->getIniFullName($name))) {
72 return $this->loadINIConfigFile($this->getIniFullName($name));
79 * Tries to load the specified addon-configuration and returns the config array.
81 * @param string $name The name of the configuration
83 * @return array The config array (empty if no config found)
85 * @throws \Exception if the configuration file isn't readable
87 public function loadAddonConfig($name)
89 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
90 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
91 $name . DIRECTORY_SEPARATOR . // openstreetmap/
92 self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/
93 $name . ".config.php"; // openstreetmap.config.php
95 if (file_exists($filepath)) {
96 return $this->loadConfigFile($filepath);
103 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
105 * @param string $name The name of the config file (default is empty, which means .htconfig.php)
107 * @return array The configuration array (empty if no config found)
109 * @deprecated since version 2018.09
111 private function loadLegacyConfig($name = '')
114 if (!empty($this->getHtConfigFullName($name))) {
115 $a = new \stdClass();
117 include $this->getHtConfigFullName($name);
119 $htConfigCategories = array_keys($a->config);
121 // map the legacy configuration structure to the current structure
122 foreach ($htConfigCategories as $htConfigCategory) {
123 if (is_array($a->config[$htConfigCategory])) {
124 $keys = array_keys($a->config[$htConfigCategory]);
126 foreach ($keys as $key) {
127 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
130 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
136 if (isset($db_host)) {
137 $config['database']['hostname'] = $db_host;
140 if (isset($db_user)) {
141 $config['database']['username'] = $db_user;
144 if (isset($db_pass)) {
145 $config['database']['password'] = $db_pass;
148 if (isset($db_data)) {
149 $config['database']['database'] = $db_data;
152 if (isset($config['system']['db_charset'])) {
153 $config['database']['charset'] = $config['system']['db_charset'];
155 if (isset($pidfile)) {
156 $config['system']['pidfile'] = $pidfile;
159 if (isset($default_timezone)) {
160 $config['system']['default_timezone'] = $default_timezone;
161 unset($default_timezone);
164 $config['system']['language'] = $lang;
173 * Tries to load the specified legacy configuration file and returns the config array.
175 * @deprecated since version 2018.12
176 * @param string $filepath
178 * @return array The configuration array
181 private function loadINIConfigFile($filepath)
183 $contents = include($filepath);
185 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
187 if ($config === false) {
188 throw new \Exception('Error parsing INI config file ' . $filepath);
195 * Tries to load the specified configuration file and returns the config array.
197 * The config format is PHP array and the template for configuration files is the following:
205 * @param string $filepath The filepath of the
206 * @return array The config array0
208 * @throws \Exception if the config cannot get loaded.
210 private function loadConfigFile($filepath)
212 $config = include($filepath);
214 if (!is_array($config)) {
215 throw new \Exception('Error loading config file ' . $filepath);