3 namespace Friendica\Util\Config;
6 use Friendica\Core\Addon;
7 use Friendica\Core\Config\Cache\IConfigCache;
10 * The ConfigCacheLoader 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 ConfigCacheLoader extends ConfigCacheManager
24 public function __construct($baseDir, App\Mode $mode)
26 parent::__construct($baseDir);
27 $this->appMode = $mode;
31 * Load the configuration files
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 The config cache to load to
40 public function loadConfigFiles(IConfigCache $config)
42 $config->load($this->loadCoreConfig('defaults'));
43 $config->load($this->loadCoreConfig('settings'));
45 $config->load($this->loadLegacyConfig('htpreconfig'), true);
46 $config->load($this->loadLegacyConfig('htconfig'), true);
48 $config->load($this->loadCoreConfig('local'), true);
50 // In case of install mode, add the found basepath (because there isn't a basepath set yet
51 if ($this->appMode->isInstall()) {
52 // Setting at least the basepath we know
53 $config->set('system', 'basepath', $this->baseDir);
58 * Tries to load the specified core-configuration and returns the config array.
60 * @param string $name The name of the configuration
62 * @return array The config array (empty if no config found)
64 * @throws \Exception if the configuration file isn't readable
66 public function loadCoreConfig($name)
68 if (!empty($this->getConfigFullName($name))) {
69 return $this->loadConfigFile($this->getConfigFullName($name));
70 } elseif (!empty($this->getIniFullName($name))) {
71 return $this->loadINIConfigFile($this->getIniFullName($name));
78 * Tries to load the specified addon-configuration and returns the config array.
80 * @param string $name The name of the configuration
82 * @return array The config array (empty if no config found)
84 * @throws \Exception if the configuration file isn't readable
86 public function loadAddonConfig($name)
88 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
89 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
90 $name . DIRECTORY_SEPARATOR . // openstreetmap/
91 self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/
92 $name . ".config.php"; // openstreetmap.config.php
94 if (file_exists($filepath)) {
95 return $this->loadConfigFile($filepath);
102 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
104 * @param string $name The name of the config file
106 * @return array The configuration array (empty if no config found)
108 * @deprecated since version 2018.09
110 private function loadLegacyConfig($name)
113 if (!empty($this->getHtConfigFullName($name))) {
114 $a = new \stdClass();
116 include $this->getHtConfigFullName($name);
118 $htConfigCategories = array_keys($a->config);
120 // map the legacy configuration structure to the current structure
121 foreach ($htConfigCategories as $htConfigCategory) {
122 if (is_array($a->config[$htConfigCategory])) {
123 $keys = array_keys($a->config[$htConfigCategory]);
125 foreach ($keys as $key) {
126 $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
129 $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
135 if (isset($db_host)) {
136 $config['database']['hostname'] = $db_host;
139 if (isset($db_user)) {
140 $config['database']['username'] = $db_user;
143 if (isset($db_pass)) {
144 $config['database']['password'] = $db_pass;
147 if (isset($db_data)) {
148 $config['database']['database'] = $db_data;
151 if (isset($config['system']['db_charset'])) {
152 $config['database']['charset'] = $config['system']['db_charset'];
154 if (isset($pidfile)) {
155 $config['system']['pidfile'] = $pidfile;
158 if (isset($default_timezone)) {
159 $config['system']['default_timezone'] = $default_timezone;
160 unset($default_timezone);
163 $config['system']['language'] = $lang;
172 * Tries to load the specified legacy configuration file and returns the config array.
174 * @deprecated since version 2018.12
175 * @param string $filepath
177 * @return array The configuration array
180 private function loadINIConfigFile($filepath)
182 $contents = include($filepath);
184 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
186 if ($config === false) {
187 throw new \Exception('Error parsing INI config file ' . $filepath);
194 * Tries to load the specified configuration file and returns the config array.
196 * The config format is PHP array and the template for configuration files is the following:
204 * @param string $filepath The filepath of the
205 * @return array The config array0
207 * @throws \Exception if the config cannot get loaded.
209 private function loadConfigFile($filepath)
211 $config = include($filepath);
213 if (!is_array($config)) {
214 throw new \Exception('Error loading config file ' . $filepath);