瀏覽代碼

Merge pull request #30130 from nextcloud/fix/config_is_read_only

Don't write to config file if `config_is_read_only` is set
tags/v24.0.0beta1
Christoph Wurst 2 年之前
父節點
當前提交
4b36f9d92f
沒有連結到貢獻者的電子郵件帳戶。
共有 3 個檔案被更改,包括 33 行新增5 行删除
  1. 5
    5
      config/config.sample.php
  2. 26
    0
      lib/private/Config.php
  3. 2
    0
      lib/public/IConfig.php

+ 5
- 5
config/config.sample.php 查看文件



/** /**
* In certain environments it is desired to have a read-only configuration file. * In certain environments it is desired to have a read-only configuration file.
* When this switch is set to ``true`` Nextcloud will not verify whether the
* configuration is writable. However, it will not be possible to configure
* all options via the Web interface. Furthermore, when updating Nextcloud
* it is required to make the configuration file writable again for the update
* process.
* When this switch is set to ``true``, writing to the config file will be
* forbidden. Therefore, it will not be possible to configure all options via
* the Web interface. Furthermore, when updating Nextcloud it is required to
* make the configuration file writable again and to set this switch to ``false``
* for the update process.
* *
* Defaults to ``false`` * Defaults to ``false``
*/ */

+ 26
- 0
lib/private/Config.php 查看文件

protected $configFilePath; protected $configFilePath;
/** @var string */ /** @var string */
protected $configFileName; protected $configFileName;
/** @var bool */
protected $isReadOnly;


/** /**
* @param string $configDir Path to the config dir, needs to end with '/' * @param string $configDir Path to the config dir, needs to end with '/'
$this->configFilePath = $this->configDir.$fileName; $this->configFilePath = $this->configDir.$fileName;
$this->configFileName = $fileName; $this->configFileName = $fileName;
$this->readData(); $this->readData();
$this->isReadOnly = $this->getValue('config_is_read_only', false);
} }


/** /**
* *
* @param array $configs Associative array with `key => value` pairs * @param array $configs Associative array with `key => value` pairs
* If value is null, the config key will be deleted * If value is null, the config key will be deleted
* @throws HintException
*/ */
public function setValues(array $configs) { public function setValues(array $configs) {
$needsUpdate = false; $needsUpdate = false;
* *
* @param string $key key * @param string $key key
* @param mixed $value value * @param mixed $value value
* @throws HintException
*/ */
public function setValue($key, $value) { public function setValue($key, $value) {
if ($this->set($key, $value)) { if ($this->set($key, $value)) {
* @param string $key key * @param string $key key
* @param mixed $value value * @param mixed $value value
* @return bool True if the file needs to be updated, false otherwise * @return bool True if the file needs to be updated, false otherwise
* @throws HintException
*/ */
protected function set($key, $value) { protected function set($key, $value) {
$this->checkReadOnly();

if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) { if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
// Add change // Add change
$this->cache[$key] = $value; $this->cache[$key] = $value;


/** /**
* Removes a key from the config and removes it from config.php if required * Removes a key from the config and removes it from config.php if required
*
* @param string $key * @param string $key
* @throws HintException
*/ */
public function deleteKey($key) { public function deleteKey($key) {
if ($this->delete($key)) { if ($this->delete($key)) {
* *
* @param string $key * @param string $key
* @return bool True if the file needs to be updated, false otherwise * @return bool True if the file needs to be updated, false otherwise
* @throws HintException
*/ */
protected function delete($key) { protected function delete($key) {
$this->checkReadOnly();

if (isset($this->cache[$key])) { if (isset($this->cache[$key])) {
// Delete key from cache // Delete key from cache
unset($this->cache[$key]); unset($this->cache[$key]);
* @throws \Exception If no file lock can be acquired * @throws \Exception If no file lock can be acquired
*/ */
private function writeData() { private function writeData() {
$this->checkReadOnly();

// Create a php file ... // Create a php file ...
$content = "<?php\n"; $content = "<?php\n";
$content .= '$CONFIG = '; $content .= '$CONFIG = ';
@opcache_invalidate($this->configFilePath, true); @opcache_invalidate($this->configFilePath, true);
} }
} }

/**
* @throws HintException
*/
private function checkReadOnly(): void {
if ($this->isReadOnly) {
throw new HintException(
'Config is set to be read-only via option "config_is_read_only".',
'Unset "config_is_read_only" to allow changes to the config file.');
}
}
} }

+ 2
- 0
lib/public/IConfig.php 查看文件

* *
* @param array $configs Associative array with `key => value` pairs * @param array $configs Associative array with `key => value` pairs
* If value is null, the config key will be deleted * If value is null, the config key will be deleted
* @throws HintException if config file is read-only
* @since 8.0.0 * @since 8.0.0
*/ */
public function setSystemValues(array $configs); public function setSystemValues(array $configs);
* *
* @param string $key the key of the value, under which will be saved * @param string $key the key of the value, under which will be saved
* @param mixed $value the value that should be stored * @param mixed $value the value that should be stored
* @throws HintException if config file is read-only
* @since 8.0.0 * @since 8.0.0
*/ */
public function setSystemValue($key, $value); public function setSystemValue($key, $value);

Loading…
取消
儲存