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