Merge pull request #6920 from nupplaphil/feature/basepath/hostname_config
Removing basepath/hostname from admin site settings
This commit is contained in:
commit
76414a5169
25 changed files with 851 additions and 150 deletions
|
@ -5,7 +5,7 @@ namespace Friendica\Core\Config\Cache;
|
|||
/**
|
||||
* The Friendica config cache for the application
|
||||
* Initial, all *.config.php files are loaded into this cache with the
|
||||
* ConfigCacheLoader ( @see ConfigCacheLoader )
|
||||
* ConfigFileLoader ( @see ConfigFileLoader )
|
||||
*/
|
||||
class ConfigCache implements IConfigCache, IPConfigCache
|
||||
{
|
||||
|
|
|
@ -1,231 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Cache;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Addon;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
class ConfigCacheLoader
|
||||
{
|
||||
/**
|
||||
* The Sub directory of the config-files
|
||||
* @var string
|
||||
*/
|
||||
const SUBDIRECTORY = 'config';
|
||||
|
||||
private $baseDir;
|
||||
private $configDir;
|
||||
|
||||
/**
|
||||
* @var App\Mode
|
||||
*/
|
||||
private $appMode;
|
||||
|
||||
public function __construct($baseDir, App\Mode $mode)
|
||||
{
|
||||
$this->appMode = $mode;
|
||||
$this->baseDir = $baseDir;
|
||||
$this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param IConfigCache The config cache to load to
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function loadConfigFiles(IConfigCache $config)
|
||||
{
|
||||
$config->load($this->loadCoreConfig('defaults'));
|
||||
$config->load($this->loadCoreConfig('settings'));
|
||||
|
||||
$config->load($this->loadLegacyConfig('htpreconfig'), true);
|
||||
$config->load($this->loadLegacyConfig('htconfig'), true);
|
||||
|
||||
$config->load($this->loadCoreConfig('local'), true);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 . DIRECTORY_SEPARATOR . $name . '.config.php')) {
|
||||
return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
|
||||
} elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
|
||||
return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
|
||||
} 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.
|
||||
*
|
||||
* @param string $name The name of the config file
|
||||
*
|
||||
* @return array The configuration array (empty if no config found)
|
||||
*
|
||||
* @deprecated since version 2018.09
|
||||
*/
|
||||
private function loadLegacyConfig($name)
|
||||
{
|
||||
$filePath = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
|
||||
|
||||
$config = [];
|
||||
|
||||
if (file_exists($filePath)) {
|
||||
$a = new \stdClass();
|
||||
$a->config = [];
|
||||
include $filePath;
|
||||
|
||||
$htConfigCategories = array_keys($a->config);
|
||||
|
||||
// map the legacy configuration structure to the current structure
|
||||
foreach ($htConfigCategories as $htConfigCategory) {
|
||||
if (is_array($a->config[$htConfigCategory])) {
|
||||
$keys = array_keys($a->config[$htConfigCategory]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
|
||||
}
|
||||
} else {
|
||||
$config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
|
||||
}
|
||||
}
|
||||
|
||||
unset($a);
|
||||
|
||||
if (isset($db_host)) {
|
||||
$config['database']['hostname'] = $db_host;
|
||||
unset($db_host);
|
||||
}
|
||||
if (isset($db_user)) {
|
||||
$config['database']['username'] = $db_user;
|
||||
unset($db_user);
|
||||
}
|
||||
if (isset($db_pass)) {
|
||||
$config['database']['password'] = $db_pass;
|
||||
unset($db_pass);
|
||||
}
|
||||
if (isset($db_data)) {
|
||||
$config['database']['database'] = $db_data;
|
||||
unset($db_data);
|
||||
}
|
||||
if (isset($config['system']['db_charset'])) {
|
||||
$config['database']['charset'] = $config['system']['db_charset'];
|
||||
}
|
||||
if (isset($pidfile)) {
|
||||
$config['system']['pidfile'] = $pidfile;
|
||||
unset($pidfile);
|
||||
}
|
||||
if (isset($default_timezone)) {
|
||||
$config['system']['default_timezone'] = $default_timezone;
|
||||
unset($default_timezone);
|
||||
}
|
||||
if (isset($lang)) {
|
||||
$config['system']['language'] = $lang;
|
||||
unset($lang);
|
||||
}
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified legacy configuration file and returns the config array.
|
||||
*
|
||||
* @deprecated since version 2018.12
|
||||
* @param string $filepath
|
||||
*
|
||||
* @return array The configuration array
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function loadINIConfigFile($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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified configuration file and returns the config array.
|
||||
*
|
||||
* The config format is PHP array and the template for configuration files is the following:
|
||||
*
|
||||
* <?php return [
|
||||
* 'section' => [
|
||||
* 'key' => 'value',
|
||||
* ],
|
||||
* ];
|
||||
*
|
||||
* @param string $filepath The filepath of the
|
||||
* @return array The config array0
|
||||
*
|
||||
* @throws \Exception if the config cannot get loaded.
|
||||
*/
|
||||
private function loadConfigFile($filepath)
|
||||
{
|
||||
$config = include($filepath);
|
||||
|
||||
if (!is_array($config)) {
|
||||
throw new \Exception('Error loading config file ' . $filepath);
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,16 @@ namespace Friendica\Core\Config;
|
|||
*/
|
||||
class Configuration
|
||||
{
|
||||
/**
|
||||
* The blacklist of configuration settings, which should not get saved to the backend
|
||||
* @var array
|
||||
*/
|
||||
private $configSaveBlacklist = [
|
||||
'config' => [
|
||||
'hostname' => true,
|
||||
]
|
||||
];
|
||||
|
||||
/**
|
||||
* @var Cache\IConfigCache
|
||||
*/
|
||||
|
@ -117,7 +127,7 @@ class Configuration
|
|||
$cached = $this->configCache->set($cat, $key, $value);
|
||||
|
||||
// If there is no connected adapter, we're finished
|
||||
if (!$this->configAdapter->isConnected()) {
|
||||
if (!$this->configAdapter->isConnected() || !empty($this->configSaveBlacklist[$cat][$key])) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ use Friendica\BaseObject;
|
|||
use Friendica\Core\Config;
|
||||
use Friendica\Core\Installer;
|
||||
use Friendica\Core\Theme;
|
||||
use Friendica\Util\Config\ConfigFileLoader;
|
||||
use RuntimeException;
|
||||
|
||||
class AutomaticInstallation extends Console
|
||||
|
@ -103,8 +104,8 @@ HELP;
|
|||
}
|
||||
|
||||
//reload the config cache
|
||||
$loader = new Config\Cache\ConfigCacheLoader($a->getBasePath(), $a->getMode());
|
||||
$loader->loadConfigFiles($configCache);
|
||||
$loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
|
||||
$loader->setupCache($configCache);
|
||||
|
||||
} else {
|
||||
// Creating config file
|
||||
|
|
|
@ -2,8 +2,12 @@
|
|||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Config\Cache\IConfigCache;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\DBStructure;
|
||||
use Friendica\Util\Config\ConfigFileLoader;
|
||||
use Friendica\Util\Config\ConfigFileSaver;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
class Update
|
||||
|
@ -24,6 +28,11 @@ class Update
|
|||
return;
|
||||
}
|
||||
|
||||
// Don't check the status if the last update was failed
|
||||
if (Config::get('system', 'update', Update::SUCCESS, true) == Update::FAILED) {
|
||||
return;
|
||||
}
|
||||
|
||||
$build = Config::get('system', 'build');
|
||||
|
||||
if (empty($build)) {
|
||||
|
@ -101,7 +110,9 @@ class Update
|
|||
for ($x = $stored + 1; $x <= $current; $x++) {
|
||||
$r = self::runUpdateFunction($x, 'pre_update');
|
||||
if (!$r) {
|
||||
break;
|
||||
Config::set('system', 'update', Update::FAILED);
|
||||
Lock::release('dbupdate');
|
||||
return $r;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,6 +126,7 @@ class Update
|
|||
);
|
||||
}
|
||||
Logger::error('Update ERROR.', ['from' => $stored, 'to' => $current, 'retval' => $retval]);
|
||||
Config::set('system', 'update', Update::FAILED);
|
||||
Lock::release('dbupdate');
|
||||
return $retval;
|
||||
} else {
|
||||
|
@ -127,7 +139,9 @@ class Update
|
|||
for ($x = $stored + 1; $x <= $current; $x++) {
|
||||
$r = self::runUpdateFunction($x, 'update');
|
||||
if (!$r) {
|
||||
break;
|
||||
Config::set('system', 'update', Update::FAILED);
|
||||
Lock::release('dbupdate');
|
||||
return $r;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,6 +150,7 @@ class Update
|
|||
self::updateSuccessfull($stored, $current);
|
||||
}
|
||||
|
||||
Config::set('system', 'update', Update::SUCCESS);
|
||||
Lock::release('dbupdate');
|
||||
}
|
||||
}
|
||||
|
@ -208,6 +223,74 @@ class Update
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the config settings and saves given config values into the config file
|
||||
*
|
||||
* @param string $basePath The basepath of Friendica
|
||||
* @param App\Mode $mode The Application mode
|
||||
*
|
||||
* @return bool True, if something has been saved
|
||||
*/
|
||||
public static function saveConfigToFile($basePath, App\Mode $mode)
|
||||
{
|
||||
$configFileLoader = new ConfigFileLoader($basePath, $mode);
|
||||
$configCache = new Config\Cache\ConfigCache();
|
||||
$configFileLoader->setupCache($configCache);
|
||||
$configFileSaver = new ConfigFileSaver($basePath);
|
||||
|
||||
$updated = false;
|
||||
|
||||
if (self::updateConfigEntry($configCache, $configFileSaver,'config', 'hostname')) {
|
||||
$updated = true;
|
||||
};
|
||||
if (self::updateConfigEntry($configCache, $configFileSaver,'system', 'basepath')) {
|
||||
$updated = true;
|
||||
}
|
||||
|
||||
if (!$configFileSaver->saveToConfigFile()) {
|
||||
Logger::alert('Config entry update failed - maybe wrong permission?');
|
||||
return false;
|
||||
}
|
||||
|
||||
DBA::delete('config', ['cat' => 'config', 'k' => 'hostname']);
|
||||
DBA::delete('config', ['cat' => 'system', 'k' => 'basepath']);
|
||||
|
||||
return $updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a value to the ConfigFileSave in case it isn't already updated
|
||||
*
|
||||
* @param IConfigCache $configCache The cached config file
|
||||
* @param ConfigFileSaver $configFileSaver The config file saver
|
||||
* @param string $cat The config category
|
||||
* @param string $key The config key
|
||||
*
|
||||
* @return boolean True, if a value was updated
|
||||
*
|
||||
* @throws \Exception if DBA or Logger doesn't work
|
||||
*/
|
||||
private static function updateConfigEntry(IConfigCache $configCache, ConfigFileSaver $configFileSaver, $cat, $key)
|
||||
{
|
||||
// check if the config file differs from the whole configuration (= The db contains other values)
|
||||
$fileConfig = $configCache->get($cat, $key);
|
||||
|
||||
$savedConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
|
||||
|
||||
if (!DBA::isResult($savedConfig)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($fileConfig !== $savedConfig['v']) {
|
||||
Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileConfig, 'saved' => $savedConfig['v']]);
|
||||
$configFileSaver->addConfigValue($cat, $key, $savedConfig['v']);
|
||||
} else {
|
||||
Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedConfig['v']]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* send the email and do what is needed to do on update fails
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue