3 namespace Friendica\Core\Config;
6 * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
8 * It is capable of loading the following config files:
9 * - *.config.php (current)
10 * - *.ini.php (deprecated)
11 * - *.htconfig.php (deprecated)
13 class ConfigCacheLoader
16 * The Sub directory of the config-files
19 const SUBDIRECTORY = '/config/';
24 public function __construct($baseDir)
26 $this->baseDir = $baseDir;
27 $this->configDir = $baseDir . self::SUBDIRECTORY;
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 public function loadConfigFiles(ConfigCache $config)
38 // Setting at least the basepath we know
39 $config->set('system', 'basepath', $this->baseDir);
41 $config->loadConfigArray($this->loadConfigFile('defaults'));
42 $config->loadConfigArray($this->loadConfigFile('settings'));
44 // Legacy .htconfig.php support
45 if (file_exists($this->baseDir . '/.htpreconfig.php')) {
47 include $this->baseDir . '/.htpreconfig.php';
50 // Legacy .htconfig.php support
51 if (file_exists($this->baseDir . '/.htconfig.php')) {
54 include $this->baseDir . '/.htconfig.php';
56 $config->set('database', 'hostname', $db_host);
57 $config->set('database', 'username', $db_user);
58 $config->set('database', 'password', $db_pass);
59 $config->set('database', 'database', $db_data);
60 $charset = $config->get('system', 'db_charset');
61 if (isset($charset)) {
62 $config->set('database', 'charset', $charset);
65 unset($db_host, $db_user, $db_pass, $db_data);
67 if (isset($default_timezone)) {
68 $config->set('system', 'default_timezone', $default_timezone);
69 unset($default_timezone);
72 if (isset($pidfile)) {
73 $config->set('system', 'pidfile', $pidfile);
78 $config->set('system', 'language', $lang);
83 if (file_exists($this->baseDir . '/config/local.config.php')) {
84 $config->loadConfigArray($this->loadConfigFile('local'), true);
85 } elseif (file_exists($this->baseDir . '/config/local.ini.php')) {
86 $config->loadConfigArray($this->loadINIConfigFile('local'), true);
91 * Tries to load the specified legacy configuration file into the App->config array.
92 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
94 * @deprecated since version 2018.12
95 * @param string $filename
97 * @return array The configuration
100 public function loadINIConfigFile($filename)
102 $filepath = $this->configDir . $filename . ".ini.php";
104 if (!file_exists($filepath)) {
105 throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
108 $contents = include($filepath);
110 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
112 if ($config === false) {
113 throw new \Exception('Error parsing INI config file ' . $filepath);
120 * Tries to load the specified configuration file into the App->config array.
121 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
123 * The config format is PHP array and the template for configuration files is the following:
131 * @param string $filename
132 * @return array The configuration
135 public function loadConfigFile($filename)
137 $filepath = $this->configDir . $filename . ".config.php";
139 if (!file_exists($filepath)) {
140 throw new \Exception('Error loading non-existent config file ' . $filepath);
143 $config = include($filepath);
145 if (!is_array($config)) {
146 throw new \Exception('Error loading config file ' . $filepath);
153 * Loads addons configuration files
155 * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
156 * again to overwrite potential local addon configuration.
158 * @return array The config array
162 public function loadAddonConfig()
164 // Load the local addon config file to overwritten default addon config values
165 if (file_exists($this->configDir . 'addon.config.php')) {
166 return $this->loadConfigFile('addon');
167 } elseif (file_exists($this->configDir . 'addon.ini.php')) {
168 return $this->loadINIConfigFile('addon');