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