Passed
Push — master ( 10aa22...f23c21 )
by Morris
13:04 queued 10s
created
apps/federation/lib/DbHandler.php 2 patches
Indentation   +291 added lines, -291 removed lines patch added patch discarded remove patch
@@ -42,295 +42,295 @@
 block discarded – undo
42 42
  */
43 43
 class DbHandler {
44 44
 
45
-	/** @var  IDBConnection */
46
-	private $connection;
47
-
48
-	/** @var  IL10N */
49
-	private $IL10N;
50
-
51
-	/** @var string  */
52
-	private $dbTable = 'trusted_servers';
53
-
54
-	/**
55
-	 * @param IDBConnection $connection
56
-	 * @param IL10N $il10n
57
-	 */
58
-	public function __construct(
59
-		IDBConnection $connection,
60
-		IL10N $il10n
61
-	) {
62
-		$this->connection = $connection;
63
-		$this->IL10N = $il10n;
64
-	}
65
-
66
-	/**
67
-	 * add server to the list of trusted servers
68
-	 *
69
-	 * @param string $url
70
-	 * @return int
71
-	 * @throws HintException
72
-	 */
73
-	public function addServer($url) {
74
-		$hash = $this->hash($url);
75
-		$url = rtrim($url, '/');
76
-		$query = $this->connection->getQueryBuilder();
77
-		$query->insert($this->dbTable)
78
-			->values(
79
-				[
80
-					'url' => $query->createParameter('url'),
81
-					'url_hash' => $query->createParameter('url_hash'),
82
-				]
83
-			)
84
-			->setParameter('url', $url)
85
-			->setParameter('url_hash', $hash);
86
-
87
-		$result = $query->execute();
88
-
89
-		if ($result) {
90
-			return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
91
-		}
92
-
93
-		$message = 'Internal failure, Could not add trusted server: ' . $url;
94
-		$message_t = $this->IL10N->t('Could not add server');
95
-		throw new HintException($message, $message_t);
96
-	}
97
-
98
-	/**
99
-	 * remove server from the list of trusted servers
100
-	 *
101
-	 * @param int $id
102
-	 */
103
-	public function removeServer($id) {
104
-		$query = $this->connection->getQueryBuilder();
105
-		$query->delete($this->dbTable)
106
-			->where($query->expr()->eq('id', $query->createParameter('id')))
107
-			->setParameter('id', $id);
108
-		$query->execute();
109
-	}
110
-
111
-	/**
112
-	 * get trusted server with given ID
113
-	 *
114
-	 * @param int $id
115
-	 * @return array
116
-	 * @throws \Exception
117
-	 */
118
-	public function getServerById($id) {
119
-		$query = $this->connection->getQueryBuilder();
120
-		$query->select('*')->from($this->dbTable)
121
-			->where($query->expr()->eq('id', $query->createParameter('id')))
122
-			->setParameter('id', $id);
123
-
124
-		$qResult = $query->execute();
125
-		$result = $qResult->fetchAll();
126
-		$qResult->closeCursor();
127
-
128
-		if (empty($result)) {
129
-			throw new \Exception('No Server found with ID: ' . $id);
130
-		}
131
-
132
-		return $result[0];
133
-	}
134
-
135
-	/**
136
-	 * get all trusted servers
137
-	 *
138
-	 * @return array
139
-	 */
140
-	public function getAllServer() {
141
-		$query = $this->connection->getQueryBuilder();
142
-		$query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
143
-			->from($this->dbTable);
144
-		$statement = $query->execute();
145
-		$result = $statement->fetchAll();
146
-		$statement->closeCursor();
147
-		return $result;
148
-	}
149
-
150
-	/**
151
-	 * check if server already exists in the database table
152
-	 *
153
-	 * @param string $url
154
-	 * @return bool
155
-	 */
156
-	public function serverExists($url) {
157
-		$hash = $this->hash($url);
158
-		$query = $this->connection->getQueryBuilder();
159
-		$query->select('url')
160
-			->from($this->dbTable)
161
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
162
-			->setParameter('url_hash', $hash);
163
-		$statement = $query->execute();
164
-		$result = $statement->fetchAll();
165
-		$statement->closeCursor();
166
-
167
-		return !empty($result);
168
-	}
169
-
170
-	/**
171
-	 * write token to database. Token is used to exchange the secret
172
-	 *
173
-	 * @param string $url
174
-	 * @param string $token
175
-	 */
176
-	public function addToken($url, $token) {
177
-		$hash = $this->hash($url);
178
-		$query = $this->connection->getQueryBuilder();
179
-		$query->update($this->dbTable)
180
-			->set('token', $query->createParameter('token'))
181
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
182
-			->setParameter('url_hash', $hash)
183
-			->setParameter('token', $token);
184
-		$query->execute();
185
-	}
186
-
187
-	/**
188
-	 * get token stored in database
189
-	 *
190
-	 * @param string $url
191
-	 * @return string
192
-	 * @throws \Exception
193
-	 */
194
-	public function getToken($url) {
195
-		$hash = $this->hash($url);
196
-		$query = $this->connection->getQueryBuilder();
197
-		$query->select('token')->from($this->dbTable)
198
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
199
-			->setParameter('url_hash', $hash);
200
-
201
-		$statement = $query->execute();
202
-		$result = $statement->fetch();
203
-		$statement->closeCursor();
204
-
205
-		if (!isset($result['token'])) {
206
-			throw new \Exception('No token found for: ' . $url);
207
-		}
208
-
209
-		return $result['token'];
210
-	}
211
-
212
-	/**
213
-	 * add shared Secret to database
214
-	 *
215
-	 * @param string $url
216
-	 * @param string $sharedSecret
217
-	 */
218
-	public function addSharedSecret($url, $sharedSecret) {
219
-		$hash = $this->hash($url);
220
-		$query = $this->connection->getQueryBuilder();
221
-		$query->update($this->dbTable)
222
-			->set('shared_secret', $query->createParameter('sharedSecret'))
223
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
224
-			->setParameter('url_hash', $hash)
225
-			->setParameter('sharedSecret', $sharedSecret);
226
-		$query->execute();
227
-	}
228
-
229
-	/**
230
-	 * get shared secret from database
231
-	 *
232
-	 * @param string $url
233
-	 * @return string
234
-	 */
235
-	public function getSharedSecret($url) {
236
-		$hash = $this->hash($url);
237
-		$query = $this->connection->getQueryBuilder();
238
-		$query->select('shared_secret')->from($this->dbTable)
239
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
240
-			->setParameter('url_hash', $hash);
241
-
242
-		$statement = $query->execute();
243
-		$result = $statement->fetch();
244
-		$statement->closeCursor();
245
-		return $result['shared_secret'];
246
-	}
247
-
248
-	/**
249
-	 * set server status
250
-	 *
251
-	 * @param string $url
252
-	 * @param int $status
253
-	 * @param string|null $token
254
-	 */
255
-	public function setServerStatus($url, $status, $token = null) {
256
-		$hash = $this->hash($url);
257
-		$query = $this->connection->getQueryBuilder();
258
-		$query->update($this->dbTable)
259
-				->set('status', $query->createNamedParameter($status))
260
-				->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
261
-		if (!is_null($token)) {
262
-			$query->set('sync_token', $query->createNamedParameter($token));
263
-		}
264
-		$query->execute();
265
-	}
266
-
267
-	/**
268
-	 * get server status
269
-	 *
270
-	 * @param string $url
271
-	 * @return int
272
-	 */
273
-	public function getServerStatus($url) {
274
-		$hash = $this->hash($url);
275
-		$query = $this->connection->getQueryBuilder();
276
-		$query->select('status')->from($this->dbTable)
277
-				->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
278
-				->setParameter('url_hash', $hash);
279
-
280
-		$statement = $query->execute();
281
-		$result = $statement->fetch();
282
-		$statement->closeCursor();
283
-		return (int)$result['status'];
284
-	}
285
-
286
-	/**
287
-	 * create hash from URL
288
-	 *
289
-	 * @param string $url
290
-	 * @return string
291
-	 */
292
-	protected function hash($url) {
293
-		$normalized = $this->normalizeUrl($url);
294
-		return sha1($normalized);
295
-	}
296
-
297
-	/**
298
-	 * normalize URL, used to create the sha1 hash
299
-	 *
300
-	 * @param string $url
301
-	 * @return string
302
-	 */
303
-	protected function normalizeUrl($url) {
304
-		$normalized = $url;
305
-
306
-		if (strpos($url, 'https://') === 0) {
307
-			$normalized = substr($url, strlen('https://'));
308
-		} elseif (strpos($url, 'http://') === 0) {
309
-			$normalized = substr($url, strlen('http://'));
310
-		}
311
-
312
-		$normalized = Filesystem::normalizePath($normalized);
313
-		$normalized = trim($normalized, '/');
314
-
315
-		return $normalized;
316
-	}
317
-
318
-	/**
319
-	 * @param $username
320
-	 * @param $password
321
-	 * @return bool
322
-	 */
323
-	public function auth($username, $password) {
324
-		if ($username !== 'system') {
325
-			return false;
326
-		}
327
-		$query = $this->connection->getQueryBuilder();
328
-		$query->select('url')->from($this->dbTable)
329
-				->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
330
-
331
-		$statement = $query->execute();
332
-		$result = $statement->fetch();
333
-		$statement->closeCursor();
334
-		return !empty($result);
335
-	}
45
+    /** @var  IDBConnection */
46
+    private $connection;
47
+
48
+    /** @var  IL10N */
49
+    private $IL10N;
50
+
51
+    /** @var string  */
52
+    private $dbTable = 'trusted_servers';
53
+
54
+    /**
55
+     * @param IDBConnection $connection
56
+     * @param IL10N $il10n
57
+     */
58
+    public function __construct(
59
+        IDBConnection $connection,
60
+        IL10N $il10n
61
+    ) {
62
+        $this->connection = $connection;
63
+        $this->IL10N = $il10n;
64
+    }
65
+
66
+    /**
67
+     * add server to the list of trusted servers
68
+     *
69
+     * @param string $url
70
+     * @return int
71
+     * @throws HintException
72
+     */
73
+    public function addServer($url) {
74
+        $hash = $this->hash($url);
75
+        $url = rtrim($url, '/');
76
+        $query = $this->connection->getQueryBuilder();
77
+        $query->insert($this->dbTable)
78
+            ->values(
79
+                [
80
+                    'url' => $query->createParameter('url'),
81
+                    'url_hash' => $query->createParameter('url_hash'),
82
+                ]
83
+            )
84
+            ->setParameter('url', $url)
85
+            ->setParameter('url_hash', $hash);
86
+
87
+        $result = $query->execute();
88
+
89
+        if ($result) {
90
+            return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
91
+        }
92
+
93
+        $message = 'Internal failure, Could not add trusted server: ' . $url;
94
+        $message_t = $this->IL10N->t('Could not add server');
95
+        throw new HintException($message, $message_t);
96
+    }
97
+
98
+    /**
99
+     * remove server from the list of trusted servers
100
+     *
101
+     * @param int $id
102
+     */
103
+    public function removeServer($id) {
104
+        $query = $this->connection->getQueryBuilder();
105
+        $query->delete($this->dbTable)
106
+            ->where($query->expr()->eq('id', $query->createParameter('id')))
107
+            ->setParameter('id', $id);
108
+        $query->execute();
109
+    }
110
+
111
+    /**
112
+     * get trusted server with given ID
113
+     *
114
+     * @param int $id
115
+     * @return array
116
+     * @throws \Exception
117
+     */
118
+    public function getServerById($id) {
119
+        $query = $this->connection->getQueryBuilder();
120
+        $query->select('*')->from($this->dbTable)
121
+            ->where($query->expr()->eq('id', $query->createParameter('id')))
122
+            ->setParameter('id', $id);
123
+
124
+        $qResult = $query->execute();
125
+        $result = $qResult->fetchAll();
126
+        $qResult->closeCursor();
127
+
128
+        if (empty($result)) {
129
+            throw new \Exception('No Server found with ID: ' . $id);
130
+        }
131
+
132
+        return $result[0];
133
+    }
134
+
135
+    /**
136
+     * get all trusted servers
137
+     *
138
+     * @return array
139
+     */
140
+    public function getAllServer() {
141
+        $query = $this->connection->getQueryBuilder();
142
+        $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
143
+            ->from($this->dbTable);
144
+        $statement = $query->execute();
145
+        $result = $statement->fetchAll();
146
+        $statement->closeCursor();
147
+        return $result;
148
+    }
149
+
150
+    /**
151
+     * check if server already exists in the database table
152
+     *
153
+     * @param string $url
154
+     * @return bool
155
+     */
156
+    public function serverExists($url) {
157
+        $hash = $this->hash($url);
158
+        $query = $this->connection->getQueryBuilder();
159
+        $query->select('url')
160
+            ->from($this->dbTable)
161
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
162
+            ->setParameter('url_hash', $hash);
163
+        $statement = $query->execute();
164
+        $result = $statement->fetchAll();
165
+        $statement->closeCursor();
166
+
167
+        return !empty($result);
168
+    }
169
+
170
+    /**
171
+     * write token to database. Token is used to exchange the secret
172
+     *
173
+     * @param string $url
174
+     * @param string $token
175
+     */
176
+    public function addToken($url, $token) {
177
+        $hash = $this->hash($url);
178
+        $query = $this->connection->getQueryBuilder();
179
+        $query->update($this->dbTable)
180
+            ->set('token', $query->createParameter('token'))
181
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
182
+            ->setParameter('url_hash', $hash)
183
+            ->setParameter('token', $token);
184
+        $query->execute();
185
+    }
186
+
187
+    /**
188
+     * get token stored in database
189
+     *
190
+     * @param string $url
191
+     * @return string
192
+     * @throws \Exception
193
+     */
194
+    public function getToken($url) {
195
+        $hash = $this->hash($url);
196
+        $query = $this->connection->getQueryBuilder();
197
+        $query->select('token')->from($this->dbTable)
198
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
199
+            ->setParameter('url_hash', $hash);
200
+
201
+        $statement = $query->execute();
202
+        $result = $statement->fetch();
203
+        $statement->closeCursor();
204
+
205
+        if (!isset($result['token'])) {
206
+            throw new \Exception('No token found for: ' . $url);
207
+        }
208
+
209
+        return $result['token'];
210
+    }
211
+
212
+    /**
213
+     * add shared Secret to database
214
+     *
215
+     * @param string $url
216
+     * @param string $sharedSecret
217
+     */
218
+    public function addSharedSecret($url, $sharedSecret) {
219
+        $hash = $this->hash($url);
220
+        $query = $this->connection->getQueryBuilder();
221
+        $query->update($this->dbTable)
222
+            ->set('shared_secret', $query->createParameter('sharedSecret'))
223
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
224
+            ->setParameter('url_hash', $hash)
225
+            ->setParameter('sharedSecret', $sharedSecret);
226
+        $query->execute();
227
+    }
228
+
229
+    /**
230
+     * get shared secret from database
231
+     *
232
+     * @param string $url
233
+     * @return string
234
+     */
235
+    public function getSharedSecret($url) {
236
+        $hash = $this->hash($url);
237
+        $query = $this->connection->getQueryBuilder();
238
+        $query->select('shared_secret')->from($this->dbTable)
239
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
240
+            ->setParameter('url_hash', $hash);
241
+
242
+        $statement = $query->execute();
243
+        $result = $statement->fetch();
244
+        $statement->closeCursor();
245
+        return $result['shared_secret'];
246
+    }
247
+
248
+    /**
249
+     * set server status
250
+     *
251
+     * @param string $url
252
+     * @param int $status
253
+     * @param string|null $token
254
+     */
255
+    public function setServerStatus($url, $status, $token = null) {
256
+        $hash = $this->hash($url);
257
+        $query = $this->connection->getQueryBuilder();
258
+        $query->update($this->dbTable)
259
+                ->set('status', $query->createNamedParameter($status))
260
+                ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
261
+        if (!is_null($token)) {
262
+            $query->set('sync_token', $query->createNamedParameter($token));
263
+        }
264
+        $query->execute();
265
+    }
266
+
267
+    /**
268
+     * get server status
269
+     *
270
+     * @param string $url
271
+     * @return int
272
+     */
273
+    public function getServerStatus($url) {
274
+        $hash = $this->hash($url);
275
+        $query = $this->connection->getQueryBuilder();
276
+        $query->select('status')->from($this->dbTable)
277
+                ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
278
+                ->setParameter('url_hash', $hash);
279
+
280
+        $statement = $query->execute();
281
+        $result = $statement->fetch();
282
+        $statement->closeCursor();
283
+        return (int)$result['status'];
284
+    }
285
+
286
+    /**
287
+     * create hash from URL
288
+     *
289
+     * @param string $url
290
+     * @return string
291
+     */
292
+    protected function hash($url) {
293
+        $normalized = $this->normalizeUrl($url);
294
+        return sha1($normalized);
295
+    }
296
+
297
+    /**
298
+     * normalize URL, used to create the sha1 hash
299
+     *
300
+     * @param string $url
301
+     * @return string
302
+     */
303
+    protected function normalizeUrl($url) {
304
+        $normalized = $url;
305
+
306
+        if (strpos($url, 'https://') === 0) {
307
+            $normalized = substr($url, strlen('https://'));
308
+        } elseif (strpos($url, 'http://') === 0) {
309
+            $normalized = substr($url, strlen('http://'));
310
+        }
311
+
312
+        $normalized = Filesystem::normalizePath($normalized);
313
+        $normalized = trim($normalized, '/');
314
+
315
+        return $normalized;
316
+    }
317
+
318
+    /**
319
+     * @param $username
320
+     * @param $password
321
+     * @return bool
322
+     */
323
+    public function auth($username, $password) {
324
+        if ($username !== 'system') {
325
+            return false;
326
+        }
327
+        $query = $this->connection->getQueryBuilder();
328
+        $query->select('url')->from($this->dbTable)
329
+                ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
330
+
331
+        $statement = $query->execute();
332
+        $result = $statement->fetch();
333
+        $statement->closeCursor();
334
+        return !empty($result);
335
+    }
336 336
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -87,10 +87,10 @@  discard block
 block discarded – undo
87 87
 		$result = $query->execute();
88 88
 
89 89
 		if ($result) {
90
-			return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
90
+			return (int) $this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
91 91
 		}
92 92
 
93
-		$message = 'Internal failure, Could not add trusted server: ' . $url;
93
+		$message = 'Internal failure, Could not add trusted server: '.$url;
94 94
 		$message_t = $this->IL10N->t('Could not add server');
95 95
 		throw new HintException($message, $message_t);
96 96
 	}
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 		$qResult->closeCursor();
127 127
 
128 128
 		if (empty($result)) {
129
-			throw new \Exception('No Server found with ID: ' . $id);
129
+			throw new \Exception('No Server found with ID: '.$id);
130 130
 		}
131 131
 
132 132
 		return $result[0];
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 		$statement->closeCursor();
204 204
 
205 205
 		if (!isset($result['token'])) {
206
-			throw new \Exception('No token found for: ' . $url);
206
+			throw new \Exception('No token found for: '.$url);
207 207
 		}
208 208
 
209 209
 		return $result['token'];
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
 		$statement = $query->execute();
281 281
 		$result = $statement->fetch();
282 282
 		$statement->closeCursor();
283
-		return (int)$result['status'];
283
+		return (int) $result['status'];
284 284
 	}
285 285
 
286 286
 	/**
Please login to merge, or discard this patch.