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