You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Adam Williamson <awilliam@redhat.com>
  6. * @author Aldo "xoen" Giambelluca <xoen@xoen.org>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Brice Maron <brice@bmaron.net>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  11. * @author Frank Karlitschek <frank@karlitschek.de>
  12. * @author Jakob Sack <mail@jakobsack.de>
  13. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  14. * @author Joas Schilling <coding@schilljs.com>
  15. * @author John Molakvoæ <skjnldsv@protonmail.com>
  16. * @author Lukas Reschke <lukas@statuscode.ch>
  17. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  18. * @author Morris Jobke <hey@morrisjobke.de>
  19. * @author Philipp Schaffrath <github@philipp.schaffrath.email>
  20. * @author Robin Appelman <robin@icewind.nl>
  21. * @author Robin McCorkell <robin@mccorkell.me.uk>
  22. * @author Roeland Jago Douma <roeland@famdouma.nl>
  23. *
  24. * @license AGPL-3.0
  25. *
  26. * This code is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Affero General Public License, version 3,
  28. * as published by the Free Software Foundation.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU Affero General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Affero General Public License, version 3,
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>
  37. *
  38. */
  39. namespace OC;
  40. use OCP\HintException;
  41. /**
  42. * This class is responsible for reading and writing config.php, the very basic
  43. * configuration file of Nextcloud.
  44. */
  45. class Config {
  46. public const ENV_PREFIX = 'NC_';
  47. /** @var array Associative array ($key => $value) */
  48. protected $cache = [];
  49. /** @var array */
  50. protected $envCache = [];
  51. /** @var string */
  52. protected $configDir;
  53. /** @var string */
  54. protected $configFilePath;
  55. /** @var string */
  56. protected $configFileName;
  57. /**
  58. * @param string $configDir Path to the config dir, needs to end with '/'
  59. * @param string $fileName (Optional) Name of the config file. Defaults to config.php
  60. */
  61. public function __construct($configDir, $fileName = 'config.php') {
  62. $this->configDir = $configDir;
  63. $this->configFilePath = $this->configDir.$fileName;
  64. $this->configFileName = $fileName;
  65. $this->readData();
  66. }
  67. /**
  68. * Lists all available config keys
  69. *
  70. * Please note that it does not return the values.
  71. *
  72. * @return array an array of key names
  73. */
  74. public function getKeys() {
  75. return array_keys($this->cache);
  76. }
  77. /**
  78. * Returns a config value
  79. *
  80. * gets its value from an `NC_` prefixed environment variable
  81. * if it doesn't exist from config.php
  82. * if this doesn't exist either, it will return the given `$default`
  83. *
  84. * @param string $key key
  85. * @param mixed $default = null default value
  86. * @return mixed the value or $default
  87. */
  88. public function getValue($key, $default = null) {
  89. $envKey = self::ENV_PREFIX . $key;
  90. if (isset($this->envCache[$envKey])) {
  91. return $this->envCache[$envKey];
  92. }
  93. if (isset($this->cache[$key])) {
  94. return $this->cache[$key];
  95. }
  96. return $default;
  97. }
  98. /**
  99. * Sets and deletes values and writes the config.php
  100. *
  101. * @param array $configs Associative array with `key => value` pairs
  102. * If value is null, the config key will be deleted
  103. */
  104. public function setValues(array $configs) {
  105. $needsUpdate = false;
  106. foreach ($configs as $key => $value) {
  107. if ($value !== null) {
  108. $needsUpdate |= $this->set($key, $value);
  109. } else {
  110. $needsUpdate |= $this->delete($key);
  111. }
  112. }
  113. if ($needsUpdate) {
  114. // Write changes
  115. $this->writeData();
  116. }
  117. }
  118. /**
  119. * Sets the value and writes it to config.php if required
  120. *
  121. * @param string $key key
  122. * @param mixed $value value
  123. */
  124. public function setValue($key, $value) {
  125. if ($this->set($key, $value)) {
  126. // Write changes
  127. $this->writeData();
  128. }
  129. }
  130. /**
  131. * This function sets the value
  132. *
  133. * @param string $key key
  134. * @param mixed $value value
  135. * @return bool True if the file needs to be updated, false otherwise
  136. */
  137. protected function set($key, $value) {
  138. if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
  139. // Add change
  140. $this->cache[$key] = $value;
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Removes a key from the config and removes it from config.php if required
  147. * @param string $key
  148. */
  149. public function deleteKey($key) {
  150. if ($this->delete($key)) {
  151. // Write changes
  152. $this->writeData();
  153. }
  154. }
  155. /**
  156. * This function removes a key from the config
  157. *
  158. * @param string $key
  159. * @return bool True if the file needs to be updated, false otherwise
  160. */
  161. protected function delete($key) {
  162. if (isset($this->cache[$key])) {
  163. // Delete key from cache
  164. unset($this->cache[$key]);
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * Loads the config file
  171. *
  172. * Reads the config file and saves it to the cache
  173. *
  174. * @throws \Exception If no lock could be acquired or the config file has not been found
  175. */
  176. private function readData() {
  177. // Default config should always get loaded
  178. $configFiles = [$this->configFilePath];
  179. // Add all files in the config dir ending with the same file name
  180. $extra = glob($this->configDir.'*.'.$this->configFileName);
  181. if (is_array($extra)) {
  182. natsort($extra);
  183. $configFiles = array_merge($configFiles, $extra);
  184. }
  185. // Include file and merge config
  186. foreach ($configFiles as $file) {
  187. $fileExistsAndIsReadable = file_exists($file) && is_readable($file);
  188. $filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
  189. if ($file === $this->configFilePath &&
  190. $filePointer === false) {
  191. // Opening the main config might not be possible, e.g. if the wrong
  192. // permissions are set (likely on a new installation)
  193. continue;
  194. }
  195. // Try to acquire a file lock
  196. if (!flock($filePointer, LOCK_SH)) {
  197. throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
  198. }
  199. unset($CONFIG);
  200. include $file;
  201. if (isset($CONFIG) && is_array($CONFIG)) {
  202. $this->cache = array_merge($this->cache, $CONFIG);
  203. }
  204. // Close the file pointer and release the lock
  205. flock($filePointer, LOCK_UN);
  206. fclose($filePointer);
  207. }
  208. $this->envCache = getenv();
  209. }
  210. /**
  211. * Writes the config file
  212. *
  213. * Saves the config to the config file.
  214. *
  215. * @throws HintException If the config file cannot be written to
  216. * @throws \Exception If no file lock can be acquired
  217. */
  218. private function writeData() {
  219. // Create a php file ...
  220. $content = "<?php\n";
  221. $content .= '$CONFIG = ';
  222. $content .= var_export($this->cache, true);
  223. $content .= ";\n";
  224. touch($this->configFilePath);
  225. $filePointer = fopen($this->configFilePath, 'r+');
  226. // Prevent others not to read the config
  227. chmod($this->configFilePath, 0640);
  228. // File does not exist, this can happen when doing a fresh install
  229. if (!is_resource($filePointer)) {
  230. throw new HintException(
  231. "Can't write into config directory!",
  232. 'This can usually be fixed by giving the webserver write access to the config directory.');
  233. }
  234. // Try to acquire a file lock
  235. if (!flock($filePointer, LOCK_EX)) {
  236. throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
  237. }
  238. // Write the config and release the lock
  239. ftruncate($filePointer, 0);
  240. fwrite($filePointer, $content);
  241. fflush($filePointer);
  242. flock($filePointer, LOCK_UN);
  243. fclose($filePointer);
  244. if (function_exists('opcache_invalidate')) {
  245. @opcache_invalidate($this->configFilePath, true);
  246. }
  247. }
  248. }