2019-02-03 22:22:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Config;
|
|
|
|
|
2019-02-04 09:33:55 +01:00
|
|
|
use Friendica\Core\Addon;
|
|
|
|
|
2019-02-03 23:39:30 +01:00
|
|
|
/**
|
|
|
|
* The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
|
|
|
|
*
|
|
|
|
* It is capable of loading the following config files:
|
|
|
|
* - *.config.php (current)
|
|
|
|
* - *.ini.php (deprecated)
|
|
|
|
* - *.htconfig.php (deprecated)
|
|
|
|
*/
|
2019-02-03 22:22:04 +01:00
|
|
|
class ConfigCacheLoader
|
|
|
|
{
|
2019-02-03 23:39:30 +01:00
|
|
|
/**
|
|
|
|
* The Sub directory of the config-files
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-02-04 02:06:15 +01:00
|
|
|
const SUBDIRECTORY = '/config/';
|
2019-02-03 23:39:30 +01:00
|
|
|
|
2019-02-03 22:22:04 +01:00
|
|
|
private $baseDir;
|
|
|
|
private $configDir;
|
|
|
|
|
|
|
|
public function __construct($baseDir)
|
|
|
|
{
|
|
|
|
$this->baseDir = $baseDir;
|
2019-02-03 23:39:30 +01:00
|
|
|
$this->configDir = $baseDir . self::SUBDIRECTORY;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the configuration files
|
|
|
|
*
|
|
|
|
* First loads the default value for all the configuration keys, then the legacy configuration files, then the
|
|
|
|
* expected local.config.php
|
|
|
|
*/
|
|
|
|
public function loadConfigFiles(ConfigCache $config)
|
|
|
|
{
|
|
|
|
// Setting at least the basepath we know
|
|
|
|
$config->set('system', 'basepath', $this->baseDir);
|
|
|
|
|
2019-02-05 22:27:57 +01:00
|
|
|
$config->loadConfigArray($this->loadCoreConfig('defaults'));
|
|
|
|
$config->loadConfigArray($this->loadCoreConfig('settings'));
|
2019-02-03 22:22:04 +01:00
|
|
|
|
|
|
|
// Legacy .htconfig.php support
|
|
|
|
if (file_exists($this->baseDir . '/.htpreconfig.php')) {
|
|
|
|
$a = $config;
|
|
|
|
include $this->baseDir . '/.htpreconfig.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy .htconfig.php support
|
|
|
|
if (file_exists($this->baseDir . '/.htconfig.php')) {
|
|
|
|
$a = $config;
|
|
|
|
|
|
|
|
include $this->baseDir . '/.htconfig.php';
|
|
|
|
|
|
|
|
$config->set('database', 'hostname', $db_host);
|
|
|
|
$config->set('database', 'username', $db_user);
|
|
|
|
$config->set('database', 'password', $db_pass);
|
|
|
|
$config->set('database', 'database', $db_data);
|
|
|
|
$charset = $config->get('system', 'db_charset');
|
|
|
|
if (isset($charset)) {
|
|
|
|
$config->set('database', 'charset', $charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($db_host, $db_user, $db_pass, $db_data);
|
|
|
|
|
|
|
|
if (isset($default_timezone)) {
|
|
|
|
$config->set('system', 'default_timezone', $default_timezone);
|
|
|
|
unset($default_timezone);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($pidfile)) {
|
|
|
|
$config->set('system', 'pidfile', $pidfile);
|
|
|
|
unset($pidfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($lang)) {
|
|
|
|
$config->set('system', 'language', $lang);
|
|
|
|
unset($lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:27:57 +01:00
|
|
|
$config->loadConfigArray($this->loadCoreConfig('local'), true);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-05 22:27:57 +01:00
|
|
|
* Tries to load the specified legacy configuration file into the ConfigCache (@see ConfigCache ).
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* @deprecated since version 2018.12
|
|
|
|
* @param string $filename
|
|
|
|
*
|
|
|
|
* @return array The configuration
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function loadINIConfigFile($filename)
|
|
|
|
{
|
|
|
|
$filepath = $this->configDir . $filename . ".ini.php";
|
|
|
|
|
|
|
|
if (!file_exists($filepath)) {
|
|
|
|
throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
{
|
|
|
|
if (!file_exists($filepath)) {
|
|
|
|
throw new \Exception('Error loading non-existent config file ' . $filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = include($filepath);
|
|
|
|
|
|
|
|
if (!is_array($config)) {
|
|
|
|
throw new \Exception('Error loading config file ' . $filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-05 22:27:57 +01:00
|
|
|
* Tries to load the specified core-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 loadCoreConfig($name)
|
|
|
|
{
|
|
|
|
if (file_exists($this->configDir . $name . '.config.php')) {
|
|
|
|
return $this->loadConfigFile($this->configDir . $name . '.config.php');
|
|
|
|
} elseif (file_exists($this->configDir . $name . '.ini.php')) {
|
|
|
|
return $this->loadINIConfigFile($this->configDir . $name . '.ini.php');
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to load the specified addon-configuration and returns the config array.
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* First loads all activated addons default configuration through the load_config hook, then load the local.config.php
|
|
|
|
* again to overwrite potential local addon configuration.
|
|
|
|
*
|
2019-02-05 22:27:57 +01:00
|
|
|
* @param string $name The name of the configuration
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
2019-02-05 22:27:57 +01:00
|
|
|
* @return array The config array (empty if no config found)
|
|
|
|
*
|
|
|
|
* @throws \Exception if the configuration file isn't readable
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2019-02-05 22:27:57 +01:00
|
|
|
public function loadAddonConfig($name)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-02-05 22:27:57 +01:00
|
|
|
$filepath = $this->baseDir . Addon::DIRECTORY . $name . self::SUBDIRECTORY . $name . ".config.php";
|
|
|
|
|
|
|
|
if (file_exists($filepath)) {
|
|
|
|
return $this->loadConfigFile($filepath);
|
2019-02-03 22:22:04 +01:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|