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