Passed
Push — master ( 71e530...2e803d )
by Morris
24:58 queued 14:06
created
lib/private/AppConfig.php 1 patch
Indentation   +283 added lines, -283 removed lines patch added patch discarded remove patch
@@ -41,291 +41,291 @@
 block discarded – undo
41 41
  */
42 42
 class AppConfig implements IAppConfig {
43 43
 
44
-	/** @var array[] */
45
-	protected $sensitiveValues = [
46
-		'spreed' => [
47
-			'/^turn_server_secret$/',
48
-		],
49
-		'user_ldap' => [
50
-			'/^(s..)?ldap_agent_password$/',
51
-		],
52
-	];
53
-
54
-	/** @var \OCP\IDBConnection */
55
-	protected $conn;
56
-
57
-	/** @var array[] */
58
-	private $cache = [];
59
-
60
-	/** @var bool */
61
-	private $configLoaded = false;
62
-
63
-	/**
64
-	 * @param IDBConnection $conn
65
-	 */
66
-	public function __construct(IDBConnection $conn) {
67
-		$this->conn = $conn;
68
-		$this->configLoaded = false;
69
-	}
70
-
71
-	/**
72
-	 * @param string $app
73
-	 * @return array
74
-	 */
75
-	private function getAppValues($app) {
76
-		$this->loadConfigValues();
77
-
78
-		if (isset($this->cache[$app])) {
79
-			return $this->cache[$app];
80
-		}
81
-
82
-		return [];
83
-	}
84
-
85
-	/**
86
-	 * Get all apps using the config
87
-	 *
88
-	 * @return array an array of app ids
89
-	 *
90
-	 * This function returns a list of all apps that have at least one
91
-	 * entry in the appconfig table.
92
-	 */
93
-	public function getApps() {
94
-		$this->loadConfigValues();
95
-
96
-		return $this->getSortedKeys($this->cache);
97
-	}
98
-
99
-	/**
100
-	 * Get the available keys for an app
101
-	 *
102
-	 * @param string $app the app we are looking for
103
-	 * @return array an array of key names
104
-	 *
105
-	 * This function gets all keys of an app. Please note that the values are
106
-	 * not returned.
107
-	 */
108
-	public function getKeys($app) {
109
-		$this->loadConfigValues();
110
-
111
-		if (isset($this->cache[$app])) {
112
-			return $this->getSortedKeys($this->cache[$app]);
113
-		}
114
-
115
-		return [];
116
-	}
117
-
118
-	public function getSortedKeys($data) {
119
-		$keys = array_keys($data);
120
-		sort($keys);
121
-		return $keys;
122
-	}
123
-
124
-	/**
125
-	 * Gets the config value
126
-	 *
127
-	 * @param string $app app
128
-	 * @param string $key key
129
-	 * @param string $default = null, default value if the key does not exist
130
-	 * @return string the value or $default
131
-	 *
132
-	 * This function gets a value from the appconfig table. If the key does
133
-	 * not exist the default value will be returned
134
-	 */
135
-	public function getValue($app, $key, $default = null) {
136
-		$this->loadConfigValues();
137
-
138
-		if ($this->hasKey($app, $key)) {
139
-			return $this->cache[$app][$key];
140
-		}
141
-
142
-		return $default;
143
-	}
144
-
145
-	/**
146
-	 * check if a key is set in the appconfig
147
-	 *
148
-	 * @param string $app
149
-	 * @param string $key
150
-	 * @return bool
151
-	 */
152
-	public function hasKey($app, $key) {
153
-		$this->loadConfigValues();
154
-
155
-		return isset($this->cache[$app][$key]);
156
-	}
157
-
158
-	/**
159
-	 * Sets a value. If the key did not exist before it will be created.
160
-	 *
161
-	 * @param string $app app
162
-	 * @param string $key key
163
-	 * @param string|float|int $value value
164
-	 * @return bool True if the value was inserted or updated, false if the value was the same
165
-	 */
166
-	public function setValue($app, $key, $value) {
167
-		if (!$this->hasKey($app, $key)) {
168
-			$inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
169
-				'appid' => $app,
170
-				'configkey' => $key,
171
-				'configvalue' => $value,
172
-			], [
173
-				'appid',
174
-				'configkey',
175
-			]);
176
-
177
-			if ($inserted) {
178
-				if (!isset($this->cache[$app])) {
179
-					$this->cache[$app] = [];
180
-				}
181
-
182
-				$this->cache[$app][$key] = $value;
183
-				return true;
184
-			}
185
-		}
186
-
187
-		$sql = $this->conn->getQueryBuilder();
188
-		$sql->update('appconfig')
189
-			->set('configvalue', $sql->createParameter('configvalue'))
190
-			->where($sql->expr()->eq('appid', $sql->createParameter('app')))
191
-			->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
192
-			->setParameter('configvalue', $value)
193
-			->setParameter('app', $app)
194
-			->setParameter('configkey', $key);
195
-
196
-		/*
44
+    /** @var array[] */
45
+    protected $sensitiveValues = [
46
+        'spreed' => [
47
+            '/^turn_server_secret$/',
48
+        ],
49
+        'user_ldap' => [
50
+            '/^(s..)?ldap_agent_password$/',
51
+        ],
52
+    ];
53
+
54
+    /** @var \OCP\IDBConnection */
55
+    protected $conn;
56
+
57
+    /** @var array[] */
58
+    private $cache = [];
59
+
60
+    /** @var bool */
61
+    private $configLoaded = false;
62
+
63
+    /**
64
+     * @param IDBConnection $conn
65
+     */
66
+    public function __construct(IDBConnection $conn) {
67
+        $this->conn = $conn;
68
+        $this->configLoaded = false;
69
+    }
70
+
71
+    /**
72
+     * @param string $app
73
+     * @return array
74
+     */
75
+    private function getAppValues($app) {
76
+        $this->loadConfigValues();
77
+
78
+        if (isset($this->cache[$app])) {
79
+            return $this->cache[$app];
80
+        }
81
+
82
+        return [];
83
+    }
84
+
85
+    /**
86
+     * Get all apps using the config
87
+     *
88
+     * @return array an array of app ids
89
+     *
90
+     * This function returns a list of all apps that have at least one
91
+     * entry in the appconfig table.
92
+     */
93
+    public function getApps() {
94
+        $this->loadConfigValues();
95
+
96
+        return $this->getSortedKeys($this->cache);
97
+    }
98
+
99
+    /**
100
+     * Get the available keys for an app
101
+     *
102
+     * @param string $app the app we are looking for
103
+     * @return array an array of key names
104
+     *
105
+     * This function gets all keys of an app. Please note that the values are
106
+     * not returned.
107
+     */
108
+    public function getKeys($app) {
109
+        $this->loadConfigValues();
110
+
111
+        if (isset($this->cache[$app])) {
112
+            return $this->getSortedKeys($this->cache[$app]);
113
+        }
114
+
115
+        return [];
116
+    }
117
+
118
+    public function getSortedKeys($data) {
119
+        $keys = array_keys($data);
120
+        sort($keys);
121
+        return $keys;
122
+    }
123
+
124
+    /**
125
+     * Gets the config value
126
+     *
127
+     * @param string $app app
128
+     * @param string $key key
129
+     * @param string $default = null, default value if the key does not exist
130
+     * @return string the value or $default
131
+     *
132
+     * This function gets a value from the appconfig table. If the key does
133
+     * not exist the default value will be returned
134
+     */
135
+    public function getValue($app, $key, $default = null) {
136
+        $this->loadConfigValues();
137
+
138
+        if ($this->hasKey($app, $key)) {
139
+            return $this->cache[$app][$key];
140
+        }
141
+
142
+        return $default;
143
+    }
144
+
145
+    /**
146
+     * check if a key is set in the appconfig
147
+     *
148
+     * @param string $app
149
+     * @param string $key
150
+     * @return bool
151
+     */
152
+    public function hasKey($app, $key) {
153
+        $this->loadConfigValues();
154
+
155
+        return isset($this->cache[$app][$key]);
156
+    }
157
+
158
+    /**
159
+     * Sets a value. If the key did not exist before it will be created.
160
+     *
161
+     * @param string $app app
162
+     * @param string $key key
163
+     * @param string|float|int $value value
164
+     * @return bool True if the value was inserted or updated, false if the value was the same
165
+     */
166
+    public function setValue($app, $key, $value) {
167
+        if (!$this->hasKey($app, $key)) {
168
+            $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
169
+                'appid' => $app,
170
+                'configkey' => $key,
171
+                'configvalue' => $value,
172
+            ], [
173
+                'appid',
174
+                'configkey',
175
+            ]);
176
+
177
+            if ($inserted) {
178
+                if (!isset($this->cache[$app])) {
179
+                    $this->cache[$app] = [];
180
+                }
181
+
182
+                $this->cache[$app][$key] = $value;
183
+                return true;
184
+            }
185
+        }
186
+
187
+        $sql = $this->conn->getQueryBuilder();
188
+        $sql->update('appconfig')
189
+            ->set('configvalue', $sql->createParameter('configvalue'))
190
+            ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
191
+            ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
192
+            ->setParameter('configvalue', $value)
193
+            ->setParameter('app', $app)
194
+            ->setParameter('configkey', $key);
195
+
196
+        /*
197 197
 		 * Only limit to the existing value for non-Oracle DBs:
198 198
 		 * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286
199 199
 		 * > Large objects (LOBs) are not supported in comparison conditions.
200 200
 		 */
201
-		if (!($this->conn instanceof OracleConnection)) {
202
-			// Only update the value when it is not the same
203
-			$sql->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))
204
-				->setParameter('configvalue', $value);
205
-		}
206
-
207
-		$changedRow = (bool) $sql->execute();
208
-
209
-		$this->cache[$app][$key] = $value;
210
-
211
-		return $changedRow;
212
-	}
213
-
214
-	/**
215
-	 * Deletes a key
216
-	 *
217
-	 * @param string $app app
218
-	 * @param string $key key
219
-	 * @return boolean
220
-	 */
221
-	public function deleteKey($app, $key) {
222
-		$this->loadConfigValues();
223
-
224
-		$sql = $this->conn->getQueryBuilder();
225
-		$sql->delete('appconfig')
226
-			->where($sql->expr()->eq('appid', $sql->createParameter('app')))
227
-			->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
228
-			->setParameter('app', $app)
229
-			->setParameter('configkey', $key);
230
-		$sql->execute();
231
-
232
-		unset($this->cache[$app][$key]);
233
-		return false;
234
-	}
235
-
236
-	/**
237
-	 * Remove app from appconfig
238
-	 *
239
-	 * @param string $app app
240
-	 * @return boolean
241
-	 *
242
-	 * Removes all keys in appconfig belonging to the app.
243
-	 */
244
-	public function deleteApp($app) {
245
-		$this->loadConfigValues();
246
-
247
-		$sql = $this->conn->getQueryBuilder();
248
-		$sql->delete('appconfig')
249
-			->where($sql->expr()->eq('appid', $sql->createParameter('app')))
250
-			->setParameter('app', $app);
251
-		$sql->execute();
252
-
253
-		unset($this->cache[$app]);
254
-		return false;
255
-	}
256
-
257
-	/**
258
-	 * get multiple values, either the app or key can be used as wildcard by setting it to false
259
-	 *
260
-	 * @param string|false $app
261
-	 * @param string|false $key
262
-	 * @return array|false
263
-	 */
264
-	public function getValues($app, $key) {
265
-		if (($app !== false) === ($key !== false)) {
266
-			return false;
267
-		}
268
-
269
-		if ($key === false) {
270
-			return $this->getAppValues($app);
271
-		} else {
272
-			$appIds = $this->getApps();
273
-			$values = array_map(function($appId) use ($key) {
274
-				return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null;
275
-			}, $appIds);
276
-			$result = array_combine($appIds, $values);
277
-
278
-			return array_filter($result);
279
-		}
280
-	}
281
-
282
-	/**
283
-	 * get all values of the app or and filters out sensitive data
284
-	 *
285
-	 * @param string $app
286
-	 * @return array
287
-	 */
288
-	public function getFilteredValues($app) {
289
-		$values = $this->getValues($app, false);
290
-
291
-		if (isset($this->sensitiveValues[$app])) {
292
-			foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) {
293
-				$sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
294
-				foreach ($sensitiveKeys as $sensitiveKey) {
295
-					$values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
296
-				}
297
-			}
298
-		}
299
-
300
-		return $values;
301
-	}
302
-
303
-	/**
304
-	 * Load all the app config values
305
-	 */
306
-	protected function loadConfigValues() {
307
-		if ($this->configLoaded) {
308
-			return;
309
-		}
310
-
311
-		$this->cache = [];
312
-
313
-		$sql = $this->conn->getQueryBuilder();
314
-		$sql->select('*')
315
-			->from('appconfig');
316
-		$result = $sql->execute();
317
-
318
-		// we are going to store the result in memory anyway
319
-		$rows = $result->fetchAll();
320
-		foreach ($rows as $row) {
321
-			if (!isset($this->cache[$row['appid']])) {
322
-				$this->cache[$row['appid']] = [];
323
-			}
324
-
325
-			$this->cache[$row['appid']][$row['configkey']] = $row['configvalue'];
326
-		}
327
-		$result->closeCursor();
328
-
329
-		$this->configLoaded = true;
330
-	}
201
+        if (!($this->conn instanceof OracleConnection)) {
202
+            // Only update the value when it is not the same
203
+            $sql->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))
204
+                ->setParameter('configvalue', $value);
205
+        }
206
+
207
+        $changedRow = (bool) $sql->execute();
208
+
209
+        $this->cache[$app][$key] = $value;
210
+
211
+        return $changedRow;
212
+    }
213
+
214
+    /**
215
+     * Deletes a key
216
+     *
217
+     * @param string $app app
218
+     * @param string $key key
219
+     * @return boolean
220
+     */
221
+    public function deleteKey($app, $key) {
222
+        $this->loadConfigValues();
223
+
224
+        $sql = $this->conn->getQueryBuilder();
225
+        $sql->delete('appconfig')
226
+            ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
227
+            ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
228
+            ->setParameter('app', $app)
229
+            ->setParameter('configkey', $key);
230
+        $sql->execute();
231
+
232
+        unset($this->cache[$app][$key]);
233
+        return false;
234
+    }
235
+
236
+    /**
237
+     * Remove app from appconfig
238
+     *
239
+     * @param string $app app
240
+     * @return boolean
241
+     *
242
+     * Removes all keys in appconfig belonging to the app.
243
+     */
244
+    public function deleteApp($app) {
245
+        $this->loadConfigValues();
246
+
247
+        $sql = $this->conn->getQueryBuilder();
248
+        $sql->delete('appconfig')
249
+            ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
250
+            ->setParameter('app', $app);
251
+        $sql->execute();
252
+
253
+        unset($this->cache[$app]);
254
+        return false;
255
+    }
256
+
257
+    /**
258
+     * get multiple values, either the app or key can be used as wildcard by setting it to false
259
+     *
260
+     * @param string|false $app
261
+     * @param string|false $key
262
+     * @return array|false
263
+     */
264
+    public function getValues($app, $key) {
265
+        if (($app !== false) === ($key !== false)) {
266
+            return false;
267
+        }
268
+
269
+        if ($key === false) {
270
+            return $this->getAppValues($app);
271
+        } else {
272
+            $appIds = $this->getApps();
273
+            $values = array_map(function($appId) use ($key) {
274
+                return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null;
275
+            }, $appIds);
276
+            $result = array_combine($appIds, $values);
277
+
278
+            return array_filter($result);
279
+        }
280
+    }
281
+
282
+    /**
283
+     * get all values of the app or and filters out sensitive data
284
+     *
285
+     * @param string $app
286
+     * @return array
287
+     */
288
+    public function getFilteredValues($app) {
289
+        $values = $this->getValues($app, false);
290
+
291
+        if (isset($this->sensitiveValues[$app])) {
292
+            foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) {
293
+                $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
294
+                foreach ($sensitiveKeys as $sensitiveKey) {
295
+                    $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
296
+                }
297
+            }
298
+        }
299
+
300
+        return $values;
301
+    }
302
+
303
+    /**
304
+     * Load all the app config values
305
+     */
306
+    protected function loadConfigValues() {
307
+        if ($this->configLoaded) {
308
+            return;
309
+        }
310
+
311
+        $this->cache = [];
312
+
313
+        $sql = $this->conn->getQueryBuilder();
314
+        $sql->select('*')
315
+            ->from('appconfig');
316
+        $result = $sql->execute();
317
+
318
+        // we are going to store the result in memory anyway
319
+        $rows = $result->fetchAll();
320
+        foreach ($rows as $row) {
321
+            if (!isset($this->cache[$row['appid']])) {
322
+                $this->cache[$row['appid']] = [];
323
+            }
324
+
325
+            $this->cache[$row['appid']][$row['configkey']] = $row['configvalue'];
326
+        }
327
+        $result->closeCursor();
328
+
329
+        $this->configLoaded = true;
330
+    }
331 331
 }
Please login to merge, or discard this patch.