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 . 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->loadConfigFile('defaults'));
44 $config->loadConfigArray($this->loadConfigFile('settings'));
46 // Legacy .htconfig.php support
47 if (file_exists($this->baseDir . '/.htpreconfig.php')) {
49 include $this->baseDir . '/.htpreconfig.php';
52 // Legacy .htconfig.php support
53 if (file_exists($this->baseDir . '/.htconfig.php')) {
56 include $this->baseDir . '/.htconfig.php';
58 $config->set('database', 'hostname', $db_host);
59 $config->set('database', 'username', $db_user);
60 $config->set('database', 'password', $db_pass);
61 $config->set('database', 'database', $db_data);
62 $charset = $config->get('system', 'db_charset');
63 if (isset($charset)) {
64 $config->set('database', 'charset', $charset);
67 unset($db_host, $db_user, $db_pass, $db_data);
69 if (isset($default_timezone)) {
70 $config->set('system', 'default_timezone', $default_timezone);
71 unset($default_timezone);
74 if (isset($pidfile)) {
75 $config->set('system', 'pidfile', $pidfile);
80 $config->set('system', 'language', $lang);
85 if (file_exists($this->baseDir . '/config/local.config.php')) {
86 $config->loadConfigArray($this->loadConfigFile('local'), true);
87 } elseif (file_exists($this->baseDir . '/config/local.ini.php')) {
88 $config->loadConfigArray($this->loadINIConfigFile('local'), true);
93 * Tries to load the specified legacy configuration file into the App->config array.
94 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
96 * @deprecated since version 2018.12
97 * @param string $filename
99 * @return array The configuration
102 public function loadINIConfigFile($filename)
104 $filepath = $this->configDir . $filename . ".ini.php";
106 if (!file_exists($filepath)) {
107 throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
110 $contents = include($filepath);
112 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
114 if ($config === false) {
115 throw new \Exception('Error parsing INI config file ' . $filepath);
122 * Tries to load the specified configuration file into the App->config array.
123 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
125 * The config format is PHP array and the template for configuration files is the following:
133 * @param string $filename
134 * @param bool $addon True, if a config for an addon should be loaded
135 * @return array The configuration
138 public function loadConfigFile($filename, $addon = false)
141 $filepath = $this->baseDir . Addon::DIRECTORY . $filename . self::SUBDIRECTORY . $filename . ".config.php";
143 $filepath = $this->configDir . $filename . ".config.php";
146 if (!file_exists($filepath)) {
147 throw new \Exception('Error loading non-existent config file ' . $filepath);
150 $config = include($filepath);
152 if (!is_array($config)) {
153 throw new \Exception('Error loading config file ' . $filepath);
160 * Loads addons configuration files
162 * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
163 * again to overwrite potential local addon configuration.
165 * @return array The config array
169 public function loadAddonConfig()
171 // Load the local addon config file to overwritten default addon config values
172 if (file_exists($this->configDir . 'addon.config.php')) {
173 return $this->loadConfigFile('addon');
174 } elseif (file_exists($this->configDir . 'addon.ini.php')) {
175 return $this->loadINIConfigFile('addon');