Completed
Push — master ( 0dae49...2dbd38 )
by Morris
18:25 queued 05:17
created
apps/files_sharing/lib/External/Manager.php 2 patches
Indentation   +384 added lines, -384 removed lines patch added patch discarded remove patch
@@ -38,441 +38,441 @@
 block discarded – undo
38 38
 use OCP\OCS\IDiscoveryService;
39 39
 
40 40
 class Manager {
41
-	const STORAGE = '\OCA\Files_Sharing\External\Storage';
42
-
43
-	/**
44
-	 * @var string
45
-	 */
46
-	private $uid;
47
-
48
-	/**
49
-	 * @var IDBConnection
50
-	 */
51
-	private $connection;
52
-
53
-	/**
54
-	 * @var \OC\Files\Mount\Manager
55
-	 */
56
-	private $mountManager;
57
-
58
-	/**
59
-	 * @var IStorageFactory
60
-	 */
61
-	private $storageLoader;
62
-
63
-	/**
64
-	 * @var IClientService
65
-	 */
66
-	private $clientService;
67
-
68
-	/**
69
-	 * @var IManager
70
-	 */
71
-	private $notificationManager;
72
-
73
-	/**
74
-	 * @var IDiscoveryService
75
-	 */
76
-	private $discoveryService;
77
-
78
-	/**
79
-	 * @param IDBConnection $connection
80
-	 * @param \OC\Files\Mount\Manager $mountManager
81
-	 * @param IStorageFactory $storageLoader
82
-	 * @param IClientService $clientService
83
-	 * @param IManager $notificationManager
84
-	 * @param IDiscoveryService $discoveryService
85
-	 * @param string $uid
86
-	 */
87
-	public function __construct(IDBConnection $connection,
88
-								\OC\Files\Mount\Manager $mountManager,
89
-								IStorageFactory $storageLoader,
90
-								IClientService $clientService,
91
-								IManager $notificationManager,
92
-								IDiscoveryService $discoveryService,
93
-								$uid) {
94
-		$this->connection = $connection;
95
-		$this->mountManager = $mountManager;
96
-		$this->storageLoader = $storageLoader;
97
-		$this->clientService = $clientService;
98
-		$this->uid = $uid;
99
-		$this->notificationManager = $notificationManager;
100
-		$this->discoveryService = $discoveryService;
101
-	}
102
-
103
-	/**
104
-	 * add new server-to-server share
105
-	 *
106
-	 * @param string $remote
107
-	 * @param string $token
108
-	 * @param string $password
109
-	 * @param string $name
110
-	 * @param string $owner
111
-	 * @param boolean $accepted
112
-	 * @param string $user
113
-	 * @param int $remoteId
114
-	 * @return Mount|null
115
-	 */
116
-	public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) {
117
-
118
-		$user = $user ? $user : $this->uid;
119
-		$accepted = $accepted ? 1 : 0;
120
-		$name = Filesystem::normalizePath('/' . $name);
121
-
122
-		if (!$accepted) {
123
-			// To avoid conflicts with the mount point generation later,
124
-			// we only use a temporary mount point name here. The real
125
-			// mount point name will be generated when accepting the share,
126
-			// using the original share item name.
127
-			$tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}';
128
-			$mountPoint = $tmpMountPointName;
129
-			$hash = md5($tmpMountPointName);
130
-			$data = [
131
-				'remote'		=> $remote,
132
-				'share_token'	=> $token,
133
-				'password'		=> $password,
134
-				'name'			=> $name,
135
-				'owner'			=> $owner,
136
-				'user'			=> $user,
137
-				'mountpoint'	=> $mountPoint,
138
-				'mountpoint_hash'	=> $hash,
139
-				'accepted'		=> $accepted,
140
-				'remote_id'		=> $remoteId,
141
-			];
142
-
143
-			$i = 1;
144
-			while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) {
145
-				// The external share already exists for the user
146
-				$data['mountpoint'] = $tmpMountPointName . '-' . $i;
147
-				$data['mountpoint_hash'] = md5($data['mountpoint']);
148
-				$i++;
149
-			}
150
-			return null;
151
-		}
152
-
153
-		$mountPoint = Files::buildNotExistingFileName('/', $name);
154
-		$mountPoint = Filesystem::normalizePath('/' . $mountPoint);
155
-		$hash = md5($mountPoint);
156
-
157
-		$query = $this->connection->prepare('
41
+    const STORAGE = '\OCA\Files_Sharing\External\Storage';
42
+
43
+    /**
44
+     * @var string
45
+     */
46
+    private $uid;
47
+
48
+    /**
49
+     * @var IDBConnection
50
+     */
51
+    private $connection;
52
+
53
+    /**
54
+     * @var \OC\Files\Mount\Manager
55
+     */
56
+    private $mountManager;
57
+
58
+    /**
59
+     * @var IStorageFactory
60
+     */
61
+    private $storageLoader;
62
+
63
+    /**
64
+     * @var IClientService
65
+     */
66
+    private $clientService;
67
+
68
+    /**
69
+     * @var IManager
70
+     */
71
+    private $notificationManager;
72
+
73
+    /**
74
+     * @var IDiscoveryService
75
+     */
76
+    private $discoveryService;
77
+
78
+    /**
79
+     * @param IDBConnection $connection
80
+     * @param \OC\Files\Mount\Manager $mountManager
81
+     * @param IStorageFactory $storageLoader
82
+     * @param IClientService $clientService
83
+     * @param IManager $notificationManager
84
+     * @param IDiscoveryService $discoveryService
85
+     * @param string $uid
86
+     */
87
+    public function __construct(IDBConnection $connection,
88
+                                \OC\Files\Mount\Manager $mountManager,
89
+                                IStorageFactory $storageLoader,
90
+                                IClientService $clientService,
91
+                                IManager $notificationManager,
92
+                                IDiscoveryService $discoveryService,
93
+                                $uid) {
94
+        $this->connection = $connection;
95
+        $this->mountManager = $mountManager;
96
+        $this->storageLoader = $storageLoader;
97
+        $this->clientService = $clientService;
98
+        $this->uid = $uid;
99
+        $this->notificationManager = $notificationManager;
100
+        $this->discoveryService = $discoveryService;
101
+    }
102
+
103
+    /**
104
+     * add new server-to-server share
105
+     *
106
+     * @param string $remote
107
+     * @param string $token
108
+     * @param string $password
109
+     * @param string $name
110
+     * @param string $owner
111
+     * @param boolean $accepted
112
+     * @param string $user
113
+     * @param int $remoteId
114
+     * @return Mount|null
115
+     */
116
+    public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) {
117
+
118
+        $user = $user ? $user : $this->uid;
119
+        $accepted = $accepted ? 1 : 0;
120
+        $name = Filesystem::normalizePath('/' . $name);
121
+
122
+        if (!$accepted) {
123
+            // To avoid conflicts with the mount point generation later,
124
+            // we only use a temporary mount point name here. The real
125
+            // mount point name will be generated when accepting the share,
126
+            // using the original share item name.
127
+            $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}';
128
+            $mountPoint = $tmpMountPointName;
129
+            $hash = md5($tmpMountPointName);
130
+            $data = [
131
+                'remote'		=> $remote,
132
+                'share_token'	=> $token,
133
+                'password'		=> $password,
134
+                'name'			=> $name,
135
+                'owner'			=> $owner,
136
+                'user'			=> $user,
137
+                'mountpoint'	=> $mountPoint,
138
+                'mountpoint_hash'	=> $hash,
139
+                'accepted'		=> $accepted,
140
+                'remote_id'		=> $remoteId,
141
+            ];
142
+
143
+            $i = 1;
144
+            while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) {
145
+                // The external share already exists for the user
146
+                $data['mountpoint'] = $tmpMountPointName . '-' . $i;
147
+                $data['mountpoint_hash'] = md5($data['mountpoint']);
148
+                $i++;
149
+            }
150
+            return null;
151
+        }
152
+
153
+        $mountPoint = Files::buildNotExistingFileName('/', $name);
154
+        $mountPoint = Filesystem::normalizePath('/' . $mountPoint);
155
+        $hash = md5($mountPoint);
156
+
157
+        $query = $this->connection->prepare('
158 158
 				INSERT INTO `*PREFIX*share_external`
159 159
 					(`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `accepted`, `remote_id`)
160 160
 				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
161 161
 			');
162
-		$query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId));
163
-
164
-		$options = array(
165
-			'remote'	=> $remote,
166
-			'token'		=> $token,
167
-			'password'	=> $password,
168
-			'mountpoint'	=> $mountPoint,
169
-			'owner'		=> $owner
170
-		);
171
-		return $this->mountShare($options);
172
-	}
173
-
174
-	/**
175
-	 * get share
176
-	 *
177
-	 * @param int $id share id
178
-	 * @return mixed share of false
179
-	 */
180
-	public function getShare($id) {
181
-		$getShare = $this->connection->prepare('
162
+        $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId));
163
+
164
+        $options = array(
165
+            'remote'	=> $remote,
166
+            'token'		=> $token,
167
+            'password'	=> $password,
168
+            'mountpoint'	=> $mountPoint,
169
+            'owner'		=> $owner
170
+        );
171
+        return $this->mountShare($options);
172
+    }
173
+
174
+    /**
175
+     * get share
176
+     *
177
+     * @param int $id share id
178
+     * @return mixed share of false
179
+     */
180
+    public function getShare($id) {
181
+        $getShare = $this->connection->prepare('
182 182
 			SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted`
183 183
 			FROM  `*PREFIX*share_external`
184 184
 			WHERE `id` = ? AND `user` = ?');
185
-		$result = $getShare->execute(array($id, $this->uid));
185
+        $result = $getShare->execute(array($id, $this->uid));
186 186
 
187
-		return $result ? $getShare->fetch() : false;
188
-	}
187
+        return $result ? $getShare->fetch() : false;
188
+    }
189 189
 
190
-	/**
191
-	 * accept server-to-server share
192
-	 *
193
-	 * @param int $id
194
-	 * @return bool True if the share could be accepted, false otherwise
195
-	 */
196
-	public function acceptShare($id) {
190
+    /**
191
+     * accept server-to-server share
192
+     *
193
+     * @param int $id
194
+     * @return bool True if the share could be accepted, false otherwise
195
+     */
196
+    public function acceptShare($id) {
197 197
 
198
-		$share = $this->getShare($id);
198
+        $share = $this->getShare($id);
199 199
 
200
-		if ($share) {
201
-			\OC_Util::setupFS($this->uid);
202
-			$mountPoint = Files::buildNotExistingFileName('/', $share['name']);
203
-			$mountPoint = Filesystem::normalizePath('/' . $mountPoint);
204
-			$hash = md5($mountPoint);
200
+        if ($share) {
201
+            \OC_Util::setupFS($this->uid);
202
+            $mountPoint = Files::buildNotExistingFileName('/', $share['name']);
203
+            $mountPoint = Filesystem::normalizePath('/' . $mountPoint);
204
+            $hash = md5($mountPoint);
205 205
 
206
-			$acceptShare = $this->connection->prepare('
206
+            $acceptShare = $this->connection->prepare('
207 207
 				UPDATE `*PREFIX*share_external`
208 208
 				SET `accepted` = ?,
209 209
 					`mountpoint` = ?,
210 210
 					`mountpoint_hash` = ?
211 211
 				WHERE `id` = ? AND `user` = ?');
212
-			$acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid));
213
-			$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept');
212
+            $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid));
213
+            $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept');
214 214
 
215
-			\OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]);
215
+            \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]);
216 216
 
217
-			$this->processNotification($id);
218
-			return true;
219
-		}
217
+            $this->processNotification($id);
218
+            return true;
219
+        }
220 220
 
221
-		return false;
222
-	}
221
+        return false;
222
+    }
223 223
 
224
-	/**
225
-	 * decline server-to-server share
226
-	 *
227
-	 * @param int $id
228
-	 * @return bool True if the share could be declined, false otherwise
229
-	 */
230
-	public function declineShare($id) {
224
+    /**
225
+     * decline server-to-server share
226
+     *
227
+     * @param int $id
228
+     * @return bool True if the share could be declined, false otherwise
229
+     */
230
+    public function declineShare($id) {
231 231
 
232
-		$share = $this->getShare($id);
232
+        $share = $this->getShare($id);
233 233
 
234
-		if ($share) {
235
-			$removeShare = $this->connection->prepare('
234
+        if ($share) {
235
+            $removeShare = $this->connection->prepare('
236 236
 				DELETE FROM `*PREFIX*share_external` WHERE `id` = ? AND `user` = ?');
237
-			$removeShare->execute(array($id, $this->uid));
238
-			$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
239
-
240
-			$this->processNotification($id);
241
-			return true;
242
-		}
243
-
244
-		return false;
245
-	}
246
-
247
-	/**
248
-	 * @param int $remoteShare
249
-	 */
250
-	public function processNotification($remoteShare) {
251
-		$filter = $this->notificationManager->createNotification();
252
-		$filter->setApp('files_sharing')
253
-			->setUser($this->uid)
254
-			->setObject('remote_share', (int) $remoteShare);
255
-		$this->notificationManager->markProcessed($filter);
256
-	}
257
-
258
-	/**
259
-	 * inform remote server whether server-to-server share was accepted/declined
260
-	 *
261
-	 * @param string $remote
262
-	 * @param string $token
263
-	 * @param int $remoteId Share id on the remote host
264
-	 * @param string $feedback
265
-	 * @return boolean
266
-	 */
267
-	private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) {
268
-
269
-		$federationEndpoints = $this->discoveryService->discover($remote, 'FEDERATED_SHARING');
270
-		$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
271
-
272
-		$url = rtrim($remote, '/') . $endpoint . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
273
-		$fields = array('token' => $token);
274
-
275
-		$client = $this->clientService->newClient();
276
-
277
-		try {
278
-			$response = $client->post(
279
-				$url,
280
-				[
281
-					'body' => $fields,
282
-					'connect_timeout' => 10,
283
-				]
284
-			);
285
-		} catch (\Exception $e) {
286
-			return false;
287
-		}
288
-
289
-		$status = json_decode($response->getBody(), true);
290
-
291
-		return ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
292
-	}
293
-
294
-	/**
295
-	 * remove '/user/files' from the path and trailing slashes
296
-	 *
297
-	 * @param string $path
298
-	 * @return string
299
-	 */
300
-	protected function stripPath($path) {
301
-		$prefix = '/' . $this->uid . '/files';
302
-		return rtrim(substr($path, strlen($prefix)), '/');
303
-	}
304
-
305
-	public function getMount($data) {
306
-		$data['manager'] = $this;
307
-		$mountPoint = '/' . $this->uid . '/files' . $data['mountpoint'];
308
-		$data['mountpoint'] = $mountPoint;
309
-		$data['certificateManager'] = \OC::$server->getCertificateManager($this->uid);
310
-		return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader);
311
-	}
312
-
313
-	/**
314
-	 * @param array $data
315
-	 * @return Mount
316
-	 */
317
-	protected function mountShare($data) {
318
-		$mount = $this->getMount($data);
319
-		$this->mountManager->addMount($mount);
320
-		return $mount;
321
-	}
322
-
323
-	/**
324
-	 * @return \OC\Files\Mount\Manager
325
-	 */
326
-	public function getMountManager() {
327
-		return $this->mountManager;
328
-	}
329
-
330
-	/**
331
-	 * @param string $source
332
-	 * @param string $target
333
-	 * @return bool
334
-	 */
335
-	public function setMountPoint($source, $target) {
336
-		$source = $this->stripPath($source);
337
-		$target = $this->stripPath($target);
338
-		$sourceHash = md5($source);
339
-		$targetHash = md5($target);
340
-
341
-		$query = $this->connection->prepare('
237
+            $removeShare->execute(array($id, $this->uid));
238
+            $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
239
+
240
+            $this->processNotification($id);
241
+            return true;
242
+        }
243
+
244
+        return false;
245
+    }
246
+
247
+    /**
248
+     * @param int $remoteShare
249
+     */
250
+    public function processNotification($remoteShare) {
251
+        $filter = $this->notificationManager->createNotification();
252
+        $filter->setApp('files_sharing')
253
+            ->setUser($this->uid)
254
+            ->setObject('remote_share', (int) $remoteShare);
255
+        $this->notificationManager->markProcessed($filter);
256
+    }
257
+
258
+    /**
259
+     * inform remote server whether server-to-server share was accepted/declined
260
+     *
261
+     * @param string $remote
262
+     * @param string $token
263
+     * @param int $remoteId Share id on the remote host
264
+     * @param string $feedback
265
+     * @return boolean
266
+     */
267
+    private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) {
268
+
269
+        $federationEndpoints = $this->discoveryService->discover($remote, 'FEDERATED_SHARING');
270
+        $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
271
+
272
+        $url = rtrim($remote, '/') . $endpoint . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
273
+        $fields = array('token' => $token);
274
+
275
+        $client = $this->clientService->newClient();
276
+
277
+        try {
278
+            $response = $client->post(
279
+                $url,
280
+                [
281
+                    'body' => $fields,
282
+                    'connect_timeout' => 10,
283
+                ]
284
+            );
285
+        } catch (\Exception $e) {
286
+            return false;
287
+        }
288
+
289
+        $status = json_decode($response->getBody(), true);
290
+
291
+        return ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
292
+    }
293
+
294
+    /**
295
+     * remove '/user/files' from the path and trailing slashes
296
+     *
297
+     * @param string $path
298
+     * @return string
299
+     */
300
+    protected function stripPath($path) {
301
+        $prefix = '/' . $this->uid . '/files';
302
+        return rtrim(substr($path, strlen($prefix)), '/');
303
+    }
304
+
305
+    public function getMount($data) {
306
+        $data['manager'] = $this;
307
+        $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint'];
308
+        $data['mountpoint'] = $mountPoint;
309
+        $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid);
310
+        return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader);
311
+    }
312
+
313
+    /**
314
+     * @param array $data
315
+     * @return Mount
316
+     */
317
+    protected function mountShare($data) {
318
+        $mount = $this->getMount($data);
319
+        $this->mountManager->addMount($mount);
320
+        return $mount;
321
+    }
322
+
323
+    /**
324
+     * @return \OC\Files\Mount\Manager
325
+     */
326
+    public function getMountManager() {
327
+        return $this->mountManager;
328
+    }
329
+
330
+    /**
331
+     * @param string $source
332
+     * @param string $target
333
+     * @return bool
334
+     */
335
+    public function setMountPoint($source, $target) {
336
+        $source = $this->stripPath($source);
337
+        $target = $this->stripPath($target);
338
+        $sourceHash = md5($source);
339
+        $targetHash = md5($target);
340
+
341
+        $query = $this->connection->prepare('
342 342
 			UPDATE `*PREFIX*share_external`
343 343
 			SET `mountpoint` = ?, `mountpoint_hash` = ?
344 344
 			WHERE `mountpoint_hash` = ?
345 345
 			AND `user` = ?
346 346
 		');
347
-		$result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid));
347
+        $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid));
348 348
 
349
-		return $result;
350
-	}
349
+        return $result;
350
+    }
351 351
 
352
-	public function removeShare($mountPoint) {
352
+    public function removeShare($mountPoint) {
353 353
 
354
-		$mountPointObj = $this->mountManager->find($mountPoint);
355
-		$id = $mountPointObj->getStorage()->getCache()->getId('');
354
+        $mountPointObj = $this->mountManager->find($mountPoint);
355
+        $id = $mountPointObj->getStorage()->getCache()->getId('');
356 356
 
357
-		$mountPoint = $this->stripPath($mountPoint);
358
-		$hash = md5($mountPoint);
357
+        $mountPoint = $this->stripPath($mountPoint);
358
+        $hash = md5($mountPoint);
359 359
 
360
-		$getShare = $this->connection->prepare('
360
+        $getShare = $this->connection->prepare('
361 361
 			SELECT `remote`, `share_token`, `remote_id`
362 362
 			FROM  `*PREFIX*share_external`
363 363
 			WHERE `mountpoint_hash` = ? AND `user` = ?');
364
-		$result = $getShare->execute(array($hash, $this->uid));
364
+        $result = $getShare->execute(array($hash, $this->uid));
365 365
 
366
-		if ($result) {
367
-			$share = $getShare->fetch();
368
-			$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
369
-		}
370
-		$getShare->closeCursor();
366
+        if ($result) {
367
+            $share = $getShare->fetch();
368
+            $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
369
+        }
370
+        $getShare->closeCursor();
371 371
 
372
-		$query = $this->connection->prepare('
372
+        $query = $this->connection->prepare('
373 373
 			DELETE FROM `*PREFIX*share_external`
374 374
 			WHERE `mountpoint_hash` = ?
375 375
 			AND `user` = ?
376 376
 		');
377
-		$result = (bool)$query->execute(array($hash, $this->uid));
378
-
379
-		if($result) {
380
-			$this->removeReShares($id);
381
-		}
382
-
383
-		return $result;
384
-	}
385
-
386
-	/**
387
-	 * remove re-shares from share table and mapping in the federated_reshares table
388
-	 *
389
-	 * @param $mountPointId
390
-	 */
391
-	protected function removeReShares($mountPointId) {
392
-		$selectQuery = $this->connection->getQueryBuilder();
393
-		$query = $this->connection->getQueryBuilder();
394
-		$selectQuery->select('id')->from('share')
395
-			->where($selectQuery->expr()->eq('file_source', $query->createNamedParameter($mountPointId)));
396
-		$select = $selectQuery->getSQL();
397
-
398
-
399
-		$query->delete('federated_reshares')
400
-			->where($query->expr()->in('share_id', $query->createFunction('(' . $select . ')')));
401
-		$query->execute();
402
-
403
-		$deleteReShares = $this->connection->getQueryBuilder();
404
-		$deleteReShares->delete('share')
405
-			->where($deleteReShares->expr()->eq('file_source', $deleteReShares->createNamedParameter($mountPointId)));
406
-		$deleteReShares->execute();
407
-	}
408
-
409
-	/**
410
-	 * remove all shares for user $uid if the user was deleted
411
-	 *
412
-	 * @param string $uid
413
-	 * @return bool
414
-	 */
415
-	public function removeUserShares($uid) {
416
-		$getShare = $this->connection->prepare('
377
+        $result = (bool)$query->execute(array($hash, $this->uid));
378
+
379
+        if($result) {
380
+            $this->removeReShares($id);
381
+        }
382
+
383
+        return $result;
384
+    }
385
+
386
+    /**
387
+     * remove re-shares from share table and mapping in the federated_reshares table
388
+     *
389
+     * @param $mountPointId
390
+     */
391
+    protected function removeReShares($mountPointId) {
392
+        $selectQuery = $this->connection->getQueryBuilder();
393
+        $query = $this->connection->getQueryBuilder();
394
+        $selectQuery->select('id')->from('share')
395
+            ->where($selectQuery->expr()->eq('file_source', $query->createNamedParameter($mountPointId)));
396
+        $select = $selectQuery->getSQL();
397
+
398
+
399
+        $query->delete('federated_reshares')
400
+            ->where($query->expr()->in('share_id', $query->createFunction('(' . $select . ')')));
401
+        $query->execute();
402
+
403
+        $deleteReShares = $this->connection->getQueryBuilder();
404
+        $deleteReShares->delete('share')
405
+            ->where($deleteReShares->expr()->eq('file_source', $deleteReShares->createNamedParameter($mountPointId)));
406
+        $deleteReShares->execute();
407
+    }
408
+
409
+    /**
410
+     * remove all shares for user $uid if the user was deleted
411
+     *
412
+     * @param string $uid
413
+     * @return bool
414
+     */
415
+    public function removeUserShares($uid) {
416
+        $getShare = $this->connection->prepare('
417 417
 			SELECT `remote`, `share_token`, `remote_id`
418 418
 			FROM  `*PREFIX*share_external`
419 419
 			WHERE `user` = ?');
420
-		$result = $getShare->execute(array($uid));
420
+        $result = $getShare->execute(array($uid));
421 421
 
422
-		if ($result) {
423
-			$shares = $getShare->fetchAll();
424
-			foreach($shares as $share) {
425
-				$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
426
-			}
427
-		}
422
+        if ($result) {
423
+            $shares = $getShare->fetchAll();
424
+            foreach($shares as $share) {
425
+                $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
426
+            }
427
+        }
428 428
 
429
-		$query = $this->connection->prepare('
429
+        $query = $this->connection->prepare('
430 430
 			DELETE FROM `*PREFIX*share_external`
431 431
 			WHERE `user` = ?
432 432
 		');
433
-		return (bool)$query->execute(array($uid));
434
-	}
435
-
436
-	/**
437
-	 * return a list of shares which are not yet accepted by the user
438
-	 *
439
-	 * @return array list of open server-to-server shares
440
-	 */
441
-	public function getOpenShares() {
442
-		return $this->getShares(false);
443
-	}
444
-
445
-	/**
446
-	 * return a list of shares which are accepted by the user
447
-	 *
448
-	 * @return array list of accepted server-to-server shares
449
-	 */
450
-	public function getAcceptedShares() {
451
-		return $this->getShares(true);
452
-	}
453
-
454
-	/**
455
-	 * return a list of shares for the user
456
-	 *
457
-	 * @param bool|null $accepted True for accepted only,
458
-	 *                            false for not accepted,
459
-	 *                            null for all shares of the user
460
-	 * @return array list of open server-to-server shares
461
-	 */
462
-	private function getShares($accepted) {
463
-		$query = 'SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted`
433
+        return (bool)$query->execute(array($uid));
434
+    }
435
+
436
+    /**
437
+     * return a list of shares which are not yet accepted by the user
438
+     *
439
+     * @return array list of open server-to-server shares
440
+     */
441
+    public function getOpenShares() {
442
+        return $this->getShares(false);
443
+    }
444
+
445
+    /**
446
+     * return a list of shares which are accepted by the user
447
+     *
448
+     * @return array list of accepted server-to-server shares
449
+     */
450
+    public function getAcceptedShares() {
451
+        return $this->getShares(true);
452
+    }
453
+
454
+    /**
455
+     * return a list of shares for the user
456
+     *
457
+     * @param bool|null $accepted True for accepted only,
458
+     *                            false for not accepted,
459
+     *                            null for all shares of the user
460
+     * @return array list of open server-to-server shares
461
+     */
462
+    private function getShares($accepted) {
463
+        $query = 'SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted`
464 464
 		          FROM `*PREFIX*share_external` 
465 465
 				  WHERE `user` = ?';
466
-		$parameters = [$this->uid];
467
-		if (!is_null($accepted)) {
468
-			$query .= ' AND `accepted` = ?';
469
-			$parameters[] = (int) $accepted;
470
-		}
471
-		$query .= ' ORDER BY `id` ASC';
472
-
473
-		$shares = $this->connection->prepare($query);
474
-		$result = $shares->execute($parameters);
475
-
476
-		return $result ? $shares->fetchAll() : [];
477
-	}
466
+        $parameters = [$this->uid];
467
+        if (!is_null($accepted)) {
468
+            $query .= ' AND `accepted` = ?';
469
+            $parameters[] = (int) $accepted;
470
+        }
471
+        $query .= ' ORDER BY `id` ASC';
472
+
473
+        $shares = $this->connection->prepare($query);
474
+        $result = $shares->execute($parameters);
475
+
476
+        return $result ? $shares->fetchAll() : [];
477
+    }
478 478
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -113,18 +113,18 @@  discard block
 block discarded – undo
113 113
 	 * @param int $remoteId
114 114
 	 * @return Mount|null
115 115
 	 */
116
-	public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) {
116
+	public function addShare($remote, $token, $password, $name, $owner, $accepted = false, $user = null, $remoteId = -1) {
117 117
 
118 118
 		$user = $user ? $user : $this->uid;
119 119
 		$accepted = $accepted ? 1 : 0;
120
-		$name = Filesystem::normalizePath('/' . $name);
120
+		$name = Filesystem::normalizePath('/'.$name);
121 121
 
122 122
 		if (!$accepted) {
123 123
 			// To avoid conflicts with the mount point generation later,
124 124
 			// we only use a temporary mount point name here. The real
125 125
 			// mount point name will be generated when accepting the share,
126 126
 			// using the original share item name.
127
-			$tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}';
127
+			$tmpMountPointName = '{{TemporaryMountPointName#'.$name.'}}';
128 128
 			$mountPoint = $tmpMountPointName;
129 129
 			$hash = md5($tmpMountPointName);
130 130
 			$data = [
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
 			$i = 1;
144 144
 			while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) {
145 145
 				// The external share already exists for the user
146
-				$data['mountpoint'] = $tmpMountPointName . '-' . $i;
146
+				$data['mountpoint'] = $tmpMountPointName.'-'.$i;
147 147
 				$data['mountpoint_hash'] = md5($data['mountpoint']);
148 148
 				$i++;
149 149
 			}
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
 		}
152 152
 
153 153
 		$mountPoint = Files::buildNotExistingFileName('/', $name);
154
-		$mountPoint = Filesystem::normalizePath('/' . $mountPoint);
154
+		$mountPoint = Filesystem::normalizePath('/'.$mountPoint);
155 155
 		$hash = md5($mountPoint);
156 156
 
157 157
 		$query = $this->connection->prepare('
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 		if ($share) {
201 201
 			\OC_Util::setupFS($this->uid);
202 202
 			$mountPoint = Files::buildNotExistingFileName('/', $share['name']);
203
-			$mountPoint = Filesystem::normalizePath('/' . $mountPoint);
203
+			$mountPoint = Filesystem::normalizePath('/'.$mountPoint);
204 204
 			$hash = md5($mountPoint);
205 205
 
206 206
 			$acceptShare = $this->connection->prepare('
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 		$federationEndpoints = $this->discoveryService->discover($remote, 'FEDERATED_SHARING');
270 270
 		$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
271 271
 
272
-		$url = rtrim($remote, '/') . $endpoint . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
272
+		$url = rtrim($remote, '/').$endpoint.'/'.$remoteId.'/'.$feedback.'?format='.\OCP\Share::RESPONSE_FORMAT;
273 273
 		$fields = array('token' => $token);
274 274
 
275 275
 		$client = $this->clientService->newClient();
@@ -298,13 +298,13 @@  discard block
 block discarded – undo
298 298
 	 * @return string
299 299
 	 */
300 300
 	protected function stripPath($path) {
301
-		$prefix = '/' . $this->uid . '/files';
301
+		$prefix = '/'.$this->uid.'/files';
302 302
 		return rtrim(substr($path, strlen($prefix)), '/');
303 303
 	}
304 304
 
305 305
 	public function getMount($data) {
306 306
 		$data['manager'] = $this;
307
-		$mountPoint = '/' . $this->uid . '/files' . $data['mountpoint'];
307
+		$mountPoint = '/'.$this->uid.'/files'.$data['mountpoint'];
308 308
 		$data['mountpoint'] = $mountPoint;
309 309
 		$data['certificateManager'] = \OC::$server->getCertificateManager($this->uid);
310 310
 		return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader);
@@ -344,7 +344,7 @@  discard block
 block discarded – undo
344 344
 			WHERE `mountpoint_hash` = ?
345 345
 			AND `user` = ?
346 346
 		');
347
-		$result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid));
347
+		$result = (bool) $query->execute(array($target, $targetHash, $sourceHash, $this->uid));
348 348
 
349 349
 		return $result;
350 350
 	}
@@ -374,9 +374,9 @@  discard block
 block discarded – undo
374 374
 			WHERE `mountpoint_hash` = ?
375 375
 			AND `user` = ?
376 376
 		');
377
-		$result = (bool)$query->execute(array($hash, $this->uid));
377
+		$result = (bool) $query->execute(array($hash, $this->uid));
378 378
 
379
-		if($result) {
379
+		if ($result) {
380 380
 			$this->removeReShares($id);
381 381
 		}
382 382
 
@@ -397,7 +397,7 @@  discard block
 block discarded – undo
397 397
 
398 398
 
399 399
 		$query->delete('federated_reshares')
400
-			->where($query->expr()->in('share_id', $query->createFunction('(' . $select . ')')));
400
+			->where($query->expr()->in('share_id', $query->createFunction('('.$select.')')));
401 401
 		$query->execute();
402 402
 
403 403
 		$deleteReShares = $this->connection->getQueryBuilder();
@@ -421,7 +421,7 @@  discard block
 block discarded – undo
421 421
 
422 422
 		if ($result) {
423 423
 			$shares = $getShare->fetchAll();
424
-			foreach($shares as $share) {
424
+			foreach ($shares as $share) {
425 425
 				$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
426 426
 			}
427 427
 		}
@@ -430,7 +430,7 @@  discard block
 block discarded – undo
430 430
 			DELETE FROM `*PREFIX*share_external`
431 431
 			WHERE `user` = ?
432 432
 		');
433
-		return (bool)$query->execute(array($uid));
433
+		return (bool) $query->execute(array($uid));
434 434
 	}
435 435
 
436 436
 	/**
Please login to merge, or discard this patch.