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/';
21 * The addon sub-directory
24 const ADDONDIRECTORY = '/addon/';
29 public function __construct($baseDir)
31 $this->baseDir = $baseDir;
32 $this->configDir = $baseDir . self::SUBDIRECTORY;
36 * Load the configuration files
38 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
39 * expected local.config.php
41 public function loadConfigFiles(ConfigCache $config)
43 // Setting at least the basepath we know
44 $config->set('system', 'basepath', $this->baseDir);
46 $config->loadConfigArray($this->loadConfigFile('defaults'));
47 $config->loadConfigArray($this->loadConfigFile('settings'));
49 // Legacy .htconfig.php support
50 if (file_exists($this->baseDir . '/.htpreconfig.php')) {
52 include $this->baseDir . '/.htpreconfig.php';
55 // Legacy .htconfig.php support
56 if (file_exists($this->baseDir . '/.htconfig.php')) {
59 include $this->baseDir . '/.htconfig.php';
61 $config->set('database', 'hostname', $db_host);
62 $config->set('database', 'username', $db_user);
63 $config->set('database', 'password', $db_pass);
64 $config->set('database', 'database', $db_data);
65 $charset = $config->get('system', 'db_charset');
66 if (isset($charset)) {
67 $config->set('database', 'charset', $charset);
70 unset($db_host, $db_user, $db_pass, $db_data);
72 if (isset($default_timezone)) {
73 $config->set('system', 'default_timezone', $default_timezone);
74 unset($default_timezone);
77 if (isset($pidfile)) {
78 $config->set('system', 'pidfile', $pidfile);
83 $config->set('system', 'language', $lang);
88 if (file_exists($this->baseDir . '/config/local.config.php')) {
89 $config->loadConfigArray($this->loadConfigFile('local'), true);
90 } elseif (file_exists($this->baseDir . '/config/local.ini.php')) {
91 $config->loadConfigArray($this->loadINIConfigFile('local'), true);
96 * Tries to load the specified legacy configuration file into the App->config array.
97 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
99 * @deprecated since version 2018.12
100 * @param string $filename
102 * @return array The configuration
105 public function loadINIConfigFile($filename)
107 $filepath = $this->configDir . $filename . ".ini.php";
109 if (!file_exists($filepath)) {
110 throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
113 $contents = include($filepath);
115 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
117 if ($config === false) {
118 throw new \Exception('Error parsing INI config file ' . $filepath);
125 * Tries to load the specified configuration file into the App->config array.
126 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
128 * The config format is PHP array and the template for configuration files is the following:
136 * @param string $filename
137 * @param bool $addon True, if a config for an addon should be loaded
138 * @return array The configuration
141 public function loadConfigFile($filename, $addon = false)
144 $filepath = $this->baseDir . self::ADDONDIRECTORY . $filename . self::SUBDIRECTORY . $filename . ".config.php";
146 $filepath = $this->configDir . $filename . ".config.php";
149 if (!file_exists($filepath)) {
150 throw new \Exception('Error loading non-existent config file ' . $filepath);
153 $config = include($filepath);
155 if (!is_array($config)) {
156 throw new \Exception('Error loading config file ' . $filepath);
163 * Loads addons configuration files
165 * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
166 * again to overwrite potential local addon configuration.
168 * @return array The config array
172 public function loadAddonConfig()
174 // Load the local addon config file to overwritten default addon config values
175 if (file_exists($this->configDir . 'addon.config.php')) {
176 return $this->loadConfigFile('addon');
177 } elseif (file_exists($this->configDir . 'addon.ini.php')) {
178 return $this->loadINIConfigFile('addon');