2019-02-03 22:22:04 +01:00
|
|
|
<?php
|
|
|
|
|
2019-03-23 15:37:05 +01:00
|
|
|
namespace Friendica\Util\Config;
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-03-14 02:36:49 +01:00
|
|
|
use Friendica\App;
|
2019-02-04 09:33:55 +01:00
|
|
|
use Friendica\Core\Addon;
|
2019-03-23 15:37:05 +01:00
|
|
|
use Friendica\Core\Config\Cache\IConfigCache;
|
2019-02-04 09:33:55 +01:00
|
|
|
|
2019-02-03 23:39:30 +01:00
|
|
|
/**
|
2019-03-24 12:54:26 +01:00
|
|
|
* The ConfigFileLoader loads config-files and stores them in a IConfigCache ( @see IConfigCache )
|
2019-02-03 23:39:30 +01:00
|
|
|
*
|
|
|
|
* It is capable of loading the following config files:
|
|
|
|
* - *.config.php (current)
|
|
|
|
* - *.ini.php (deprecated)
|
|
|
|
* - *.htconfig.php (deprecated)
|
|
|
|
*/
|
2019-03-24 12:54:26 +01:00
|
|
|
class ConfigFileLoader extends ConfigFileManager
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-03-14 02:36:49 +01:00
|
|
|
/**
|
|
|
|
* @var App\Mode
|
|
|
|
*/
|
|
|
|
private $appMode;
|
|
|
|
|
|
|
|
public function __construct($baseDir, App\Mode $mode)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-03-23 15:37:05 +01:00
|
|
|
parent::__construct($baseDir);
|
2019-03-14 02:36:49 +01:00
|
|
|
$this->appMode = $mode;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-24 12:54:26 +01:00
|
|
|
* Load the configuration files into an configuration cache
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* First loads the default value for all the configuration keys, then the legacy configuration files, then the
|
|
|
|
* expected local.config.php
|
2019-03-14 02:36:49 +01:00
|
|
|
*
|
2019-03-24 22:51:30 +01:00
|
|
|
* @param IConfigCache $config The config cache to load to
|
2019-03-14 02:36:49 +01:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2019-03-24 12:54:26 +01:00
|
|
|
public function setupCache(IConfigCache $config)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-02-10 19:52:21 +01:00
|
|
|
$config->load($this->loadCoreConfig('defaults'));
|
|
|
|
$config->load($this->loadCoreConfig('settings'));
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-02-10 19:52:21 +01:00
|
|
|
$config->load($this->loadLegacyConfig('htpreconfig'), true);
|
|
|
|
$config->load($this->loadLegacyConfig('htconfig'), true);
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-02-10 19:52:21 +01:00
|
|
|
$config->load($this->loadCoreConfig('local'), true);
|
2019-03-14 02:36:49 +01:00
|
|
|
|
|
|
|
// In case of install mode, add the found basepath (because there isn't a basepath set yet
|
|
|
|
if ($this->appMode->isInstall()) {
|
|
|
|
// Setting at least the basepath we know
|
|
|
|
$config->set('system', 'basepath', $this->baseDir);
|
|
|
|
}
|
2019-02-05 23:36:01 +01:00
|
|
|
}
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-02-05 23:36:01 +01:00
|
|
|
/**
|
2019-02-06 00:02:25 +01:00
|
|
|
* Tries to load the specified core-configuration and returns the config array.
|
|
|
|
*
|
2019-03-23 15:40:09 +01:00
|
|
|
* @param string $name The name of the configuration (default is empty, which means 'local')
|
2019-02-06 00:02:25 +01:00
|
|
|
*
|
|
|
|
* @return array The config array (empty if no config found)
|
|
|
|
*
|
|
|
|
* @throws \Exception if the configuration file isn't readable
|
|
|
|
*/
|
2019-03-23 15:40:09 +01:00
|
|
|
public function loadCoreConfig($name = '')
|
2019-02-06 00:02:25 +01:00
|
|
|
{
|
2019-03-23 15:37:05 +01:00
|
|
|
if (!empty($this->getConfigFullName($name))) {
|
|
|
|
return $this->loadConfigFile($this->getConfigFullName($name));
|
|
|
|
} elseif (!empty($this->getIniFullName($name))) {
|
|
|
|
return $this->loadINIConfigFile($this->getIniFullName($name));
|
2019-02-06 00:02:25 +01:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to load the specified addon-configuration and returns the config array.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the configuration
|
|
|
|
*
|
|
|
|
* @return array The config array (empty if no config found)
|
|
|
|
*
|
|
|
|
* @throws \Exception if the configuration file isn't readable
|
|
|
|
*/
|
|
|
|
public function loadAddonConfig($name)
|
|
|
|
{
|
|
|
|
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
|
|
|
|
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
|
|
|
|
$name . DIRECTORY_SEPARATOR . // openstreetmap/
|
|
|
|
self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/
|
|
|
|
$name . ".config.php"; // openstreetmap.config.php
|
|
|
|
|
|
|
|
if (file_exists($filepath)) {
|
|
|
|
return $this->loadConfigFile($filepath);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
|
2019-02-05 23:36:01 +01:00
|
|
|
*
|
2019-03-23 15:40:09 +01:00
|
|
|
* @param string $name The name of the config file (default is empty, which means .htconfig.php)
|
2019-02-06 00:02:25 +01:00
|
|
|
*
|
|
|
|
* @return array The configuration array (empty if no config found)
|
2019-02-05 23:36:01 +01:00
|
|
|
*
|
|
|
|
* @deprecated since version 2018.09
|
|
|
|
*/
|
2019-03-23 15:40:09 +01:00
|
|
|
private function loadLegacyConfig($name = '')
|
2019-02-05 23:36:01 +01:00
|
|
|
{
|
2019-03-16 20:04:11 +01:00
|
|
|
$config = [];
|
2019-03-23 15:37:05 +01:00
|
|
|
if (!empty($this->getHtConfigFullName($name))) {
|
2019-03-16 20:31:10 +01:00
|
|
|
$a = new \stdClass();
|
|
|
|
$a->config = [];
|
2019-03-23 15:37:05 +01:00
|
|
|
include $this->getHtConfigFullName($name);
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-03-17 09:57:34 +01:00
|
|
|
$htConfigCategories = array_keys($a->config);
|
2019-03-16 20:31:10 +01:00
|
|
|
|
|
|
|
// map the legacy configuration structure to the current structure
|
2019-03-17 09:57:34 +01:00
|
|
|
foreach ($htConfigCategories as $htConfigCategory) {
|
|
|
|
if (is_array($a->config[$htConfigCategory])) {
|
|
|
|
$keys = array_keys($a->config[$htConfigCategory]);
|
2019-03-16 20:31:10 +01:00
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
2019-03-17 09:57:34 +01:00
|
|
|
$config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
|
2019-03-16 20:31:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-17 09:57:34 +01:00
|
|
|
$config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
|
2019-03-16 20:31:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($a);
|
|
|
|
|
2019-02-05 23:36:01 +01:00
|
|
|
if (isset($db_host)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['database']['hostname'] = $db_host;
|
2019-02-05 23:36:01 +01:00
|
|
|
unset($db_host);
|
|
|
|
}
|
|
|
|
if (isset($db_user)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['database']['username'] = $db_user;
|
2019-02-05 23:36:01 +01:00
|
|
|
unset($db_user);
|
|
|
|
}
|
|
|
|
if (isset($db_pass)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['database']['password'] = $db_pass;
|
2019-02-05 23:36:01 +01:00
|
|
|
unset($db_pass);
|
|
|
|
}
|
|
|
|
if (isset($db_data)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['database']['database'] = $db_data;
|
2019-02-05 23:36:01 +01:00
|
|
|
unset($db_data);
|
|
|
|
}
|
2019-03-16 20:31:10 +01:00
|
|
|
if (isset($config['system']['db_charset'])) {
|
|
|
|
$config['database']['charset'] = $config['system']['db_charset'];
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
if (isset($pidfile)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['system']['pidfile'] = $pidfile;
|
2019-02-03 22:22:04 +01:00
|
|
|
unset($pidfile);
|
|
|
|
}
|
2019-02-05 23:36:01 +01:00
|
|
|
if (isset($default_timezone)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['system']['default_timezone'] = $default_timezone;
|
2019-02-05 23:36:01 +01:00
|
|
|
unset($default_timezone);
|
|
|
|
}
|
2019-02-03 22:22:04 +01:00
|
|
|
if (isset($lang)) {
|
2019-03-16 20:04:11 +01:00
|
|
|
$config['system']['language'] = $lang;
|
2019-02-03 22:22:04 +01:00
|
|
|
unset($lang);
|
|
|
|
}
|
2019-02-05 23:36:01 +01:00
|
|
|
}
|
2019-03-16 20:04:11 +01:00
|
|
|
|
|
|
|
return $config;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-05 23:36:01 +01:00
|
|
|
* Tries to load the specified legacy configuration file and returns the config array.
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* @deprecated since version 2018.12
|
2019-02-05 23:47:40 +01:00
|
|
|
* @param string $filepath
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
2019-02-05 23:36:01 +01:00
|
|
|
* @return array The configuration array
|
2019-02-03 22:22:04 +01:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2019-02-06 00:02:25 +01:00
|
|
|
private function loadINIConfigFile($filepath)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
|
|
|
$contents = include($filepath);
|
|
|
|
|
|
|
|
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
|
|
|
|
|
|
|
|
if ($config === false) {
|
|
|
|
throw new \Exception('Error parsing INI config file ' . $filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-05 22:27:57 +01:00
|
|
|
* Tries to load the specified configuration file and returns the config array.
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* The config format is PHP array and the template for configuration files is the following:
|
|
|
|
*
|
|
|
|
* <?php return [
|
|
|
|
* 'section' => [
|
|
|
|
* 'key' => 'value',
|
|
|
|
* ],
|
|
|
|
* ];
|
|
|
|
*
|
2019-02-05 22:27:57 +01:00
|
|
|
* @param string $filepath The filepath of the
|
|
|
|
* @return array The config array0
|
|
|
|
*
|
|
|
|
* @throws \Exception if the config cannot get loaded.
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2019-02-05 22:27:57 +01:00
|
|
|
private function loadConfigFile($filepath)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
|
|
|
$config = include($filepath);
|
|
|
|
|
|
|
|
if (!is_array($config)) {
|
|
|
|
throw new \Exception('Error loading config file ' . $filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
}
|