]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ConfigCacheLoader.php
Bugfixing PConfig
[friendica.git] / src / Core / Config / ConfigCacheLoader.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 class ConfigCacheLoader
6 {
7         private $baseDir;
8         private $configDir;
9
10         public function __construct($baseDir)
11         {
12                 $this->baseDir = $baseDir;
13                 $this->configDir = $baseDir . '/config/';
14         }
15
16         /**
17          * Load the configuration files
18          *
19          * First loads the default value for all the configuration keys, then the legacy configuration files, then the
20          * expected local.config.php
21          */
22         public function loadConfigFiles(ConfigCache $config)
23         {
24                 // Setting at least the basepath we know
25                 $config->set('system', 'basepath', $this->baseDir);
26
27                 $config->loadConfigArray($this->loadConfigFile('defaults'));
28                 $config->loadConfigArray($this->loadConfigFile('settings'));
29
30                 // Legacy .htconfig.php support
31                 if (file_exists($this->baseDir  . '/.htpreconfig.php')) {
32                         $a = $config;
33                         include $this->baseDir . '/.htpreconfig.php';
34                 }
35
36                 // Legacy .htconfig.php support
37                 if (file_exists($this->baseDir . '/.htconfig.php')) {
38                         $a = $config;
39
40                         include $this->baseDir . '/.htconfig.php';
41
42                         $config->set('database', 'hostname', $db_host);
43                         $config->set('database', 'username', $db_user);
44                         $config->set('database', 'password', $db_pass);
45                         $config->set('database', 'database', $db_data);
46                         $charset = $config->get('system', 'db_charset');
47                         if (isset($charset)) {
48                                 $config->set('database', 'charset', $charset);
49                         }
50
51                         unset($db_host, $db_user, $db_pass, $db_data);
52
53                         if (isset($default_timezone)) {
54                                 $config->set('system', 'default_timezone', $default_timezone);
55                                 unset($default_timezone);
56                         }
57
58                         if (isset($pidfile)) {
59                                 $config->set('system', 'pidfile', $pidfile);
60                                 unset($pidfile);
61                         }
62
63                         if (isset($lang)) {
64                                 $config->set('system', 'language', $lang);
65                                 unset($lang);
66                         }
67                 }
68
69                 if (file_exists($this->baseDir . '/config/local.config.php')) {
70                         $config->loadConfigArray($this->loadConfigFile('local'), true);
71                 } elseif (file_exists($this->baseDir . '/config/local.ini.php')) {
72                         $config->loadConfigArray($this->loadINIConfigFile('local'), true);
73                 }
74         }
75
76         /**
77          * Tries to load the specified legacy configuration file into the App->config array.
78          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
79          *
80          * @deprecated since version 2018.12
81          * @param string $filename
82          *
83          * @return array The configuration
84          * @throws \Exception
85          */
86         public function loadINIConfigFile($filename)
87         {
88                 $filepath = $this->configDir . $filename . ".ini.php";
89
90                 if (!file_exists($filepath)) {
91                         throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
92                 }
93
94                 $contents = include($filepath);
95
96                 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
97
98                 if ($config === false) {
99                         throw new \Exception('Error parsing INI config file ' . $filepath);
100                 }
101
102                 return $config;
103         }
104
105         /**
106          * Tries to load the specified configuration file into the App->config array.
107          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
108          *
109          * The config format is PHP array and the template for configuration files is the following:
110          *
111          * <?php return [
112          *      'section' => [
113          *          'key' => 'value',
114          *      ],
115          * ];
116          *
117          * @param string $filename
118          * @return array The configuration
119          * @throws \Exception
120          */
121         public function loadConfigFile($filename)
122         {
123                 $filepath = $this->configDir . $filename . ".config.php";
124
125                 if (!file_exists($filepath)) {
126                         throw new \Exception('Error loading non-existent config file ' . $filepath);
127                 }
128
129                 $config = include($filepath);
130
131                 if (!is_array($config)) {
132                         throw new \Exception('Error loading config file ' . $filepath);
133                 }
134
135                 return $config;
136         }
137
138         /**
139          * Loads addons configuration files
140          *
141          * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
142          * again to overwrite potential local addon configuration.
143          *
144          * @return array The config array
145          *
146          * @throws \Exception
147          */
148         public function loadAddonConfig()
149         {
150                 // Load the local addon config file to overwritten default addon config values
151                 if (file_exists($this->configDir . 'addon.config.php')) {
152                         return $this->loadConfigFile('addon');
153                 } elseif (file_exists($this->configDir . 'addon.ini.php')) {
154                         return $this->loadINIConfigFile('addon');
155                 } else {
156                         return [];
157                 }
158         }
159 }