]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ConfigCacheLoader.php
Bugfixing Addon-config
[friendica.git] / src / Core / Config / ConfigCacheLoader.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 /**
6  * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
7  *
8  * It is capable of loading the following config files:
9  * - *.config.php   (current)
10  * - *.ini.php      (deprecated)
11  * - *.htconfig.php (deprecated)
12  */
13 class ConfigCacheLoader
14 {
15         /**
16          * The Sub directory of the config-files
17          * @var string
18          */
19         const SUBDIRECTORY   = '/config/';
20         /**
21          * The addon sub-directory
22          * @var string
23          */
24         const ADDONDIRECTORY = '/addon/';
25
26         private $baseDir;
27         private $configDir;
28
29         public function __construct($baseDir)
30         {
31                 $this->baseDir = $baseDir;
32                 $this->configDir = $baseDir . self::SUBDIRECTORY;
33         }
34
35         /**
36          * Load the configuration files
37          *
38          * First loads the default value for all the configuration keys, then the legacy configuration files, then the
39          * expected local.config.php
40          */
41         public function loadConfigFiles(ConfigCache $config)
42         {
43                 // Setting at least the basepath we know
44                 $config->set('system', 'basepath', $this->baseDir);
45
46                 $config->loadConfigArray($this->loadConfigFile('defaults'));
47                 $config->loadConfigArray($this->loadConfigFile('settings'));
48
49                 // Legacy .htconfig.php support
50                 if (file_exists($this->baseDir  . '/.htpreconfig.php')) {
51                         $a = $config;
52                         include $this->baseDir . '/.htpreconfig.php';
53                 }
54
55                 // Legacy .htconfig.php support
56                 if (file_exists($this->baseDir . '/.htconfig.php')) {
57                         $a = $config;
58
59                         include $this->baseDir . '/.htconfig.php';
60
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);
68                         }
69
70                         unset($db_host, $db_user, $db_pass, $db_data);
71
72                         if (isset($default_timezone)) {
73                                 $config->set('system', 'default_timezone', $default_timezone);
74                                 unset($default_timezone);
75                         }
76
77                         if (isset($pidfile)) {
78                                 $config->set('system', 'pidfile', $pidfile);
79                                 unset($pidfile);
80                         }
81
82                         if (isset($lang)) {
83                                 $config->set('system', 'language', $lang);
84                                 unset($lang);
85                         }
86                 }
87
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);
92                 }
93         }
94
95         /**
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.
98          *
99          * @deprecated since version 2018.12
100          * @param string $filename
101          *
102          * @return array The configuration
103          * @throws \Exception
104          */
105         public function loadINIConfigFile($filename)
106         {
107                 $filepath = $this->configDir . $filename . ".ini.php";
108
109                 if (!file_exists($filepath)) {
110                         throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
111                 }
112
113                 $contents = include($filepath);
114
115                 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
116
117                 if ($config === false) {
118                         throw new \Exception('Error parsing INI config file ' . $filepath);
119                 }
120
121                 return $config;
122         }
123
124         /**
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.
127          *
128          * The config format is PHP array and the template for configuration files is the following:
129          *
130          * <?php return [
131          *      'section' => [
132          *          'key' => 'value',
133          *      ],
134          * ];
135          *
136          * @param string $filename
137          * @param bool   $addon     True, if a config for an addon should be loaded
138          * @return array The configuration
139          * @throws \Exception
140          */
141         public function loadConfigFile($filename, $addon = false)
142         {
143                 if ($addon) {
144                         $filepath = $this->baseDir . self::ADDONDIRECTORY . self::SUBDIRECTORY . $filename . ".config.php";
145                 } else {
146                         $filepath = $this->configDir . $filename . ".config.php";
147                 }
148
149                 if (!file_exists($filepath)) {
150                         throw new \Exception('Error loading non-existent config file ' . $filepath);
151                 }
152
153                 $config = include($filepath);
154
155                 if (!is_array($config)) {
156                         throw new \Exception('Error loading config file ' . $filepath);
157                 }
158
159                 return $config;
160         }
161
162         /**
163          * Loads addons configuration files
164          *
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.
167          *
168          * @return array The config array
169          *
170          * @throws \Exception
171          */
172         public function loadAddonConfig()
173         {
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');
179                 } else {
180                         return [];
181                 }
182         }
183 }