3 namespace Friendica\Core\Config;
5 use Friendica\Core\Addon;
8 * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
10 * It is capable of loading the following config files:
11 * - *.config.php (current)
12 * - *.ini.php (deprecated)
13 * - *.htconfig.php (deprecated)
15 class ConfigCacheLoader
18 * The Sub directory of the config-files
21 const SUBDIRECTORY = 'config';
26 public function __construct($baseDir)
28 $this->baseDir = $baseDir;
29 $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
33 * Load the configuration files
35 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
36 * expected local.config.php
38 public function loadConfigFiles(ConfigCache $config)
40 // Setting at least the basepath we know
41 $config->set('system', 'basepath', $this->baseDir);
43 $config->loadConfigArray($this->loadCoreConfig('defaults'));
44 $config->loadConfigArray($this->loadCoreConfig('settings'));
46 $config->loadConfigArray($this->loadLegacyConfig('htpreconfig'), true);
47 $config->loadConfigArray($this->loadLegacyConfig('htconfig'), true);
49 $config->loadConfigArray($this->loadCoreConfig('local'), true);
53 * Tries to load the specified core-configuration and returns the config array.
55 * @param string $name The name of the configuration
57 * @return array The config array (empty if no config found)
59 * @throws \Exception if the configuration file isn't readable
61 public function loadCoreConfig($name)
63 if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
64 return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
65 } elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
66 return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
73 * Tries to load the specified addon-configuration and returns the config array.
75 * @param string $name The name of the configuration
77 * @return array The config array (empty if no config found)
79 * @throws \Exception if the configuration file isn't readable
81 public function loadAddonConfig($name)
83 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
84 Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
85 $name . DIRECTORY_SEPARATOR . // openstreetmap/
86 self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/
87 $name . ".config.php"; // openstreetmap.config.php
89 if (file_exists($filepath)) {
90 return $this->loadConfigFile($filepath);
97 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
99 * @param string $name The name of the config file
101 * @return array The configuration array (empty if no config found)
103 * @deprecated since version 2018.09
105 private function loadLegacyConfig($name)
107 $filePath = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
109 if (file_exists($filePath)) {
110 $a = new \stdClass();
114 if (isset($db_host)) {
115 $a->config['database']['hostname'] = $db_host;
118 if (isset($db_user)) {
119 $a->config['database']['username'] = $db_user;
122 if (isset($db_pass)) {
123 $a->config['database']['password'] = $db_pass;
126 if (isset($db_data)) {
127 $a->config['database']['database'] = $db_data;
130 if (isset($a->config['system']['db_charset'])) {
131 $a->config['database']['charset'] = $a->config['system']['charset'];
133 if (isset($pidfile)) {
134 $a->config['system']['pidfile'] = $pidfile;
137 if (isset($default_timezone)) {
138 $a->config['system']['default_timezone'] = $default_timezone;
139 unset($default_timezone);
142 $a->config['system']['language'] = $lang;
153 * Tries to load the specified legacy configuration file and returns the config array.
155 * @deprecated since version 2018.12
156 * @param string $filepath
158 * @return array The configuration array
161 private function loadINIConfigFile($filepath)
163 if (!file_exists($filepath)) {
164 throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
167 $contents = include($filepath);
169 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
171 if ($config === false) {
172 throw new \Exception('Error parsing INI config file ' . $filepath);
179 * Tries to load the specified configuration file and returns the config array.
181 * The config format is PHP array and the template for configuration files is the following:
189 * @param string $filepath The filepath of the
190 * @return array The config array0
192 * @throws \Exception if the config cannot get loaded.
194 private function loadConfigFile($filepath)
196 if (!file_exists($filepath)) {
197 throw new \Exception('Error loading non-existent config file ' . $filepath);
200 $config = include($filepath);
202 if (!is_array($config)) {
203 throw new \Exception('Error loading config file ' . $filepath);