Fix #1721 add some safeguards when saving system config file

This commit is contained in:
Kijin Sung 2021-06-19 19:54:30 +09:00
parent b01a148675
commit 254634479c
3 changed files with 25 additions and 4 deletions

View file

@ -135,16 +135,34 @@ class Config
self::setAll($config);
}
// Backup the main config file.
$config_filename = \RX_BASEDIR . self::$config_filename;
$backup_filename = \RX_BASEDIR . self::$config_filename . '.backup.' . time() . '.php';
$result = Storage::copy($config_filename, $backup_filename);
clearstatcache(true, $backup_filename);
if (!$result || filesize($config_filename) !== filesize($backup_filename))
{
return false;
}
// Save the main config file.
$buff = '<?php' . "\n" . '// Rhymix System Configuration' . "\n" . 'return ' . self::serialize(self::$_config) . ';' . "\n";
$result = Storage::write(\RX_BASEDIR . self::$config_filename, $buff) ? true : false;
$result = Storage::write($config_filename, $buff) ? true : false;
clearstatcache(true, $config_filename);
if (!$result || filesize($config_filename) !== strlen($buff))
{
return false;
}
// Remove the backup file.
Storage::delete($backup_filename);
// Save XE-compatible config files.
$warning = '// THIS FILE IS NOT USED IN RHYMIX.' . "\n" . '// TO MODIFY SYSTEM CONFIGURATION, EDIT config.php INSTEAD.';
$buff = '<?php' . "\n" . $warning . "\n";
Storage::write(\RX_BASEDIR . self::$old_db_config_filename, $buff);
Storage::write(\RX_BASEDIR . self::$old_ftp_config_filename, $buff);
return $result;
return true;
}
/**

View file

@ -303,7 +303,7 @@ class Storage
flock($fp, \LOCK_UN);
fclose($fp);
if ($result === false)
if ($result === false || (is_string($content) && strlen($content) !== $result))
{
trigger_error('Cannot write file: ' . (isset($original_filename) ? $original_filename : $filename), \E_USER_WARNING);
return false;

View file

@ -1257,7 +1257,10 @@ class adminAdminController extends admin
Rhymix\Framework\Config::set('url.http_port', $vars->http_port ?: null);
Rhymix\Framework\Config::set('url.https_port', $vars->https_port ?: null);
Rhymix\Framework\Config::set('url.ssl', $vars->domain_security);
Rhymix\Framework\Config::save();
if (!Rhymix\Framework\Config::save())
{
throw new Rhymix\Framework\Exception('msg_failed_to_save_config');
}
}
// Commit.