2010-03-16 15:25:05 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2011-03-12 12:19:11 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2011-03-13 12:25:34 -04:00
|
|
|
*/
|
2013-03-03 06:06:00 -05:00
|
|
|
namespace OC;
|
|
|
|
|
|
2021-06-29 19:20:33 -04:00
|
|
|
use OCP\HintException;
|
|
|
|
|
|
2011-03-12 12:19:11 -05:00
|
|
|
/**
|
|
|
|
|
* This class is responsible for reading and writing config.php, the very basic
|
2018-03-21 09:30:56 -04:00
|
|
|
* configuration file of Nextcloud.
|
2011-03-12 12:19:11 -05:00
|
|
|
*/
|
2013-03-03 06:06:00 -05:00
|
|
|
class Config {
|
2020-04-10 10:54:27 -04:00
|
|
|
public const ENV_PREFIX = 'NC_';
|
2017-01-24 06:18:29 -05:00
|
|
|
|
2014-09-25 12:43:04 -04:00
|
|
|
/** @var array Associative array ($key => $value) */
|
2020-03-26 04:30:18 -04:00
|
|
|
protected $cache = [];
|
2020-11-02 15:30:14 -05:00
|
|
|
/** @var array */
|
|
|
|
|
protected $envCache = [];
|
2014-09-25 12:43:04 -04:00
|
|
|
/** @var string */
|
2013-05-08 12:20:44 -04:00
|
|
|
protected $configDir;
|
2014-09-25 12:43:04 -04:00
|
|
|
/** @var string */
|
|
|
|
|
protected $configFilePath;
|
|
|
|
|
/** @var string */
|
|
|
|
|
protected $configFileName;
|
2021-12-06 06:40:57 -05:00
|
|
|
/** @var bool */
|
|
|
|
|
protected $isReadOnly;
|
2013-06-27 16:24:17 -04:00
|
|
|
|
2013-07-08 12:01:32 -04:00
|
|
|
/**
|
2014-09-25 12:43:04 -04:00
|
|
|
* @param string $configDir Path to the config dir, needs to end with '/'
|
|
|
|
|
* @param string $fileName (Optional) Name of the config file. Defaults to config.php
|
2013-07-08 12:01:32 -04:00
|
|
|
*/
|
2014-09-25 12:43:04 -04:00
|
|
|
public function __construct($configDir, $fileName = 'config.php') {
|
2013-05-08 12:20:44 -04:00
|
|
|
$this->configDir = $configDir;
|
2024-09-19 05:10:31 -04:00
|
|
|
$this->configFilePath = $this->configDir . $fileName;
|
2014-09-25 12:43:04 -04:00
|
|
|
$this->configFileName = $fileName;
|
2013-03-03 06:06:00 -05:00
|
|
|
$this->readData();
|
2021-12-06 06:40:57 -05:00
|
|
|
$this->isReadOnly = $this->getValue('config_is_read_only', false);
|
2014-09-25 12:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
2010-04-22 18:05:04 -04:00
|
|
|
/**
|
2014-05-19 11:50:53 -04:00
|
|
|
* Lists all available config keys
|
2011-03-12 12:19:11 -05:00
|
|
|
*
|
2015-01-23 04:50:25 -05:00
|
|
|
* Please note that it does not return the values.
|
|
|
|
|
*
|
|
|
|
|
* @return array an array of key names
|
2011-03-12 12:19:11 -05:00
|
|
|
*/
|
2013-03-03 06:06:00 -05:00
|
|
|
public function getKeys() {
|
2024-06-26 09:31:44 -04:00
|
|
|
return array_merge(array_keys($this->cache), array_keys($this->envCache));
|
2010-04-22 18:05:04 -04:00
|
|
|
}
|
2010-07-11 16:44:48 -04:00
|
|
|
|
|
|
|
|
/**
|
2017-01-24 06:18:29 -05:00
|
|
|
* Returns a config value
|
2015-01-23 04:50:25 -05:00
|
|
|
*
|
2017-03-20 23:29:55 -04:00
|
|
|
* gets its value from an `NC_` prefixed environment variable
|
2017-01-24 06:18:29 -05:00
|
|
|
* if it doesn't exist from config.php
|
|
|
|
|
* if this doesn't exist either, it will return the given `$default`
|
2015-01-23 04:50:25 -05:00
|
|
|
*
|
2012-09-22 20:39:11 -04:00
|
|
|
* @param string $key key
|
2014-05-12 18:27:36 -04:00
|
|
|
* @param mixed $default = null default value
|
|
|
|
|
* @return mixed the value or $default
|
2010-07-11 16:44:48 -04:00
|
|
|
*/
|
2013-06-03 18:05:38 -04:00
|
|
|
public function getValue($key, $default = null) {
|
2024-06-26 09:31:44 -04:00
|
|
|
if (isset($this->envCache[$key])) {
|
2025-02-17 06:35:18 -05:00
|
|
|
return self::trustSystemConfig($this->envCache[$key]);
|
2017-01-24 06:18:29 -05:00
|
|
|
}
|
|
|
|
|
|
2013-06-03 18:05:38 -04:00
|
|
|
if (isset($this->cache[$key])) {
|
2025-02-17 06:35:18 -05:00
|
|
|
return self::trustSystemConfig($this->cache[$key]);
|
2011-04-08 10:54:12 -04:00
|
|
|
}
|
|
|
|
|
|
2011-03-12 12:19:11 -05:00
|
|
|
return $default;
|
2010-07-11 16:44:48 -04:00
|
|
|
}
|
2010-04-22 18:05:04 -04:00
|
|
|
|
2025-02-17 06:35:18 -05:00
|
|
|
/**
|
|
|
|
|
* Since system config is admin controlled, we can tell psalm to ignore any taint
|
|
|
|
|
*
|
|
|
|
|
* @psalm-taint-escape callable
|
|
|
|
|
* @psalm-taint-escape cookie
|
|
|
|
|
* @psalm-taint-escape file
|
|
|
|
|
* @psalm-taint-escape has_quotes
|
|
|
|
|
* @psalm-taint-escape header
|
|
|
|
|
* @psalm-taint-escape html
|
|
|
|
|
* @psalm-taint-escape include
|
|
|
|
|
* @psalm-taint-escape ldap
|
|
|
|
|
* @psalm-taint-escape shell
|
|
|
|
|
* @psalm-taint-escape sql
|
|
|
|
|
* @psalm-taint-escape unserialize
|
|
|
|
|
* @psalm-pure
|
|
|
|
|
*/
|
|
|
|
|
public static function trustSystemConfig(mixed $value): mixed {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-18 14:08:24 -04:00
|
|
|
/**
|
2015-01-23 04:50:25 -05:00
|
|
|
* Sets and deletes values and writes the config.php
|
2013-06-03 18:05:38 -04:00
|
|
|
*
|
2015-01-23 04:50:25 -05:00
|
|
|
* @param array $configs Associative array with `key => value` pairs
|
|
|
|
|
* If value is null, the config key will be deleted
|
2021-12-06 06:40:57 -05:00
|
|
|
* @throws HintException
|
2011-03-12 12:19:11 -05:00
|
|
|
*/
|
2015-01-23 04:50:25 -05:00
|
|
|
public function setValues(array $configs) {
|
|
|
|
|
$needsUpdate = false;
|
|
|
|
|
foreach ($configs as $key => $value) {
|
|
|
|
|
if ($value !== null) {
|
|
|
|
|
$needsUpdate |= $this->set($key, $value);
|
|
|
|
|
} else {
|
|
|
|
|
$needsUpdate |= $this->delete($key);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-08 10:54:12 -04:00
|
|
|
|
2015-01-23 04:50:25 -05:00
|
|
|
if ($needsUpdate) {
|
|
|
|
|
// Write changes
|
|
|
|
|
$this->writeData();
|
|
|
|
|
}
|
2010-06-18 14:08:24 -04:00
|
|
|
}
|
2011-03-12 12:19:11 -05:00
|
|
|
|
2010-06-18 14:08:24 -04:00
|
|
|
/**
|
2015-01-23 04:50:25 -05:00
|
|
|
* Sets the value and writes it to config.php if required
|
2011-03-12 12:19:11 -05:00
|
|
|
*
|
2015-01-23 04:50:25 -05:00
|
|
|
* @param string $key key
|
|
|
|
|
* @param mixed $value value
|
2021-12-06 06:40:57 -05:00
|
|
|
* @throws HintException
|
2015-01-23 04:50:25 -05:00
|
|
|
*/
|
|
|
|
|
public function setValue($key, $value) {
|
|
|
|
|
if ($this->set($key, $value)) {
|
|
|
|
|
// Write changes
|
|
|
|
|
$this->writeData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function sets the value
|
2013-06-03 18:05:38 -04:00
|
|
|
*
|
2015-01-23 04:50:25 -05:00
|
|
|
* @param string $key key
|
|
|
|
|
* @param mixed $value value
|
|
|
|
|
* @return bool True if the file needs to be updated, false otherwise
|
2021-12-06 06:40:57 -05:00
|
|
|
* @throws HintException
|
2015-01-23 04:50:25 -05:00
|
|
|
*/
|
|
|
|
|
protected function set($key, $value) {
|
|
|
|
|
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
|
|
|
|
|
// Add change
|
|
|
|
|
$this->cache[$key] = $value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes a key from the config and removes it from config.php if required
|
2021-12-06 06:40:57 -05:00
|
|
|
*
|
2015-01-23 04:50:25 -05:00
|
|
|
* @param string $key
|
2021-12-06 06:40:57 -05:00
|
|
|
* @throws HintException
|
2011-03-12 12:19:11 -05:00
|
|
|
*/
|
2013-06-03 18:05:38 -04:00
|
|
|
public function deleteKey($key) {
|
2015-01-23 04:50:25 -05:00
|
|
|
if ($this->delete($key)) {
|
|
|
|
|
// Write changes
|
|
|
|
|
$this->writeData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function removes a key from the config
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return bool True if the file needs to be updated, false otherwise
|
2021-12-06 06:40:57 -05:00
|
|
|
* @throws HintException
|
2015-01-23 04:50:25 -05:00
|
|
|
*/
|
|
|
|
|
protected function delete($key) {
|
2013-06-03 18:05:38 -04:00
|
|
|
if (isset($this->cache[$key])) {
|
2011-04-08 10:54:12 -04:00
|
|
|
// Delete key from cache
|
2013-06-03 18:05:38 -04:00
|
|
|
unset($this->cache[$key]);
|
2015-01-23 04:50:25 -05:00
|
|
|
return true;
|
2011-04-08 10:54:12 -04:00
|
|
|
}
|
2015-01-23 04:50:25 -05:00
|
|
|
return false;
|
2011-04-08 10:54:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-19 11:50:53 -04:00
|
|
|
* Loads the config file
|
2011-04-08 10:54:12 -04:00
|
|
|
*
|
|
|
|
|
* Reads the config file and saves it to the cache
|
2014-09-25 12:43:04 -04:00
|
|
|
*
|
|
|
|
|
* @throws \Exception If no lock could be acquired or the config file has not been found
|
2011-04-08 10:54:12 -04:00
|
|
|
*/
|
2013-03-03 06:06:00 -05:00
|
|
|
private function readData() {
|
2014-09-25 12:43:04 -04:00
|
|
|
// Default config should always get loaded
|
2020-03-26 04:30:18 -04:00
|
|
|
$configFiles = [$this->configFilePath];
|
2014-09-25 12:43:04 -04:00
|
|
|
|
|
|
|
|
// Add all files in the config dir ending with the same file name
|
2024-09-19 05:10:31 -04:00
|
|
|
$extra = glob($this->configDir . '*.' . $this->configFileName);
|
2013-06-10 11:42:20 -04:00
|
|
|
if (is_array($extra)) {
|
|
|
|
|
natsort($extra);
|
|
|
|
|
$configFiles = array_merge($configFiles, $extra);
|
|
|
|
|
}
|
2014-09-25 12:43:04 -04:00
|
|
|
|
2013-06-10 11:42:20 -04:00
|
|
|
// Include file and merge config
|
2013-06-03 18:05:38 -04:00
|
|
|
foreach ($configFiles as $file) {
|
2024-03-15 13:24:17 -04:00
|
|
|
unset($CONFIG);
|
|
|
|
|
|
|
|
|
|
// Invalidate opcache (only if the timestamp changed)
|
|
|
|
|
if (function_exists('opcache_invalidate')) {
|
2024-08-28 08:20:25 -04:00
|
|
|
@opcache_invalidate($file, false);
|
2024-03-15 13:24:17 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-27 17:28:14 -04:00
|
|
|
// suppressor doesn't work here at boot time since it'll go via our onError custom error handler
|
|
|
|
|
$filePointer = file_exists($file) ? @fopen($file, 'r') : false;
|
2024-03-15 13:24:17 -04:00
|
|
|
if ($filePointer === false) {
|
|
|
|
|
// e.g. wrong permissions are set
|
|
|
|
|
if ($file === $this->configFilePath) {
|
|
|
|
|
// opening the main config file might not be possible
|
|
|
|
|
// (likely on a new installation)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http_response_code(500);
|
|
|
|
|
die(sprintf('FATAL: Could not open the config file %s', $file));
|
2013-03-03 06:06:00 -05:00
|
|
|
}
|
2014-09-25 12:43:04 -04:00
|
|
|
|
2022-09-19 08:02:07 -04:00
|
|
|
// Try to acquire a file lock
|
|
|
|
|
if (!flock($filePointer, LOCK_SH)) {
|
|
|
|
|
throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-15 13:24:17 -04:00
|
|
|
try {
|
|
|
|
|
include $file;
|
|
|
|
|
} finally {
|
|
|
|
|
// Close the file pointer and release the lock
|
|
|
|
|
flock($filePointer, LOCK_UN);
|
|
|
|
|
fclose($filePointer);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 04:19:05 -04:00
|
|
|
if (!defined('PHPUNIT_RUN') && headers_sent()) {
|
|
|
|
|
// syntax issues in the config file like leading spaces causing PHP to send output
|
|
|
|
|
$errorMessage = sprintf('Config file has leading content, please remove everything before "<?php" in %s', basename($file));
|
|
|
|
|
if (!defined('OC_CONSOLE')) {
|
|
|
|
|
print(\OCP\Util::sanitizeHTML($errorMessage));
|
|
|
|
|
}
|
|
|
|
|
throw new \Exception($errorMessage);
|
|
|
|
|
}
|
2020-04-10 08:19:56 -04:00
|
|
|
if (isset($CONFIG) && is_array($CONFIG)) {
|
2026-01-06 08:53:24 -05:00
|
|
|
$this->cache = array_replace_recursive($this->cache, $CONFIG);
|
2013-03-06 17:30:16 -05:00
|
|
|
}
|
2011-05-15 09:03:12 -04:00
|
|
|
}
|
2020-11-02 15:30:14 -05:00
|
|
|
|
2024-06-26 09:31:44 -04:00
|
|
|
// grab any "NC_" environment variables
|
|
|
|
|
$envRaw = getenv();
|
|
|
|
|
// only save environment variables prefixed with "NC_" in the cache
|
2024-06-27 10:57:14 -04:00
|
|
|
$envPrefixLen = strlen(self::ENV_PREFIX);
|
2024-06-26 09:31:44 -04:00
|
|
|
foreach ($envRaw as $rawEnvKey => $rawEnvValue) {
|
|
|
|
|
if (str_starts_with($rawEnvKey, self::ENV_PREFIX)) {
|
2024-06-27 10:57:14 -04:00
|
|
|
$realKey = substr($rawEnvKey, $envPrefixLen);
|
2024-06-26 09:31:44 -04:00
|
|
|
$this->envCache[$realKey] = $rawEnvValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-08 10:54:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-19 11:50:53 -04:00
|
|
|
* Writes the config file
|
2011-04-08 10:54:12 -04:00
|
|
|
*
|
|
|
|
|
* Saves the config to the config file.
|
|
|
|
|
*
|
2022-09-19 08:02:07 -04:00
|
|
|
* @throws HintException If the config file cannot be written to
|
|
|
|
|
* @throws \Exception If no file lock can be acquired
|
2011-04-08 10:54:12 -04:00
|
|
|
*/
|
2025-02-17 12:06:45 -05:00
|
|
|
private function writeData(): void {
|
2021-12-06 06:40:57 -05:00
|
|
|
$this->checkReadOnly();
|
|
|
|
|
|
2024-09-19 05:10:31 -04:00
|
|
|
if (!is_file(\OC::$configDir . '/CAN_INSTALL') && !isset($this->cache['version'])) {
|
2022-09-06 11:22:16 -04:00
|
|
|
throw new HintException(sprintf('Configuration was not read or initialized correctly, not overwriting %s', $this->configFilePath));
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-08 10:54:12 -04:00
|
|
|
// Create a php file ...
|
2013-03-03 06:06:00 -05:00
|
|
|
$content = "<?php\n";
|
|
|
|
|
$content .= '$CONFIG = ';
|
2025-02-17 12:06:45 -05:00
|
|
|
$content .= var_export(self::trustSystemConfig($this->cache), true);
|
2012-06-22 17:01:12 -04:00
|
|
|
$content .= ";\n";
|
2011-04-08 10:54:12 -04:00
|
|
|
|
2022-09-19 08:02:07 -04:00
|
|
|
touch($this->configFilePath);
|
|
|
|
|
$filePointer = fopen($this->configFilePath, 'r+');
|
2014-09-25 12:43:04 -04:00
|
|
|
|
2022-09-19 08:02:07 -04:00
|
|
|
// Prevent others not to read the config
|
|
|
|
|
chmod($this->configFilePath, 0640);
|
|
|
|
|
|
|
|
|
|
// File does not exist, this can happen when doing a fresh install
|
2020-04-10 08:19:56 -04:00
|
|
|
if (!is_resource($filePointer)) {
|
2013-03-03 06:06:00 -05:00
|
|
|
throw new HintException(
|
2022-09-19 08:02:07 -04:00
|
|
|
"Can't write into config directory!",
|
|
|
|
|
'This can usually be fixed by giving the webserver write access to the config directory.');
|
2011-08-12 13:22:29 -04:00
|
|
|
}
|
2014-09-25 12:43:04 -04:00
|
|
|
|
2023-02-15 07:43:54 -05:00
|
|
|
// Never write file back if disk space should be too low
|
2023-03-07 03:51:00 -05:00
|
|
|
if (function_exists('disk_free_space')) {
|
|
|
|
|
$df = disk_free_space($this->configDir);
|
|
|
|
|
$size = strlen($content) + 10240;
|
|
|
|
|
if ($df !== false && $df < (float)$size) {
|
2024-08-23 09:10:27 -04:00
|
|
|
throw new \Exception($this->configDir . ' does not have enough space for writing the config file! Not writing it back!');
|
2023-03-07 03:51:00 -05:00
|
|
|
}
|
2023-02-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:02:07 -04:00
|
|
|
// Try to acquire a file lock
|
|
|
|
|
if (!flock($filePointer, LOCK_EX)) {
|
|
|
|
|
throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
|
2014-09-25 12:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:02:07 -04:00
|
|
|
// Write the config and release the lock
|
|
|
|
|
ftruncate($filePointer, 0);
|
|
|
|
|
fwrite($filePointer, $content);
|
|
|
|
|
fflush($filePointer);
|
|
|
|
|
flock($filePointer, LOCK_UN);
|
|
|
|
|
fclose($filePointer);
|
2014-09-25 12:43:04 -04:00
|
|
|
|
2019-01-30 11:36:14 -05:00
|
|
|
if (function_exists('opcache_invalidate')) {
|
|
|
|
|
@opcache_invalidate($this->configFilePath, true);
|
2014-09-13 02:33:18 -04:00
|
|
|
}
|
2010-04-08 08:49:48 -04:00
|
|
|
}
|
2021-12-06 06:40:57 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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.');
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-16 15:25:05 -04:00
|
|
|
}
|