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