- Adding additional legacy .htconfig information
[friendica.git/.git] / src / Core / Config / Cache / ConfigCacheLoader.php
1 <?php
2
3 namespace Friendica\Core\Config\Cache;
4
5 use Friendica\Core\Addon;
6
7 /**
8  * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
9  *
10  * It is capable of loading the following config files:
11  * - *.config.php   (current)
12  * - *.ini.php      (deprecated)
13  * - *.htconfig.php (deprecated)
14  */
15 class ConfigCacheLoader
16 {
17         /**
18          * The Sub directory of the config-files
19          * @var string
20          */
21         const SUBDIRECTORY = 'config';
22
23         private $baseDir;
24         private $configDir;
25
26         public function __construct($baseDir)
27         {
28                 $this->baseDir = $baseDir;
29                 $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
30         }
31
32         /**
33          * Load the configuration files
34          *
35          * First loads the default value for all the configuration keys, then the legacy configuration files, then the
36          * expected local.config.php
37          */
38         public function loadConfigFiles(ConfigCache $config)
39         {
40                 $config->load($this->loadCoreConfig('defaults'));
41                 $config->load($this->loadCoreConfig('settings'));
42
43                 $config->load($this->loadLegacyConfig('htpreconfig'), true);
44                 $config->load($this->loadLegacyConfig('htconfig'), true);
45
46                 $config->load($this->loadCoreConfig('local'), true);
47         }
48
49         /**
50          * Tries to load the specified core-configuration and returns the config array.
51          *
52          * @param string $name The name of the configuration
53          *
54          * @return array The config array (empty if no config found)
55          *
56          * @throws \Exception if the configuration file isn't readable
57          */
58         public function loadCoreConfig($name)
59         {
60                 if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
61                         return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
62                 } elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
63                         return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
64                 } else {
65                         return [];
66                 }
67         }
68
69         /**
70          * Tries to load the specified addon-configuration and returns the config array.
71          *
72          * @param string $name The name of the configuration
73          *
74          * @return array The config array (empty if no config found)
75          *
76          * @throws \Exception if the configuration file isn't readable
77          */
78         public function loadAddonConfig($name)
79         {
80                 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
81                         Addon::DIRECTORY       . DIRECTORY_SEPARATOR . // addon/
82                         $name                  . DIRECTORY_SEPARATOR . // openstreetmap/
83                         self::SUBDIRECTORY     . DIRECTORY_SEPARATOR . // config/
84                         $name . ".config.php";                         // openstreetmap.config.php
85
86                 if (file_exists($filepath)) {
87                         return $this->loadConfigFile($filepath);
88                 } else {
89                         return [];
90                 }
91         }
92
93         /**
94          * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
95          *
96          * @param string $name The name of the config file
97          *
98          * @return array The configuration array (empty if no config found)
99          *
100          * @deprecated since version 2018.09
101          */
102         private function loadLegacyConfig($name)
103         {
104                 $filePath = $this->baseDir  . DIRECTORY_SEPARATOR . '.' . $name . '.php';
105
106                 $config = [];
107
108                 if (file_exists($filePath)) {
109                         include $filePath;
110
111                         if (isset($db_host)) {
112                                 $config['database']['hostname'] = $db_host;
113                                 unset($db_host);
114                         }
115                         if (isset($db_user)) {
116                                 $config['database']['username'] = $db_user;
117                                 unset($db_user);
118                         }
119                         if (isset($db_pass)) {
120                                 $config['database']['password'] = $db_pass;
121                                 unset($db_pass);
122                         }
123                         if (isset($db_data)) {
124                                 $config['database']['database'] = $db_data;
125                                 unset($db_data);
126                         }
127                         if (isset($a->config['system']['db_charset'])) {
128                                 $a->config['database']['charset'] = $config['system']['charset'];
129                         }
130                         if (isset($pidfile)) {
131                                 $config['system']['pidfile'] = $pidfile;
132                                 unset($pidfile);
133                         }
134                         if (isset($default_timezone)) {
135                                 $config['system']['default_timezone'] = $default_timezone;
136                                 unset($default_timezone);
137                         }
138                         if (isset($lang)) {
139                                 $config['system']['language'] = $lang;
140                                 unset($lang);
141                         }
142                         if (isset($admin_email)) {
143                                 $config['config']['admin_email'] = $admin_email;
144                                 unset($admin_email);
145                         }
146                         if (isset($admin_nickname)) {
147                                 $config['config']['admin_nickname'] = $admin_nickname;
148                                 unset($admin_nickname);
149                         }
150                         if (isset($php_path)) {
151                                 $config['config']['php_path'] = $php_path;
152                                 unset($php_path);
153                         }
154                         if (isset($max_import_size)) {
155                                 $config['config']['max_import_size'] = $max_import_size;
156                                 unset($max_import_size);
157                         }
158                 }
159
160                 return $config;
161         }
162
163         /**
164          * Tries to load the specified legacy configuration file and returns the config array.
165          *
166          * @deprecated since version 2018.12
167          * @param string $filepath
168          *
169          * @return array The configuration array
170          * @throws \Exception
171          */
172         private function loadINIConfigFile($filepath)
173         {
174                 $contents = include($filepath);
175
176                 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
177
178                 if ($config === false) {
179                         throw new \Exception('Error parsing INI config file ' . $filepath);
180                 }
181
182                 return $config;
183         }
184
185         /**
186          * Tries to load the specified configuration file and returns the config array.
187          *
188          * The config format is PHP array and the template for configuration files is the following:
189          *
190          * <?php return [
191          *      'section' => [
192          *          'key' => 'value',
193          *      ],
194          * ];
195          *
196          * @param  string $filepath The filepath of the
197          * @return array The config array0
198          *
199          * @throws \Exception if the config cannot get loaded.
200          */
201         private function loadConfigFile($filepath)
202         {
203                 $config = include($filepath);
204
205                 if (!is_array($config)) {
206                         throw new \Exception('Error loading config file ' . $filepath);
207                 }
208
209                 return $config;
210         }
211 }