Revert node.config.php into Config table

This commit is contained in:
Philipp 2023-02-05 00:15:01 +01:00
parent 6db89adc04
commit 513ef03421
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
27 changed files with 425 additions and 829 deletions

View file

@ -111,9 +111,6 @@ class ConfigFileManager
// Now load every other config you find inside the 'config/' directory
$this->loadCoreConfig($configCache);
// Now load the node.config.php file with the node specific config values (based on admin gui/console actions)
$this->loadDataConfig($configCache);
$configCache->load($this->loadEnvConfig(), Cache::SOURCE_ENV);
// In case of install mode, add the found basepath (because there isn't a basepath set yet
@ -166,171 +163,6 @@ class ConfigFileManager
}
}
/**
* Tries to load the data config file with the overridden data
*
* @param Cache $configCache The Config cache
*
* @throws ConfigFileException In case the config file isn't loadable
*/
private function loadDataConfig(Cache $configCache)
{
$filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
if (file_exists($filename) && (filesize($filename) > 0)) {
// The fallback empty return content
$content = '<?php return [];';
/**
* This code-block creates a readonly node.config.php content stream (fopen() with "r")
* The stream is locked shared (LOCK_SH), so not exclusively, but the OS knows that there's a lock
*
* Any exclusive locking (LOCK_EX) would need to wait until all LOCK_SHs are unlocked
*/
if (($configStream = @fopen($filename, 'r')) === false) {
throw new ConfigFileException(sprintf('Cannot open file "%s" in mode r', $filename));
}
try {
if (flock($configStream, LOCK_SH)) {
clearstatcache(true, $filename);
if (($filesize = filesize($filename)) === 0) {
return;
}
$content = fread($configStream, $filesize);
if (!$content) {
throw new ConfigFileException(sprintf('Couldn\'t read file %s', $filename));
}
} else {
throw new ConfigFileException(sprintf('Cannot lock file %s', $filename));
}
} finally {
// unlock and close the stream for every circumstances
flock($configStream, LOCK_UN);
fclose($configStream);
}
/**
* Evaluate the content string as PHP code
*
* @see https://www.php.net/manual/en/function.eval.php
*
* @note
* To leave the PHP mode, we have to use the appropriate PHP tags '?>' as prefix.
*/
$dataArray = eval('?>' . $content);
if (is_array($dataArray)) {
$configCache->load($dataArray, Cache::SOURCE_DATA);
}
}
}
/**
* Checks, if the node.config.php is writable
*
* @return bool
*/
public function dataIsWritable(): bool
{
$filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
if (file_exists($filename)) {
return is_writable($filename);
} else {
return is_writable($this->configDir);
}
}
/**
* Saves overridden config entries back into the data.config.php
*
* @param Cache $configCache The config cache
*
* @throws ConfigFileException In case the config file isn't writeable or the data is invalid
*/
public function saveData(Cache $configCache)
{
$filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
if (file_exists($filename)) {
$fileExists = true;
} else {
$fileExists = false;
}
/**
* Creates a read-write stream
*
* @see https://www.php.net/manual/en/function.fopen.php
* @note Open the file for reading and writing. If the file does not exist, it is created.
* If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails
* (as is the case with 'x'). The file pointer is positioned on the beginning of the file.
*
*/
if (($configStream = @fopen($filename, 'c+')) === false) {
throw new ConfigFileException(sprintf('Cannot open file "%s" in mode c+', $filename));
}
try {
// We do want an exclusive lock, so we wait until every LOCK_SH (config reading) is unlocked
if (flock($configStream, LOCK_EX)) {
/**
* If the file exists, we read the whole file again to avoid a race condition with concurrent threads that could have modified the file between the first config read of this thread and now
* Since we're currently exclusive locked, no other process can now change the config again
*/
if ($fileExists) {
// When reading the config file too fast, we get a wrong filesize, "clearstatcache" prevents that
clearstatcache(true, $filename);
$content = fread($configStream, filesize($filename));
if (!$content) {
throw new ConfigFileException(sprintf('Cannot read file %s', $filename));
}
// Event truncating the whole content wouldn't automatically rewind the stream,
// so we need to do it manually
rewind($configStream);
$dataArray = eval('?>' . $content);
// Merge the new content into the existing file based config cache and use it
// as the new config cache
if (is_array($dataArray)) {
$fileConfigCache = new Cache();
$fileConfigCache->load($dataArray, Cache::SOURCE_DATA);
$configCache = $fileConfigCache->merge($configCache);
}
}
// Only SOURCE_DATA is wanted, the rest isn't part of the node.config.php file
$data = $configCache->getDataBySource(Cache::SOURCE_DATA);
$encodedData = ConfigFileTransformer::encode($data);
if (!$encodedData) {
throw new ConfigFileException('config source cannot get encoded');
}
// Once again to avoid wrong, implicit "filesize" calls during the fwrite() or ftruncate() call
clearstatcache(true, $filename);
if (!ftruncate($configStream, 0) ||
!fwrite($configStream, $encodedData) ||
!fflush($configStream)) {
throw new ConfigFileException(sprintf('Cannot modify locked file %s', $filename));
}
} else {
throw new ConfigFileException(sprintf('Cannot lock file %s', $filename));
}
} finally {
// unlock and close the stream for every circumstances
flock($configStream, LOCK_UN);
fclose($configStream);
}
}
/**
* Tries to load the specified addon-configuration and returns the config array.
*