選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Config.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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) <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. /**
  41. * This class is responsible for reading and writing config.php, the very basic
  42. * configuration file of Nextcloud.
  43. */
  44. class Config {
  45. public const ENV_PREFIX = 'NC_';
  46. /** @var array Associative array ($key => $value) */
  47. protected $cache = [];
  48. /** @var array */
  49. protected $envCache = [];
  50. /** @var string */
  51. protected $configDir;
  52. /** @var string */
  53. protected $configFilePath;
  54. /** @var string */
  55. protected $configFileName;
  56. /**
  57. * @param string $configDir Path to the config dir, needs to end with '/'
  58. * @param string $fileName (Optional) Name of the config file. Defaults to config.php
  59. */
  60. public function __construct($configDir, $fileName = 'config.php') {
  61. $this->configDir = $configDir;
  62. $this->configFilePath = $this->configDir.$fileName;
  63. $this->configFileName = $fileName;
  64. $this->readData();
  65. }
  66. /**
  67. * Lists all available config keys
  68. *
  69. * Please note that it does not return the values.
  70. *
  71. * @return array an array of key names
  72. */
  73. public function getKeys() {
  74. return array_keys($this->cache);
  75. }
  76. /**
  77. * Returns a config value
  78. *
  79. * gets its value from an `NC_` prefixed environment variable
  80. * if it doesn't exist from config.php
  81. * if this doesn't exist either, it will return the given `$default`
  82. *
  83. * @param string $key key
  84. * @param mixed $default = null default value
  85. * @return mixed the value or $default
  86. */
  87. public function getValue($key, $default = null) {
  88. $envKey = self::ENV_PREFIX . $key;
  89. if (isset($this->envCache[$envKey])) {
  90. return $this->envCache[$envKey];
  91. }
  92. if (isset($this->cache[$key])) {
  93. return $this->cache[$key];
  94. }
  95. return $default;
  96. }
  97. /**
  98. * Sets and deletes values and writes the config.php
  99. *
  100. * @param array $configs Associative array with `key => value` pairs
  101. * If value is null, the config key will be deleted
  102. */
  103. public function setValues(array $configs) {
  104. $needsUpdate = false;
  105. foreach ($configs as $key => $value) {
  106. if ($value !== null) {
  107. $needsUpdate |= $this->set($key, $value);
  108. } else {
  109. $needsUpdate |= $this->delete($key);
  110. }
  111. }
  112. if ($needsUpdate) {
  113. // Write changes
  114. $this->writeData();
  115. }
  116. }
  117. /**
  118. * Sets the value and writes it to config.php if required
  119. *
  120. * @param string $key key
  121. * @param mixed $value value
  122. */
  123. public function setValue($key, $value) {
  124. if ($this->set($key, $value)) {
  125. // Write changes
  126. $this->writeData();
  127. }
  128. }
  129. /**
  130. * This function sets the value
  131. *
  132. * @param string $key key
  133. * @param mixed $value value
  134. * @return bool True if the file needs to be updated, false otherwise
  135. */
  136. protected function set($key, $value) {
  137. if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
  138. // Add change
  139. $this->cache[$key] = $value;
  140. return true;
  141. }
  142. return false;
  143. }
  144. /**
  145. * Removes a key from the config and removes it from config.php if required
  146. * @param string $key
  147. */
  148. public function deleteKey($key) {
  149. if ($this->delete($key)) {
  150. // Write changes
  151. $this->writeData();
  152. }
  153. }
  154. /**
  155. * This function removes a key from the config
  156. *
  157. * @param string $key
  158. * @return bool True if the file needs to be updated, false otherwise
  159. */
  160. protected function delete($key) {
  161. if (isset($this->cache[$key])) {
  162. // Delete key from cache
  163. unset($this->cache[$key]);
  164. return true;
  165. }
  166. return false;
  167. }
  168. /**
  169. * Loads the config file
  170. *
  171. * Reads the config file and saves it to the cache
  172. *
  173. * @throws \Exception If no lock could be acquired or the config file has not been found
  174. */
  175. private function readData() {
  176. // Default config should always get loaded
  177. $configFiles = [$this->configFilePath];
  178. // Add all files in the config dir ending with the same file name
  179. $extra = glob($this->configDir.'*.'.$this->configFileName);
  180. if (is_array($extra)) {
  181. natsort($extra);
  182. $configFiles = array_merge($configFiles, $extra);
  183. }
  184. // Include file and merge config
  185. foreach ($configFiles as $file) {
  186. $fileExistsAndIsReadable = file_exists($file) && is_readable($file);
  187. $filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
  188. if ($file === $this->configFilePath &&
  189. $filePointer === false) {
  190. // Opening the main config might not be possible, e.g. if the wrong
  191. // permissions are set (likely on a new installation)
  192. continue;
  193. }
  194. // Try to acquire a file lock
  195. if (!flock($filePointer, LOCK_SH)) {
  196. throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
  197. }
  198. unset($CONFIG);
  199. include $file;
  200. if (isset($CONFIG) && is_array($CONFIG)) {
  201. $this->cache = array_merge($this->cache, $CONFIG);
  202. }
  203. // Close the file pointer and release the lock
  204. flock($filePointer, LOCK_UN);
  205. fclose($filePointer);
  206. }
  207. $this->envCache = getenv();
  208. }
  209. /**
  210. * Writes the config file
  211. *
  212. * Saves the config to the config file.
  213. *
  214. * @throws HintException If the config file cannot be written to
  215. * @throws \Exception If no file lock can be acquired
  216. */
  217. private function writeData() {
  218. // Create a php file ...
  219. $content = "<?php\n";
  220. $content .= '$CONFIG = ';
  221. $content .= var_export($this->cache, true);
  222. $content .= ";\n";
  223. touch($this->configFilePath);
  224. $filePointer = fopen($this->configFilePath, 'r+');
  225. // Prevent others not to read the config
  226. chmod($this->configFilePath, 0640);
  227. // File does not exist, this can happen when doing a fresh install
  228. if (!is_resource($filePointer)) {
  229. throw new HintException(
  230. "Can't write into config directory!",
  231. 'This can usually be fixed by giving the webserver write access to the config directory.');
  232. }
  233. // Try to acquire a file lock
  234. if (!flock($filePointer, LOCK_EX)) {
  235. throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
  236. }
  237. // Write the config and release the lock
  238. ftruncate($filePointer, 0);
  239. fwrite($filePointer, $content);
  240. fflush($filePointer);
  241. flock($filePointer, LOCK_UN);
  242. fclose($filePointer);
  243. if (function_exists('opcache_invalidate')) {
  244. @opcache_invalidate($this->configFilePath, true);
  245. }
  246. }
  247. }