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