2019-02-03 22:22:04 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-06-23 19:56:21 +02:00
|
|
|
namespace Friendica\Util;
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-06-23 19:56:21 +02:00
|
|
|
use Exception;
|
2019-02-04 09:33:55 +01:00
|
|
|
use Friendica\Core\Addon;
|
2020-01-19 22:23:44 +01:00
|
|
|
use Friendica\Core\Config\Cache;
|
2019-02-04 09:33:55 +01:00
|
|
|
|
2019-02-03 23:39:30 +01:00
|
|
|
/**
|
2020-01-19 22:23:44 +01:00
|
|
|
* The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see Cache )
|
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-06-23 19:56:21 +02:00
|
|
|
class ConfigFileLoader
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-06-23 19:56:21 +02:00
|
|
|
/**
|
|
|
|
* The Sub directory of the config-files
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const CONFIG_DIR = 'config';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sub directory of the static config-files
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const STATIC_DIR = 'static';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default name of the user defined ini file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const CONFIG_INI = 'local';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default name of the user defined legacy config file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const CONFIG_HTCONFIG = 'htconfig';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sample string inside the configs, which shouldn't get loaded
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const SAMPLE_END = '-sample';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $baseDir;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $configDir;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $staticDir;
|
2019-03-14 02:36:49 +01:00
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
public function __construct(string $basePath)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-07-21 01:22:10 +02:00
|
|
|
$this->baseDir = $basePath;
|
|
|
|
$this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
|
|
|
|
$this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
|
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
|
|
|
*
|
2020-01-19 22:23:44 +01:00
|
|
|
* @param Cache $config The config cache to load to
|
2020-10-06 20:06:52 +02:00
|
|
|
* @param array $server The $_SERVER array
|
2020-01-19 22:23:44 +01:00
|
|
|
* @param bool $raw Setup the raw config format
|
2019-03-14 02:36:49 +01:00
|
|
|
*
|
2019-06-23 19:56:21 +02:00
|
|
|
* @throws Exception
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2020-10-06 20:06:52 +02:00
|
|
|
public function setupCache(Cache $config, array $server = [], $raw = false)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-06-23 19:56:21 +02:00
|
|
|
// Load static config files first, the order is important
|
2020-10-04 20:37:35 +02:00
|
|
|
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
|
|
|
|
$config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-06-23 19:56:21 +02:00
|
|
|
// try to load the legacy config first
|
2020-10-04 20:37:35 +02:00
|
|
|
$config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
|
|
|
|
$config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2019-06-23 19:56:21 +02:00
|
|
|
// Now load every other config you find inside the 'config/' directory
|
|
|
|
$this->loadCoreConfig($config);
|
2019-03-14 02:36:49 +01:00
|
|
|
|
2020-10-06 20:06:52 +02:00
|
|
|
$config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
|
|
|
|
|
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
|
2019-07-21 01:22:10 +02:00
|
|
|
if (!$raw && empty($config->get('system', 'basepath'))) {
|
2019-03-14 02:36:49 +01:00
|
|
|
// Setting at least the basepath we know
|
2020-10-06 20:06:52 +02:00
|
|
|
$config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
|
2019-03-14 02:36:49 +01:00
|
|
|
}
|
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-06-23 19:56:21 +02:00
|
|
|
* Tries to load the static core-configuration and returns the config array.
|
2019-02-06 00:02:25 +01:00
|
|
|
*
|
2019-06-23 19:56:21 +02:00
|
|
|
* @param string $name The name of the configuration
|
2019-02-06 00:02:25 +01:00
|
|
|
*
|
|
|
|
* @return array The config array (empty if no config found)
|
|
|
|
*
|
2019-06-23 19:56:21 +02:00
|
|
|
* @throws Exception if the configuration file isn't readable
|
2019-02-06 00:02:25 +01:00
|
|
|
*/
|
2019-06-23 19:56:21 +02:00
|
|
|
private function loadStaticConfig($name)
|
2019-02-06 00:02:25 +01:00
|
|
|
{
|
2019-06-23 19:56:21 +02:00
|
|
|
$configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
|
|
|
|
$iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
|
|
|
|
|
|
|
|
if (file_exists($configName)) {
|
|
|
|
return $this->loadConfigFile($configName);
|
|
|
|
} elseif (file_exists($iniName)) {
|
|
|
|
return $this->loadINIConfigFile($iniName);
|
2019-02-06 00:02:25 +01:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 19:56:21 +02:00
|
|
|
/**
|
|
|
|
* Tries to load the specified core-configuration into the config cache.
|
|
|
|
*
|
2020-01-19 22:23:44 +01:00
|
|
|
* @param Cache $config The Config cache
|
2019-06-23 19:56:21 +02:00
|
|
|
*
|
|
|
|
* @return array The config array (empty if no config found)
|
|
|
|
*
|
|
|
|
* @throws Exception if the configuration file isn't readable
|
|
|
|
*/
|
2020-01-19 22:23:44 +01:00
|
|
|
private function loadCoreConfig(Cache $config)
|
2019-06-23 19:56:21 +02:00
|
|
|
{
|
|
|
|
// try to load legacy ini-files first
|
|
|
|
foreach ($this->getConfigFiles(true) as $configFile) {
|
2020-10-04 20:37:35 +02:00
|
|
|
$config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
|
2019-06-23 19:56:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to load supported config at last to overwrite it
|
|
|
|
foreach ($this->getConfigFiles() as $configFile) {
|
2020-10-04 20:37:35 +02:00
|
|
|
$config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
|
2019-06-23 19:56:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:02:25 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*
|
2019-06-23 19:56:21 +02:00
|
|
|
* @throws Exception if the configuration file isn't readable
|
2019-02-06 00:02:25 +01:00
|
|
|
*/
|
|
|
|
public function loadAddonConfig($name)
|
|
|
|
{
|
2019-07-16 21:53:48 +02:00
|
|
|
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
|
|
|
|
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
|
|
|
|
$name . DIRECTORY_SEPARATOR . // openstreetmap/
|
|
|
|
self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
|
|
|
|
$name . ".config.php"; // openstreetmap.config.php
|
2019-02-06 00:02:25 +01:00
|
|
|
|
|
|
|
if (file_exists($filepath)) {
|
|
|
|
return $this->loadConfigFile($filepath);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:06:52 +02:00
|
|
|
/**
|
|
|
|
* Tries to load environment specific variables, based on the `env.config.php` mapping table
|
|
|
|
*
|
|
|
|
* @param array $server The $_SERVER variable
|
|
|
|
*
|
|
|
|
* @return array The config array (empty if no config was found)
|
|
|
|
*
|
|
|
|
* @throws Exception if the configuration file isn't readable
|
|
|
|
*/
|
|
|
|
public function loadEnvConfig(array $server)
|
|
|
|
{
|
|
|
|
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
|
|
|
|
self::STATIC_DIR . DIRECTORY_SEPARATOR . // static/
|
|
|
|
"env.config.php"; // env.config.php
|
|
|
|
|
|
|
|
if (!file_exists($filepath)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$envConfig = $this->loadConfigFile($filepath);
|
|
|
|
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
foreach ($envConfig as $envKey => $configStructure) {
|
|
|
|
if (isset($server[$envKey])) {
|
|
|
|
$return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2019-06-23 19:56:21 +02:00
|
|
|
/**
|
|
|
|
* Get the config files of the config-directory
|
|
|
|
*
|
|
|
|
* @param bool $ini True, if scan for ini-files instead of config files
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getConfigFiles(bool $ini = false)
|
|
|
|
{
|
|
|
|
$files = scandir($this->configDir);
|
|
|
|
$found = array();
|
|
|
|
|
|
|
|
$filePattern = ($ini ? '*.ini.php' : '*.config.php');
|
|
|
|
|
|
|
|
// Don't load sample files
|
|
|
|
$sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
|
|
|
|
|
|
|
|
foreach ($files as $filename) {
|
|
|
|
if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
|
|
|
|
$found[] = $this->configDir . '/' . $filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:02:25 +01:00
|
|
|
/**
|
|
|
|
* 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-06-23 19:56:21 +02:00
|
|
|
$name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
|
|
|
|
$fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
|
|
|
|
|
2019-03-16 20:04:11 +01:00
|
|
|
$config = [];
|
2019-06-23 19:56:21 +02:00
|
|
|
if (file_exists($fullName)) {
|
|
|
|
$a = new \stdClass();
|
2019-03-16 20:31:10 +01:00
|
|
|
$a->config = [];
|
2019-06-23 19:56:21 +02:00
|
|
|
include $fullName;
|
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
|
|
|
*
|
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-06-23 19:56:21 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @deprecated since version 2018.12
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
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) {
|
2019-06-23 19:56:21 +02:00
|
|
|
throw new Exception('Error parsing INI config file ' . $filepath);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-23 19:56:21 +02:00
|
|
|
* @param string $filepath The filepath of the
|
|
|
|
*
|
2019-02-05 22:27:57 +01:00
|
|
|
* @return array The config array0
|
|
|
|
*
|
2019-06-23 19:56:21 +02:00
|
|
|
* @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)) {
|
2019-06-23 19:56:21 +02:00
|
|
|
throw new Exception('Error loading config file ' . $filepath);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
}
|