Config.php 7.5 KB

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