Passed
Push — master ( 9da935...b0fe40 )
by Christoph
13:08 queued 11s
created
apps/sharebymail/lib/ShareByMailProvider.php 2 patches
Indentation   +1112 added lines, -1112 removed lines patch added patch discarded remove patch
@@ -71,1123 +71,1123 @@
 block discarded – undo
71 71
  */
72 72
 class ShareByMailProvider implements IShareProvider {
73 73
 
74
-	/** @var  IDBConnection */
75
-	private $dbConnection;
76
-
77
-	/** @var ILogger */
78
-	private $logger;
79
-
80
-	/** @var ISecureRandom */
81
-	private $secureRandom;
82
-
83
-	/** @var IUserManager */
84
-	private $userManager;
85
-
86
-	/** @var IRootFolder */
87
-	private $rootFolder;
88
-
89
-	/** @var IL10N */
90
-	private $l;
91
-
92
-	/** @var IMailer */
93
-	private $mailer;
94
-
95
-	/** @var IURLGenerator */
96
-	private $urlGenerator;
97
-
98
-	/** @var IManager  */
99
-	private $activityManager;
100
-
101
-	/** @var SettingsManager */
102
-	private $settingsManager;
103
-
104
-	/** @var Defaults */
105
-	private $defaults;
106
-
107
-	/** @var IHasher */
108
-	private $hasher;
109
-
110
-	/** @var IEventDispatcher */
111
-	private $eventDispatcher;
112
-
113
-	/**
114
-	 * Return the identifier of this provider.
115
-	 *
116
-	 * @return string Containing only [a-zA-Z0-9]
117
-	 */
118
-	public function identifier() {
119
-		return 'ocMailShare';
120
-	}
121
-
122
-	public function __construct(
123
-		IDBConnection $connection,
124
-		ISecureRandom $secureRandom,
125
-		IUserManager $userManager,
126
-		IRootFolder $rootFolder,
127
-		IL10N $l,
128
-		ILogger $logger,
129
-		IMailer $mailer,
130
-		IURLGenerator $urlGenerator,
131
-		IManager $activityManager,
132
-		SettingsManager $settingsManager,
133
-		Defaults $defaults,
134
-		IHasher $hasher,
135
-		IEventDispatcher $eventDispatcher
136
-	) {
137
-		$this->dbConnection = $connection;
138
-		$this->secureRandom = $secureRandom;
139
-		$this->userManager = $userManager;
140
-		$this->rootFolder = $rootFolder;
141
-		$this->l = $l;
142
-		$this->logger = $logger;
143
-		$this->mailer = $mailer;
144
-		$this->urlGenerator = $urlGenerator;
145
-		$this->activityManager = $activityManager;
146
-		$this->settingsManager = $settingsManager;
147
-		$this->defaults = $defaults;
148
-		$this->hasher = $hasher;
149
-		$this->eventDispatcher = $eventDispatcher;
150
-	}
151
-
152
-	/**
153
-	 * Share a path
154
-	 *
155
-	 * @param IShare $share
156
-	 * @return IShare The share object
157
-	 * @throws ShareNotFound
158
-	 * @throws \Exception
159
-	 */
160
-	public function create(IShare $share) {
161
-		$shareWith = $share->getSharedWith();
162
-		/*
74
+    /** @var  IDBConnection */
75
+    private $dbConnection;
76
+
77
+    /** @var ILogger */
78
+    private $logger;
79
+
80
+    /** @var ISecureRandom */
81
+    private $secureRandom;
82
+
83
+    /** @var IUserManager */
84
+    private $userManager;
85
+
86
+    /** @var IRootFolder */
87
+    private $rootFolder;
88
+
89
+    /** @var IL10N */
90
+    private $l;
91
+
92
+    /** @var IMailer */
93
+    private $mailer;
94
+
95
+    /** @var IURLGenerator */
96
+    private $urlGenerator;
97
+
98
+    /** @var IManager  */
99
+    private $activityManager;
100
+
101
+    /** @var SettingsManager */
102
+    private $settingsManager;
103
+
104
+    /** @var Defaults */
105
+    private $defaults;
106
+
107
+    /** @var IHasher */
108
+    private $hasher;
109
+
110
+    /** @var IEventDispatcher */
111
+    private $eventDispatcher;
112
+
113
+    /**
114
+     * Return the identifier of this provider.
115
+     *
116
+     * @return string Containing only [a-zA-Z0-9]
117
+     */
118
+    public function identifier() {
119
+        return 'ocMailShare';
120
+    }
121
+
122
+    public function __construct(
123
+        IDBConnection $connection,
124
+        ISecureRandom $secureRandom,
125
+        IUserManager $userManager,
126
+        IRootFolder $rootFolder,
127
+        IL10N $l,
128
+        ILogger $logger,
129
+        IMailer $mailer,
130
+        IURLGenerator $urlGenerator,
131
+        IManager $activityManager,
132
+        SettingsManager $settingsManager,
133
+        Defaults $defaults,
134
+        IHasher $hasher,
135
+        IEventDispatcher $eventDispatcher
136
+    ) {
137
+        $this->dbConnection = $connection;
138
+        $this->secureRandom = $secureRandom;
139
+        $this->userManager = $userManager;
140
+        $this->rootFolder = $rootFolder;
141
+        $this->l = $l;
142
+        $this->logger = $logger;
143
+        $this->mailer = $mailer;
144
+        $this->urlGenerator = $urlGenerator;
145
+        $this->activityManager = $activityManager;
146
+        $this->settingsManager = $settingsManager;
147
+        $this->defaults = $defaults;
148
+        $this->hasher = $hasher;
149
+        $this->eventDispatcher = $eventDispatcher;
150
+    }
151
+
152
+    /**
153
+     * Share a path
154
+     *
155
+     * @param IShare $share
156
+     * @return IShare The share object
157
+     * @throws ShareNotFound
158
+     * @throws \Exception
159
+     */
160
+    public function create(IShare $share) {
161
+        $shareWith = $share->getSharedWith();
162
+        /*
163 163
 		 * Check if file is not already shared with the remote user
164 164
 		 */
165
-		$alreadyShared = $this->getSharedWith($shareWith, IShare::TYPE_EMAIL, $share->getNode(), 1, 0);
166
-		if (!empty($alreadyShared)) {
167
-			$message = 'Sharing %1$s failed, this item is already shared with %2$s';
168
-			$message_t = $this->l->t('Sharing %1$s failed, this item is already shared with %2$s', [$share->getNode()->getName(), $shareWith]);
169
-			$this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
170
-			throw new \Exception($message_t);
171
-		}
172
-
173
-		// if the admin enforces a password for all mail shares we create a
174
-		// random password and send it to the recipient
175
-		$password = $share->getPassword() ?: '';
176
-		$passwordEnforced = $this->settingsManager->enforcePasswordProtection();
177
-		if ($passwordEnforced && empty($password)) {
178
-			$password = $this->autoGeneratePassword($share);
179
-		}
180
-
181
-		if (!empty($password)) {
182
-			$share->setPassword($this->hasher->hash($password));
183
-		}
184
-
185
-		$shareId = $this->createMailShare($share);
186
-		$send = $this->sendPassword($share, $password);
187
-		if ($passwordEnforced && $send === false) {
188
-			$this->sendPasswordToOwner($share, $password);
189
-		}
190
-
191
-		$this->createShareActivity($share);
192
-		$data = $this->getRawShare($shareId);
193
-
194
-		return $this->createShareObject($data);
195
-	}
196
-
197
-	/**
198
-	 * auto generate password in case of password enforcement on mail shares
199
-	 *
200
-	 * @param IShare $share
201
-	 * @return string
202
-	 * @throws \Exception
203
-	 */
204
-	protected function autoGeneratePassword($share) {
205
-		$initiatorUser = $this->userManager->get($share->getSharedBy());
206
-		$initiatorEMailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
207
-		$allowPasswordByMail = $this->settingsManager->sendPasswordByMail();
208
-
209
-		if ($initiatorEMailAddress === null && !$allowPasswordByMail) {
210
-			throw new \Exception(
211
-				$this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
212
-			);
213
-		}
214
-
215
-		$passwordEvent = new GenerateSecurePasswordEvent();
216
-		$this->eventDispatcher->dispatchTyped($passwordEvent);
217
-
218
-		$password = $passwordEvent->getPassword();
219
-		if ($password === null) {
220
-			$password = $this->secureRandom->generate(8, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
221
-		}
222
-
223
-		return $password;
224
-	}
225
-
226
-	/**
227
-	 * create activity if a file/folder was shared by mail
228
-	 *
229
-	 * @param IShare $share
230
-	 * @param string $type
231
-	 */
232
-	protected function createShareActivity(IShare $share, string $type = 'share') {
233
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
234
-
235
-		$this->publishActivity(
236
-			$type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_SELF : Activity::SUBJECT_UNSHARED_EMAIL_SELF,
237
-			[$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
238
-			$share->getSharedBy(),
239
-			$share->getNode()->getId(),
240
-			(string) $userFolder->getRelativePath($share->getNode()->getPath())
241
-		);
242
-
243
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
244
-			$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
245
-			$fileId = $share->getNode()->getId();
246
-			$nodes = $ownerFolder->getById($fileId);
247
-			$ownerPath = $nodes[0]->getPath();
248
-			$this->publishActivity(
249
-				$type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_BY : Activity::SUBJECT_UNSHARED_EMAIL_BY,
250
-				[$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
251
-				$share->getShareOwner(),
252
-				$fileId,
253
-				(string) $ownerFolder->getRelativePath($ownerPath)
254
-			);
255
-		}
256
-	}
257
-
258
-	/**
259
-	 * create activity if a file/folder was shared by mail
260
-	 *
261
-	 * @param IShare $share
262
-	 * @param string $sharedWith
263
-	 * @param bool $sendToSelf
264
-	 */
265
-	protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) {
266
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
267
-
268
-		if ($sendToSelf) {
269
-			$this->publishActivity(
270
-				Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
271
-				[$userFolder->getRelativePath($share->getNode()->getPath())],
272
-				$share->getSharedBy(),
273
-				$share->getNode()->getId(),
274
-				(string) $userFolder->getRelativePath($share->getNode()->getPath())
275
-			);
276
-		} else {
277
-			$this->publishActivity(
278
-				Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
279
-				[$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
280
-				$share->getSharedBy(),
281
-				$share->getNode()->getId(),
282
-				(string) $userFolder->getRelativePath($share->getNode()->getPath())
283
-			);
284
-		}
285
-	}
286
-
287
-
288
-	/**
289
-	 * publish activity if a file/folder was shared by mail
290
-	 *
291
-	 * @param string $subject
292
-	 * @param array $parameters
293
-	 * @param string $affectedUser
294
-	 * @param int $fileId
295
-	 * @param string $filePath
296
-	 */
297
-	protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath) {
298
-		$event = $this->activityManager->generateEvent();
299
-		$event->setApp('sharebymail')
300
-			->setType('shared')
301
-			->setSubject($subject, $parameters)
302
-			->setAffectedUser($affectedUser)
303
-			->setObject('files', $fileId, $filePath);
304
-		$this->activityManager->publish($event);
305
-	}
306
-
307
-	/**
308
-	 * @param IShare $share
309
-	 * @return int
310
-	 * @throws \Exception
311
-	 */
312
-	protected function createMailShare(IShare $share) {
313
-		$share->setToken($this->generateToken());
314
-		$shareId = $this->addShareToDB(
315
-			$share->getNodeId(),
316
-			$share->getNodeType(),
317
-			$share->getSharedWith(),
318
-			$share->getSharedBy(),
319
-			$share->getShareOwner(),
320
-			$share->getPermissions(),
321
-			$share->getToken(),
322
-			$share->getPassword(),
323
-			$share->getSendPasswordByTalk(),
324
-			$share->getHideDownload(),
325
-			$share->getExpirationDate()
326
-		);
327
-
328
-		try {
329
-			$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
330
-				['token' => $share->getToken()]);
331
-			$this->sendMailNotification(
332
-				$share->getNode()->getName(),
333
-				$link,
334
-				$share->getSharedBy(),
335
-				$share->getSharedWith(),
336
-				$share->getExpirationDate()
337
-			);
338
-		} catch (HintException $hintException) {
339
-			$this->logger->logException($hintException, [
340
-				'message' => 'Failed to send share by mail.',
341
-				'level' => ILogger::ERROR,
342
-				'app' => 'sharebymail',
343
-			]);
344
-			$this->removeShareFromTable($shareId);
345
-			throw $hintException;
346
-		} catch (\Exception $e) {
347
-			$this->logger->logException($e, [
348
-				'message' => 'Failed to send share by mail.',
349
-				'level' => ILogger::ERROR,
350
-				'app' => 'sharebymail',
351
-			]);
352
-			$this->removeShareFromTable($shareId);
353
-			throw new HintException('Failed to send share by mail',
354
-				$this->l->t('Failed to send share by email'));
355
-		}
356
-
357
-		return $shareId;
358
-	}
359
-
360
-	/**
361
-	 * @param string $filename
362
-	 * @param string $link
363
-	 * @param string $initiator
364
-	 * @param string $shareWith
365
-	 * @param \DateTime|null $expiration
366
-	 * @throws \Exception If mail couldn't be sent
367
-	 */
368
-	protected function sendMailNotification($filename,
369
-											$link,
370
-											$initiator,
371
-											$shareWith,
372
-											\DateTime $expiration = null) {
373
-		$initiatorUser = $this->userManager->get($initiator);
374
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
375
-		$message = $this->mailer->createMessage();
376
-
377
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
378
-			'filename' => $filename,
379
-			'link' => $link,
380
-			'initiator' => $initiatorDisplayName,
381
-			'expiration' => $expiration,
382
-			'shareWith' => $shareWith,
383
-		]);
384
-
385
-		$emailTemplate->setSubject($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]));
386
-		$emailTemplate->addHeader();
387
-		$emailTemplate->addHeading($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false);
388
-		$text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
389
-
390
-		$emailTemplate->addBodyText(
391
-			htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
392
-			$text
393
-		);
394
-		$emailTemplate->addBodyButton(
395
-			$this->l->t('Open »%s«', [$filename]),
396
-			$link
397
-		);
398
-
399
-		$message->setTo([$shareWith]);
400
-
401
-		// The "From" contains the sharers name
402
-		$instanceName = $this->defaults->getName();
403
-		$senderName = $instanceName;
404
-		if ($this->settingsManager->replyToInitiator()) {
405
-			$senderName = $this->l->t(
406
-				'%1$s via %2$s',
407
-				[
408
-					$initiatorDisplayName,
409
-					$instanceName
410
-				]
411
-			);
412
-		}
413
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
414
-
415
-		// The "Reply-To" is set to the sharer if an mail address is configured
416
-		// also the default footer contains a "Do not reply" which needs to be adjusted.
417
-		$initiatorEmail = $initiatorUser->getEMailAddress();
418
-		if ($this->settingsManager->replyToInitiator() && $initiatorEmail !== null) {
419
-			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
420
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
421
-		} else {
422
-			$emailTemplate->addFooter();
423
-		}
424
-
425
-		$message->useTemplate($emailTemplate);
426
-		$this->mailer->send($message);
427
-	}
428
-
429
-	/**
430
-	 * send password to recipient of a mail share
431
-	 *
432
-	 * @param IShare $share
433
-	 * @param string $password
434
-	 * @return bool
435
-	 */
436
-	protected function sendPassword(IShare $share, $password) {
437
-		$filename = $share->getNode()->getName();
438
-		$initiator = $share->getSharedBy();
439
-		$shareWith = $share->getSharedWith();
440
-
441
-		if ($password === '' || $this->settingsManager->sendPasswordByMail() === false || $share->getSendPasswordByTalk()) {
442
-			return false;
443
-		}
444
-
445
-		$initiatorUser = $this->userManager->get($initiator);
446
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
447
-		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
448
-
449
-		$plainBodyPart = $this->l->t("%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
450
-		$htmlBodyPart = $this->l->t('%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
451
-
452
-		$message = $this->mailer->createMessage();
453
-
454
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
455
-			'filename' => $filename,
456
-			'password' => $password,
457
-			'initiator' => $initiatorDisplayName,
458
-			'initiatorEmail' => $initiatorEmailAddress,
459
-			'shareWith' => $shareWith,
460
-		]);
461
-
462
-		$emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]));
463
-		$emailTemplate->addHeader();
464
-		$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
465
-		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
466
-		$emailTemplate->addBodyText($this->l->t('It is protected with the following password:'));
467
-		$emailTemplate->addBodyText($password);
468
-
469
-		// The "From" contains the sharers name
470
-		$instanceName = $this->defaults->getName();
471
-		$senderName = $instanceName;
472
-		if ($this->settingsManager->replyToInitiator()) {
473
-			$senderName = $this->l->t(
474
-				'%1$s via %2$s',
475
-				[
476
-					$initiatorDisplayName,
477
-					$instanceName
478
-				]
479
-			);
480
-		}
481
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
482
-		if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
483
-			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
484
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
485
-		} else {
486
-			$emailTemplate->addFooter();
487
-		}
488
-
489
-		$message->setTo([$shareWith]);
490
-		$message->useTemplate($emailTemplate);
491
-		$this->mailer->send($message);
492
-
493
-		$this->createPasswordSendActivity($share, $shareWith, false);
494
-
495
-		return true;
496
-	}
497
-
498
-	protected function sendNote(IShare $share) {
499
-		$recipient = $share->getSharedWith();
500
-
501
-
502
-		$filename = $share->getNode()->getName();
503
-		$initiator = $share->getSharedBy();
504
-		$note = $share->getNote();
505
-
506
-		$initiatorUser = $this->userManager->get($initiator);
507
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
508
-		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
509
-
510
-		$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
511
-		$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
512
-
513
-		$message = $this->mailer->createMessage();
514
-
515
-		$emailTemplate = $this->mailer->createEMailTemplate('shareByMail.sendNote');
516
-
517
-		$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
518
-		$emailTemplate->addHeader();
519
-		$emailTemplate->addHeading(htmlspecialchars($htmlHeading), $plainHeading);
520
-		$emailTemplate->addBodyText(htmlspecialchars($note), $note);
521
-
522
-		$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
523
-			['token' => $share->getToken()]);
524
-		$emailTemplate->addBodyButton(
525
-			$this->l->t('Open »%s«', [$filename]),
526
-			$link
527
-		);
528
-
529
-		// The "From" contains the sharers name
530
-		$instanceName = $this->defaults->getName();
531
-		$senderName = $instanceName;
532
-		if ($this->settingsManager->replyToInitiator()) {
533
-			$senderName = $this->l->t(
534
-				'%1$s via %2$s',
535
-				[
536
-					$initiatorDisplayName,
537
-					$instanceName
538
-				]
539
-			);
540
-		}
541
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
542
-		if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
543
-			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
544
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
545
-		} else {
546
-			$emailTemplate->addFooter();
547
-		}
548
-
549
-		$message->setTo([$recipient]);
550
-		$message->useTemplate($emailTemplate);
551
-		$this->mailer->send($message);
552
-	}
553
-
554
-	/**
555
-	 * send auto generated password to the owner. This happens if the admin enforces
556
-	 * a password for mail shares and forbid to send the password by mail to the recipient
557
-	 *
558
-	 * @param IShare $share
559
-	 * @param string $password
560
-	 * @return bool
561
-	 * @throws \Exception
562
-	 */
563
-	protected function sendPasswordToOwner(IShare $share, $password) {
564
-		$filename = $share->getNode()->getName();
565
-		$initiator = $this->userManager->get($share->getSharedBy());
566
-		$initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
567
-		$initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
568
-		$shareWith = $share->getSharedWith();
569
-
570
-		if ($initiatorEMailAddress === null) {
571
-			throw new \Exception(
572
-				$this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
573
-			);
574
-		}
575
-
576
-		$bodyPart = $this->l->t('You just shared »%1$s« with %2$s. The share was already sent to the recipient. Due to the security policies defined by the administrator of %3$s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.', [$filename, $shareWith, $this->defaults->getName()]);
577
-
578
-		$message = $this->mailer->createMessage();
579
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
580
-			'filename' => $filename,
581
-			'password' => $password,
582
-			'initiator' => $initiatorDisplayName,
583
-			'initiatorEmail' => $initiatorEMailAddress,
584
-			'shareWith' => $shareWith,
585
-		]);
586
-
587
-		$emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared by you with %2$s', [$filename, $shareWith]));
588
-		$emailTemplate->addHeader();
589
-		$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
590
-		$emailTemplate->addBodyText($bodyPart);
591
-		$emailTemplate->addBodyText($this->l->t('This is the password:'));
592
-		$emailTemplate->addBodyText($password);
593
-		$emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
594
-		$emailTemplate->addFooter();
595
-
596
-		$instanceName = $this->defaults->getName();
597
-		$senderName = $this->l->t(
598
-			'%1$s via %2$s',
599
-			[
600
-				$initiatorDisplayName,
601
-				$instanceName
602
-			]
603
-		);
604
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
605
-		$message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
606
-		$message->useTemplate($emailTemplate);
607
-		$this->mailer->send($message);
608
-
609
-		$this->createPasswordSendActivity($share, $shareWith, true);
610
-
611
-		return true;
612
-	}
613
-
614
-	/**
615
-	 * generate share token
616
-	 *
617
-	 * @return string
618
-	 */
619
-	protected function generateToken($size = 15) {
620
-		$token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
621
-		return $token;
622
-	}
623
-
624
-	/**
625
-	 * Get all children of this share
626
-	 *
627
-	 * @param IShare $parent
628
-	 * @return IShare[]
629
-	 */
630
-	public function getChildren(IShare $parent) {
631
-		$children = [];
632
-
633
-		$qb = $this->dbConnection->getQueryBuilder();
634
-		$qb->select('*')
635
-			->from('share')
636
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
637
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
638
-			->orderBy('id');
639
-
640
-		$cursor = $qb->execute();
641
-		while ($data = $cursor->fetch()) {
642
-			$children[] = $this->createShareObject($data);
643
-		}
644
-		$cursor->closeCursor();
645
-
646
-		return $children;
647
-	}
648
-
649
-	/**
650
-	 * add share to the database and return the ID
651
-	 *
652
-	 * @param int $itemSource
653
-	 * @param string $itemType
654
-	 * @param string $shareWith
655
-	 * @param string $sharedBy
656
-	 * @param string $uidOwner
657
-	 * @param int $permissions
658
-	 * @param string $token
659
-	 * @param string $password
660
-	 * @param bool $sendPasswordByTalk
661
-	 * @param bool $hideDownload
662
-	 * @param \DateTime|null $expirationTime
663
-	 * @return int
664
-	 */
665
-	protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password, $sendPasswordByTalk, $hideDownload, $expirationTime) {
666
-		$qb = $this->dbConnection->getQueryBuilder();
667
-		$qb->insert('share')
668
-			->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
669
-			->setValue('item_type', $qb->createNamedParameter($itemType))
670
-			->setValue('item_source', $qb->createNamedParameter($itemSource))
671
-			->setValue('file_source', $qb->createNamedParameter($itemSource))
672
-			->setValue('share_with', $qb->createNamedParameter($shareWith))
673
-			->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
674
-			->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
675
-			->setValue('permissions', $qb->createNamedParameter($permissions))
676
-			->setValue('token', $qb->createNamedParameter($token))
677
-			->setValue('password', $qb->createNamedParameter($password))
678
-			->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
679
-			->setValue('stime', $qb->createNamedParameter(time()))
680
-			->setValue('hide_download', $qb->createNamedParameter((int)$hideDownload, IQueryBuilder::PARAM_INT));
681
-
682
-		if ($expirationTime !== null) {
683
-			$qb->setValue('expiration', $qb->createNamedParameter($expirationTime, IQueryBuilder::PARAM_DATE));
684
-		}
685
-
686
-		/*
165
+        $alreadyShared = $this->getSharedWith($shareWith, IShare::TYPE_EMAIL, $share->getNode(), 1, 0);
166
+        if (!empty($alreadyShared)) {
167
+            $message = 'Sharing %1$s failed, this item is already shared with %2$s';
168
+            $message_t = $this->l->t('Sharing %1$s failed, this item is already shared with %2$s', [$share->getNode()->getName(), $shareWith]);
169
+            $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
170
+            throw new \Exception($message_t);
171
+        }
172
+
173
+        // if the admin enforces a password for all mail shares we create a
174
+        // random password and send it to the recipient
175
+        $password = $share->getPassword() ?: '';
176
+        $passwordEnforced = $this->settingsManager->enforcePasswordProtection();
177
+        if ($passwordEnforced && empty($password)) {
178
+            $password = $this->autoGeneratePassword($share);
179
+        }
180
+
181
+        if (!empty($password)) {
182
+            $share->setPassword($this->hasher->hash($password));
183
+        }
184
+
185
+        $shareId = $this->createMailShare($share);
186
+        $send = $this->sendPassword($share, $password);
187
+        if ($passwordEnforced && $send === false) {
188
+            $this->sendPasswordToOwner($share, $password);
189
+        }
190
+
191
+        $this->createShareActivity($share);
192
+        $data = $this->getRawShare($shareId);
193
+
194
+        return $this->createShareObject($data);
195
+    }
196
+
197
+    /**
198
+     * auto generate password in case of password enforcement on mail shares
199
+     *
200
+     * @param IShare $share
201
+     * @return string
202
+     * @throws \Exception
203
+     */
204
+    protected function autoGeneratePassword($share) {
205
+        $initiatorUser = $this->userManager->get($share->getSharedBy());
206
+        $initiatorEMailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
207
+        $allowPasswordByMail = $this->settingsManager->sendPasswordByMail();
208
+
209
+        if ($initiatorEMailAddress === null && !$allowPasswordByMail) {
210
+            throw new \Exception(
211
+                $this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
212
+            );
213
+        }
214
+
215
+        $passwordEvent = new GenerateSecurePasswordEvent();
216
+        $this->eventDispatcher->dispatchTyped($passwordEvent);
217
+
218
+        $password = $passwordEvent->getPassword();
219
+        if ($password === null) {
220
+            $password = $this->secureRandom->generate(8, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
221
+        }
222
+
223
+        return $password;
224
+    }
225
+
226
+    /**
227
+     * create activity if a file/folder was shared by mail
228
+     *
229
+     * @param IShare $share
230
+     * @param string $type
231
+     */
232
+    protected function createShareActivity(IShare $share, string $type = 'share') {
233
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
234
+
235
+        $this->publishActivity(
236
+            $type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_SELF : Activity::SUBJECT_UNSHARED_EMAIL_SELF,
237
+            [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
238
+            $share->getSharedBy(),
239
+            $share->getNode()->getId(),
240
+            (string) $userFolder->getRelativePath($share->getNode()->getPath())
241
+        );
242
+
243
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
244
+            $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
245
+            $fileId = $share->getNode()->getId();
246
+            $nodes = $ownerFolder->getById($fileId);
247
+            $ownerPath = $nodes[0]->getPath();
248
+            $this->publishActivity(
249
+                $type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_BY : Activity::SUBJECT_UNSHARED_EMAIL_BY,
250
+                [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
251
+                $share->getShareOwner(),
252
+                $fileId,
253
+                (string) $ownerFolder->getRelativePath($ownerPath)
254
+            );
255
+        }
256
+    }
257
+
258
+    /**
259
+     * create activity if a file/folder was shared by mail
260
+     *
261
+     * @param IShare $share
262
+     * @param string $sharedWith
263
+     * @param bool $sendToSelf
264
+     */
265
+    protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) {
266
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
267
+
268
+        if ($sendToSelf) {
269
+            $this->publishActivity(
270
+                Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
271
+                [$userFolder->getRelativePath($share->getNode()->getPath())],
272
+                $share->getSharedBy(),
273
+                $share->getNode()->getId(),
274
+                (string) $userFolder->getRelativePath($share->getNode()->getPath())
275
+            );
276
+        } else {
277
+            $this->publishActivity(
278
+                Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
279
+                [$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
280
+                $share->getSharedBy(),
281
+                $share->getNode()->getId(),
282
+                (string) $userFolder->getRelativePath($share->getNode()->getPath())
283
+            );
284
+        }
285
+    }
286
+
287
+
288
+    /**
289
+     * publish activity if a file/folder was shared by mail
290
+     *
291
+     * @param string $subject
292
+     * @param array $parameters
293
+     * @param string $affectedUser
294
+     * @param int $fileId
295
+     * @param string $filePath
296
+     */
297
+    protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath) {
298
+        $event = $this->activityManager->generateEvent();
299
+        $event->setApp('sharebymail')
300
+            ->setType('shared')
301
+            ->setSubject($subject, $parameters)
302
+            ->setAffectedUser($affectedUser)
303
+            ->setObject('files', $fileId, $filePath);
304
+        $this->activityManager->publish($event);
305
+    }
306
+
307
+    /**
308
+     * @param IShare $share
309
+     * @return int
310
+     * @throws \Exception
311
+     */
312
+    protected function createMailShare(IShare $share) {
313
+        $share->setToken($this->generateToken());
314
+        $shareId = $this->addShareToDB(
315
+            $share->getNodeId(),
316
+            $share->getNodeType(),
317
+            $share->getSharedWith(),
318
+            $share->getSharedBy(),
319
+            $share->getShareOwner(),
320
+            $share->getPermissions(),
321
+            $share->getToken(),
322
+            $share->getPassword(),
323
+            $share->getSendPasswordByTalk(),
324
+            $share->getHideDownload(),
325
+            $share->getExpirationDate()
326
+        );
327
+
328
+        try {
329
+            $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
330
+                ['token' => $share->getToken()]);
331
+            $this->sendMailNotification(
332
+                $share->getNode()->getName(),
333
+                $link,
334
+                $share->getSharedBy(),
335
+                $share->getSharedWith(),
336
+                $share->getExpirationDate()
337
+            );
338
+        } catch (HintException $hintException) {
339
+            $this->logger->logException($hintException, [
340
+                'message' => 'Failed to send share by mail.',
341
+                'level' => ILogger::ERROR,
342
+                'app' => 'sharebymail',
343
+            ]);
344
+            $this->removeShareFromTable($shareId);
345
+            throw $hintException;
346
+        } catch (\Exception $e) {
347
+            $this->logger->logException($e, [
348
+                'message' => 'Failed to send share by mail.',
349
+                'level' => ILogger::ERROR,
350
+                'app' => 'sharebymail',
351
+            ]);
352
+            $this->removeShareFromTable($shareId);
353
+            throw new HintException('Failed to send share by mail',
354
+                $this->l->t('Failed to send share by email'));
355
+        }
356
+
357
+        return $shareId;
358
+    }
359
+
360
+    /**
361
+     * @param string $filename
362
+     * @param string $link
363
+     * @param string $initiator
364
+     * @param string $shareWith
365
+     * @param \DateTime|null $expiration
366
+     * @throws \Exception If mail couldn't be sent
367
+     */
368
+    protected function sendMailNotification($filename,
369
+                                            $link,
370
+                                            $initiator,
371
+                                            $shareWith,
372
+                                            \DateTime $expiration = null) {
373
+        $initiatorUser = $this->userManager->get($initiator);
374
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
375
+        $message = $this->mailer->createMessage();
376
+
377
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
378
+            'filename' => $filename,
379
+            'link' => $link,
380
+            'initiator' => $initiatorDisplayName,
381
+            'expiration' => $expiration,
382
+            'shareWith' => $shareWith,
383
+        ]);
384
+
385
+        $emailTemplate->setSubject($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]));
386
+        $emailTemplate->addHeader();
387
+        $emailTemplate->addHeading($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false);
388
+        $text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
389
+
390
+        $emailTemplate->addBodyText(
391
+            htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
392
+            $text
393
+        );
394
+        $emailTemplate->addBodyButton(
395
+            $this->l->t('Open »%s«', [$filename]),
396
+            $link
397
+        );
398
+
399
+        $message->setTo([$shareWith]);
400
+
401
+        // The "From" contains the sharers name
402
+        $instanceName = $this->defaults->getName();
403
+        $senderName = $instanceName;
404
+        if ($this->settingsManager->replyToInitiator()) {
405
+            $senderName = $this->l->t(
406
+                '%1$s via %2$s',
407
+                [
408
+                    $initiatorDisplayName,
409
+                    $instanceName
410
+                ]
411
+            );
412
+        }
413
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
414
+
415
+        // The "Reply-To" is set to the sharer if an mail address is configured
416
+        // also the default footer contains a "Do not reply" which needs to be adjusted.
417
+        $initiatorEmail = $initiatorUser->getEMailAddress();
418
+        if ($this->settingsManager->replyToInitiator() && $initiatorEmail !== null) {
419
+            $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
420
+            $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
421
+        } else {
422
+            $emailTemplate->addFooter();
423
+        }
424
+
425
+        $message->useTemplate($emailTemplate);
426
+        $this->mailer->send($message);
427
+    }
428
+
429
+    /**
430
+     * send password to recipient of a mail share
431
+     *
432
+     * @param IShare $share
433
+     * @param string $password
434
+     * @return bool
435
+     */
436
+    protected function sendPassword(IShare $share, $password) {
437
+        $filename = $share->getNode()->getName();
438
+        $initiator = $share->getSharedBy();
439
+        $shareWith = $share->getSharedWith();
440
+
441
+        if ($password === '' || $this->settingsManager->sendPasswordByMail() === false || $share->getSendPasswordByTalk()) {
442
+            return false;
443
+        }
444
+
445
+        $initiatorUser = $this->userManager->get($initiator);
446
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
447
+        $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
448
+
449
+        $plainBodyPart = $this->l->t("%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
450
+        $htmlBodyPart = $this->l->t('%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
451
+
452
+        $message = $this->mailer->createMessage();
453
+
454
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
455
+            'filename' => $filename,
456
+            'password' => $password,
457
+            'initiator' => $initiatorDisplayName,
458
+            'initiatorEmail' => $initiatorEmailAddress,
459
+            'shareWith' => $shareWith,
460
+        ]);
461
+
462
+        $emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]));
463
+        $emailTemplate->addHeader();
464
+        $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
465
+        $emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
466
+        $emailTemplate->addBodyText($this->l->t('It is protected with the following password:'));
467
+        $emailTemplate->addBodyText($password);
468
+
469
+        // The "From" contains the sharers name
470
+        $instanceName = $this->defaults->getName();
471
+        $senderName = $instanceName;
472
+        if ($this->settingsManager->replyToInitiator()) {
473
+            $senderName = $this->l->t(
474
+                '%1$s via %2$s',
475
+                [
476
+                    $initiatorDisplayName,
477
+                    $instanceName
478
+                ]
479
+            );
480
+        }
481
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
482
+        if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
483
+            $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
484
+            $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
485
+        } else {
486
+            $emailTemplate->addFooter();
487
+        }
488
+
489
+        $message->setTo([$shareWith]);
490
+        $message->useTemplate($emailTemplate);
491
+        $this->mailer->send($message);
492
+
493
+        $this->createPasswordSendActivity($share, $shareWith, false);
494
+
495
+        return true;
496
+    }
497
+
498
+    protected function sendNote(IShare $share) {
499
+        $recipient = $share->getSharedWith();
500
+
501
+
502
+        $filename = $share->getNode()->getName();
503
+        $initiator = $share->getSharedBy();
504
+        $note = $share->getNote();
505
+
506
+        $initiatorUser = $this->userManager->get($initiator);
507
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
508
+        $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
509
+
510
+        $plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
511
+        $htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
512
+
513
+        $message = $this->mailer->createMessage();
514
+
515
+        $emailTemplate = $this->mailer->createEMailTemplate('shareByMail.sendNote');
516
+
517
+        $emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
518
+        $emailTemplate->addHeader();
519
+        $emailTemplate->addHeading(htmlspecialchars($htmlHeading), $plainHeading);
520
+        $emailTemplate->addBodyText(htmlspecialchars($note), $note);
521
+
522
+        $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
523
+            ['token' => $share->getToken()]);
524
+        $emailTemplate->addBodyButton(
525
+            $this->l->t('Open »%s«', [$filename]),
526
+            $link
527
+        );
528
+
529
+        // The "From" contains the sharers name
530
+        $instanceName = $this->defaults->getName();
531
+        $senderName = $instanceName;
532
+        if ($this->settingsManager->replyToInitiator()) {
533
+            $senderName = $this->l->t(
534
+                '%1$s via %2$s',
535
+                [
536
+                    $initiatorDisplayName,
537
+                    $instanceName
538
+                ]
539
+            );
540
+        }
541
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
542
+        if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
543
+            $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
544
+            $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
545
+        } else {
546
+            $emailTemplate->addFooter();
547
+        }
548
+
549
+        $message->setTo([$recipient]);
550
+        $message->useTemplate($emailTemplate);
551
+        $this->mailer->send($message);
552
+    }
553
+
554
+    /**
555
+     * send auto generated password to the owner. This happens if the admin enforces
556
+     * a password for mail shares and forbid to send the password by mail to the recipient
557
+     *
558
+     * @param IShare $share
559
+     * @param string $password
560
+     * @return bool
561
+     * @throws \Exception
562
+     */
563
+    protected function sendPasswordToOwner(IShare $share, $password) {
564
+        $filename = $share->getNode()->getName();
565
+        $initiator = $this->userManager->get($share->getSharedBy());
566
+        $initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
567
+        $initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
568
+        $shareWith = $share->getSharedWith();
569
+
570
+        if ($initiatorEMailAddress === null) {
571
+            throw new \Exception(
572
+                $this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
573
+            );
574
+        }
575
+
576
+        $bodyPart = $this->l->t('You just shared »%1$s« with %2$s. The share was already sent to the recipient. Due to the security policies defined by the administrator of %3$s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.', [$filename, $shareWith, $this->defaults->getName()]);
577
+
578
+        $message = $this->mailer->createMessage();
579
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
580
+            'filename' => $filename,
581
+            'password' => $password,
582
+            'initiator' => $initiatorDisplayName,
583
+            'initiatorEmail' => $initiatorEMailAddress,
584
+            'shareWith' => $shareWith,
585
+        ]);
586
+
587
+        $emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared by you with %2$s', [$filename, $shareWith]));
588
+        $emailTemplate->addHeader();
589
+        $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
590
+        $emailTemplate->addBodyText($bodyPart);
591
+        $emailTemplate->addBodyText($this->l->t('This is the password:'));
592
+        $emailTemplate->addBodyText($password);
593
+        $emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
594
+        $emailTemplate->addFooter();
595
+
596
+        $instanceName = $this->defaults->getName();
597
+        $senderName = $this->l->t(
598
+            '%1$s via %2$s',
599
+            [
600
+                $initiatorDisplayName,
601
+                $instanceName
602
+            ]
603
+        );
604
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
605
+        $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
606
+        $message->useTemplate($emailTemplate);
607
+        $this->mailer->send($message);
608
+
609
+        $this->createPasswordSendActivity($share, $shareWith, true);
610
+
611
+        return true;
612
+    }
613
+
614
+    /**
615
+     * generate share token
616
+     *
617
+     * @return string
618
+     */
619
+    protected function generateToken($size = 15) {
620
+        $token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
621
+        return $token;
622
+    }
623
+
624
+    /**
625
+     * Get all children of this share
626
+     *
627
+     * @param IShare $parent
628
+     * @return IShare[]
629
+     */
630
+    public function getChildren(IShare $parent) {
631
+        $children = [];
632
+
633
+        $qb = $this->dbConnection->getQueryBuilder();
634
+        $qb->select('*')
635
+            ->from('share')
636
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
637
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
638
+            ->orderBy('id');
639
+
640
+        $cursor = $qb->execute();
641
+        while ($data = $cursor->fetch()) {
642
+            $children[] = $this->createShareObject($data);
643
+        }
644
+        $cursor->closeCursor();
645
+
646
+        return $children;
647
+    }
648
+
649
+    /**
650
+     * add share to the database and return the ID
651
+     *
652
+     * @param int $itemSource
653
+     * @param string $itemType
654
+     * @param string $shareWith
655
+     * @param string $sharedBy
656
+     * @param string $uidOwner
657
+     * @param int $permissions
658
+     * @param string $token
659
+     * @param string $password
660
+     * @param bool $sendPasswordByTalk
661
+     * @param bool $hideDownload
662
+     * @param \DateTime|null $expirationTime
663
+     * @return int
664
+     */
665
+    protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password, $sendPasswordByTalk, $hideDownload, $expirationTime) {
666
+        $qb = $this->dbConnection->getQueryBuilder();
667
+        $qb->insert('share')
668
+            ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
669
+            ->setValue('item_type', $qb->createNamedParameter($itemType))
670
+            ->setValue('item_source', $qb->createNamedParameter($itemSource))
671
+            ->setValue('file_source', $qb->createNamedParameter($itemSource))
672
+            ->setValue('share_with', $qb->createNamedParameter($shareWith))
673
+            ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
674
+            ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
675
+            ->setValue('permissions', $qb->createNamedParameter($permissions))
676
+            ->setValue('token', $qb->createNamedParameter($token))
677
+            ->setValue('password', $qb->createNamedParameter($password))
678
+            ->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
679
+            ->setValue('stime', $qb->createNamedParameter(time()))
680
+            ->setValue('hide_download', $qb->createNamedParameter((int)$hideDownload, IQueryBuilder::PARAM_INT));
681
+
682
+        if ($expirationTime !== null) {
683
+            $qb->setValue('expiration', $qb->createNamedParameter($expirationTime, IQueryBuilder::PARAM_DATE));
684
+        }
685
+
686
+        /*
687 687
 		 * Added to fix https://github.com/owncloud/core/issues/22215
688 688
 		 * Can be removed once we get rid of ajax/share.php
689 689
 		 */
690
-		$qb->setValue('file_target', $qb->createNamedParameter(''));
691
-
692
-		$qb->execute();
693
-		$id = $qb->getLastInsertId();
694
-
695
-		return (int)$id;
696
-	}
697
-
698
-	/**
699
-	 * Update a share
700
-	 *
701
-	 * @param IShare $share
702
-	 * @param string|null $plainTextPassword
703
-	 * @return IShare The share object
704
-	 */
705
-	public function update(IShare $share, $plainTextPassword = null) {
706
-		$originalShare = $this->getShareById($share->getId());
707
-
708
-		// a real password was given
709
-		$validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
710
-
711
-		if ($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
712
-								($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()))) {
713
-			$this->sendPassword($share, $plainTextPassword);
714
-		}
715
-		/*
690
+        $qb->setValue('file_target', $qb->createNamedParameter(''));
691
+
692
+        $qb->execute();
693
+        $id = $qb->getLastInsertId();
694
+
695
+        return (int)$id;
696
+    }
697
+
698
+    /**
699
+     * Update a share
700
+     *
701
+     * @param IShare $share
702
+     * @param string|null $plainTextPassword
703
+     * @return IShare The share object
704
+     */
705
+    public function update(IShare $share, $plainTextPassword = null) {
706
+        $originalShare = $this->getShareById($share->getId());
707
+
708
+        // a real password was given
709
+        $validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
710
+
711
+        if ($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
712
+                                ($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()))) {
713
+            $this->sendPassword($share, $plainTextPassword);
714
+        }
715
+        /*
716 716
 		 * We allow updating the permissions and password of mail shares
717 717
 		 */
718
-		$qb = $this->dbConnection->getQueryBuilder();
719
-		$qb->update('share')
720
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
721
-			->set('permissions', $qb->createNamedParameter($share->getPermissions()))
722
-			->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
723
-			->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
724
-			->set('password', $qb->createNamedParameter($share->getPassword()))
725
-			->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
726
-			->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
727
-			->set('note', $qb->createNamedParameter($share->getNote()))
728
-			->set('hide_download', $qb->createNamedParameter((int)$share->getHideDownload(), IQueryBuilder::PARAM_INT))
729
-			->execute();
730
-
731
-		if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
732
-			$this->sendNote($share);
733
-		}
734
-
735
-		return $share;
736
-	}
737
-
738
-	/**
739
-	 * @inheritdoc
740
-	 */
741
-	public function move(IShare $share, $recipient) {
742
-		/**
743
-		 * nothing to do here, mail shares are only outgoing shares
744
-		 */
745
-		return $share;
746
-	}
747
-
748
-	/**
749
-	 * Delete a share (owner unShares the file)
750
-	 *
751
-	 * @param IShare $share
752
-	 */
753
-	public function delete(IShare $share) {
754
-		try {
755
-			$this->createShareActivity($share, 'unshare');
756
-		} catch (\Exception $e) {
757
-		}
758
-
759
-		$this->removeShareFromTable($share->getId());
760
-	}
761
-
762
-	/**
763
-	 * @inheritdoc
764
-	 */
765
-	public function deleteFromSelf(IShare $share, $recipient) {
766
-		// nothing to do here, mail shares are only outgoing shares
767
-	}
768
-
769
-	public function restore(IShare $share, string $recipient): IShare {
770
-		throw new GenericShareException('not implemented');
771
-	}
772
-
773
-	/**
774
-	 * @inheritdoc
775
-	 */
776
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
777
-		$qb = $this->dbConnection->getQueryBuilder();
778
-		$qb->select('*')
779
-			->from('share');
780
-
781
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
782
-
783
-		/**
784
-		 * Reshares for this user are shares where they are the owner.
785
-		 */
786
-		if ($reshares === false) {
787
-			//Special case for old shares created via the web UI
788
-			$or1 = $qb->expr()->andX(
789
-				$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
790
-				$qb->expr()->isNull('uid_initiator')
791
-			);
792
-
793
-			$qb->andWhere(
794
-				$qb->expr()->orX(
795
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
796
-					$or1
797
-				)
798
-			);
799
-		} else {
800
-			$qb->andWhere(
801
-				$qb->expr()->orX(
802
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
803
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
804
-				)
805
-			);
806
-		}
807
-
808
-		if ($node !== null) {
809
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
810
-		}
811
-
812
-		if ($limit !== -1) {
813
-			$qb->setMaxResults($limit);
814
-		}
815
-
816
-		$qb->setFirstResult($offset);
817
-		$qb->orderBy('id');
818
-
819
-		$cursor = $qb->execute();
820
-		$shares = [];
821
-		while ($data = $cursor->fetch()) {
822
-			$shares[] = $this->createShareObject($data);
823
-		}
824
-		$cursor->closeCursor();
825
-
826
-		return $shares;
827
-	}
828
-
829
-	/**
830
-	 * @inheritdoc
831
-	 */
832
-	public function getShareById($id, $recipientId = null) {
833
-		$qb = $this->dbConnection->getQueryBuilder();
834
-
835
-		$qb->select('*')
836
-			->from('share')
837
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
838
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
839
-
840
-		$cursor = $qb->execute();
841
-		$data = $cursor->fetch();
842
-		$cursor->closeCursor();
843
-
844
-		if ($data === false) {
845
-			throw new ShareNotFound();
846
-		}
847
-
848
-		try {
849
-			$share = $this->createShareObject($data);
850
-		} catch (InvalidShare $e) {
851
-			throw new ShareNotFound();
852
-		}
853
-
854
-		return $share;
855
-	}
856
-
857
-	/**
858
-	 * Get shares for a given path
859
-	 *
860
-	 * @param \OCP\Files\Node $path
861
-	 * @return IShare[]
862
-	 */
863
-	public function getSharesByPath(Node $path) {
864
-		$qb = $this->dbConnection->getQueryBuilder();
865
-
866
-		$cursor = $qb->select('*')
867
-			->from('share')
868
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
869
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
870
-			->execute();
871
-
872
-		$shares = [];
873
-		while ($data = $cursor->fetch()) {
874
-			$shares[] = $this->createShareObject($data);
875
-		}
876
-		$cursor->closeCursor();
877
-
878
-		return $shares;
879
-	}
880
-
881
-	/**
882
-	 * @inheritdoc
883
-	 */
884
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
885
-		/** @var IShare[] $shares */
886
-		$shares = [];
887
-
888
-		//Get shares directly with this user
889
-		$qb = $this->dbConnection->getQueryBuilder();
890
-		$qb->select('*')
891
-			->from('share');
892
-
893
-		// Order by id
894
-		$qb->orderBy('id');
895
-
896
-		// Set limit and offset
897
-		if ($limit !== -1) {
898
-			$qb->setMaxResults($limit);
899
-		}
900
-		$qb->setFirstResult($offset);
901
-
902
-		$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
903
-		$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
904
-
905
-		// Filter by node if provided
906
-		if ($node !== null) {
907
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
908
-		}
909
-
910
-		$cursor = $qb->execute();
911
-
912
-		while ($data = $cursor->fetch()) {
913
-			$shares[] = $this->createShareObject($data);
914
-		}
915
-		$cursor->closeCursor();
916
-
917
-
918
-		return $shares;
919
-	}
920
-
921
-	/**
922
-	 * Get a share by token
923
-	 *
924
-	 * @param string $token
925
-	 * @return IShare
926
-	 * @throws ShareNotFound
927
-	 */
928
-	public function getShareByToken($token) {
929
-		$qb = $this->dbConnection->getQueryBuilder();
930
-
931
-		$cursor = $qb->select('*')
932
-			->from('share')
933
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
934
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
935
-			->execute();
936
-
937
-		$data = $cursor->fetch();
938
-
939
-		if ($data === false) {
940
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
941
-		}
942
-
943
-		try {
944
-			$share = $this->createShareObject($data);
945
-		} catch (InvalidShare $e) {
946
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
947
-		}
948
-
949
-		return $share;
950
-	}
951
-
952
-	/**
953
-	 * remove share from table
954
-	 *
955
-	 * @param string $shareId
956
-	 */
957
-	protected function removeShareFromTable($shareId) {
958
-		$qb = $this->dbConnection->getQueryBuilder();
959
-		$qb->delete('share')
960
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
961
-		$qb->execute();
962
-	}
963
-
964
-	/**
965
-	 * Create a share object from an database row
966
-	 *
967
-	 * @param array $data
968
-	 * @return IShare
969
-	 * @throws InvalidShare
970
-	 * @throws ShareNotFound
971
-	 */
972
-	protected function createShareObject($data) {
973
-		$share = new Share($this->rootFolder, $this->userManager);
974
-		$share->setId((int)$data['id'])
975
-			->setShareType((int)$data['share_type'])
976
-			->setPermissions((int)$data['permissions'])
977
-			->setTarget($data['file_target'])
978
-			->setMailSend((bool)$data['mail_send'])
979
-			->setNote($data['note'])
980
-			->setToken($data['token']);
981
-
982
-		$shareTime = new \DateTime();
983
-		$shareTime->setTimestamp((int)$data['stime']);
984
-		$share->setShareTime($shareTime);
985
-		$share->setSharedWith($data['share_with']);
986
-		$share->setPassword($data['password']);
987
-		$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
988
-		$share->setHideDownload((bool)$data['hide_download']);
989
-
990
-		if ($data['uid_initiator'] !== null) {
991
-			$share->setShareOwner($data['uid_owner']);
992
-			$share->setSharedBy($data['uid_initiator']);
993
-		} else {
994
-			//OLD SHARE
995
-			$share->setSharedBy($data['uid_owner']);
996
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
997
-
998
-			$owner = $path->getOwner();
999
-			$share->setShareOwner($owner->getUID());
1000
-		}
1001
-
1002
-		if ($data['expiration'] !== null) {
1003
-			$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
1004
-			if ($expiration !== false) {
1005
-				$share->setExpirationDate($expiration);
1006
-			}
1007
-		}
1008
-
1009
-		$share->setNodeId((int)$data['file_source']);
1010
-		$share->setNodeType($data['item_type']);
1011
-
1012
-		$share->setProviderId($this->identifier());
1013
-
1014
-		return $share;
1015
-	}
1016
-
1017
-	/**
1018
-	 * Get the node with file $id for $user
1019
-	 *
1020
-	 * @param string $userId
1021
-	 * @param int $id
1022
-	 * @return \OCP\Files\File|\OCP\Files\Folder
1023
-	 * @throws InvalidShare
1024
-	 */
1025
-	private function getNode($userId, $id) {
1026
-		try {
1027
-			$userFolder = $this->rootFolder->getUserFolder($userId);
1028
-		} catch (NoUserException $e) {
1029
-			throw new InvalidShare();
1030
-		}
1031
-
1032
-		$nodes = $userFolder->getById($id);
1033
-
1034
-		if (empty($nodes)) {
1035
-			throw new InvalidShare();
1036
-		}
1037
-
1038
-		return $nodes[0];
1039
-	}
1040
-
1041
-	/**
1042
-	 * A user is deleted from the system
1043
-	 * So clean up the relevant shares.
1044
-	 *
1045
-	 * @param string $uid
1046
-	 * @param int $shareType
1047
-	 */
1048
-	public function userDeleted($uid, $shareType) {
1049
-		$qb = $this->dbConnection->getQueryBuilder();
1050
-
1051
-		$qb->delete('share')
1052
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
1053
-			->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
1054
-			->execute();
1055
-	}
1056
-
1057
-	/**
1058
-	 * This provider does not support group shares
1059
-	 *
1060
-	 * @param string $gid
1061
-	 */
1062
-	public function groupDeleted($gid) {
1063
-	}
1064
-
1065
-	/**
1066
-	 * This provider does not support group shares
1067
-	 *
1068
-	 * @param string $uid
1069
-	 * @param string $gid
1070
-	 */
1071
-	public function userDeletedFromGroup($uid, $gid) {
1072
-	}
1073
-
1074
-	/**
1075
-	 * get database row of a give share
1076
-	 *
1077
-	 * @param $id
1078
-	 * @return array
1079
-	 * @throws ShareNotFound
1080
-	 */
1081
-	protected function getRawShare($id) {
1082
-
1083
-		// Now fetch the inserted share and create a complete share object
1084
-		$qb = $this->dbConnection->getQueryBuilder();
1085
-		$qb->select('*')
1086
-			->from('share')
1087
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
1088
-
1089
-		$cursor = $qb->execute();
1090
-		$data = $cursor->fetch();
1091
-		$cursor->closeCursor();
1092
-
1093
-		if ($data === false) {
1094
-			throw new ShareNotFound;
1095
-		}
1096
-
1097
-		return $data;
1098
-	}
1099
-
1100
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
1101
-		$qb = $this->dbConnection->getQueryBuilder();
1102
-		$qb->select('*')
1103
-			->from('share', 's')
1104
-			->andWhere($qb->expr()->orX(
1105
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1106
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1107
-			))
1108
-			->andWhere(
1109
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
1110
-			);
1111
-
1112
-		/**
1113
-		 * Reshares for this user are shares where they are the owner.
1114
-		 */
1115
-		if ($reshares === false) {
1116
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
1117
-		} else {
1118
-			$qb->andWhere(
1119
-				$qb->expr()->orX(
1120
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
1121
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
1122
-				)
1123
-			);
1124
-		}
1125
-
1126
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1127
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1128
-
1129
-		$qb->orderBy('id');
1130
-
1131
-		$cursor = $qb->execute();
1132
-		$shares = [];
1133
-		while ($data = $cursor->fetch()) {
1134
-			$shares[$data['fileid']][] = $this->createShareObject($data);
1135
-		}
1136
-		$cursor->closeCursor();
1137
-
1138
-		return $shares;
1139
-	}
1140
-
1141
-	/**
1142
-	 * @inheritdoc
1143
-	 */
1144
-	public function getAccessList($nodes, $currentAccess) {
1145
-		$ids = [];
1146
-		foreach ($nodes as $node) {
1147
-			$ids[] = $node->getId();
1148
-		}
1149
-
1150
-		$qb = $this->dbConnection->getQueryBuilder();
1151
-		$qb->select('share_with')
1152
-			->from('share')
1153
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
1154
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1155
-			->andWhere($qb->expr()->orX(
1156
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1157
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1158
-			))
1159
-			->setMaxResults(1);
1160
-		$cursor = $qb->execute();
1161
-
1162
-		$mail = $cursor->fetch() !== false;
1163
-		$cursor->closeCursor();
1164
-
1165
-		return ['public' => $mail];
1166
-	}
1167
-
1168
-	public function getAllShares(): iterable {
1169
-		$qb = $this->dbConnection->getQueryBuilder();
1170
-
1171
-		$qb->select('*')
1172
-			->from('share')
1173
-			->where(
1174
-				$qb->expr()->orX(
1175
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_EMAIL))
1176
-				)
1177
-			);
1178
-
1179
-		$cursor = $qb->execute();
1180
-		while ($data = $cursor->fetch()) {
1181
-			try {
1182
-				$share = $this->createShareObject($data);
1183
-			} catch (InvalidShare $e) {
1184
-				continue;
1185
-			} catch (ShareNotFound $e) {
1186
-				continue;
1187
-			}
1188
-
1189
-			yield $share;
1190
-		}
1191
-		$cursor->closeCursor();
1192
-	}
718
+        $qb = $this->dbConnection->getQueryBuilder();
719
+        $qb->update('share')
720
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
721
+            ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
722
+            ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
723
+            ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
724
+            ->set('password', $qb->createNamedParameter($share->getPassword()))
725
+            ->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
726
+            ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
727
+            ->set('note', $qb->createNamedParameter($share->getNote()))
728
+            ->set('hide_download', $qb->createNamedParameter((int)$share->getHideDownload(), IQueryBuilder::PARAM_INT))
729
+            ->execute();
730
+
731
+        if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
732
+            $this->sendNote($share);
733
+        }
734
+
735
+        return $share;
736
+    }
737
+
738
+    /**
739
+     * @inheritdoc
740
+     */
741
+    public function move(IShare $share, $recipient) {
742
+        /**
743
+         * nothing to do here, mail shares are only outgoing shares
744
+         */
745
+        return $share;
746
+    }
747
+
748
+    /**
749
+     * Delete a share (owner unShares the file)
750
+     *
751
+     * @param IShare $share
752
+     */
753
+    public function delete(IShare $share) {
754
+        try {
755
+            $this->createShareActivity($share, 'unshare');
756
+        } catch (\Exception $e) {
757
+        }
758
+
759
+        $this->removeShareFromTable($share->getId());
760
+    }
761
+
762
+    /**
763
+     * @inheritdoc
764
+     */
765
+    public function deleteFromSelf(IShare $share, $recipient) {
766
+        // nothing to do here, mail shares are only outgoing shares
767
+    }
768
+
769
+    public function restore(IShare $share, string $recipient): IShare {
770
+        throw new GenericShareException('not implemented');
771
+    }
772
+
773
+    /**
774
+     * @inheritdoc
775
+     */
776
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
777
+        $qb = $this->dbConnection->getQueryBuilder();
778
+        $qb->select('*')
779
+            ->from('share');
780
+
781
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
782
+
783
+        /**
784
+         * Reshares for this user are shares where they are the owner.
785
+         */
786
+        if ($reshares === false) {
787
+            //Special case for old shares created via the web UI
788
+            $or1 = $qb->expr()->andX(
789
+                $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
790
+                $qb->expr()->isNull('uid_initiator')
791
+            );
792
+
793
+            $qb->andWhere(
794
+                $qb->expr()->orX(
795
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
796
+                    $or1
797
+                )
798
+            );
799
+        } else {
800
+            $qb->andWhere(
801
+                $qb->expr()->orX(
802
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
803
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
804
+                )
805
+            );
806
+        }
807
+
808
+        if ($node !== null) {
809
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
810
+        }
811
+
812
+        if ($limit !== -1) {
813
+            $qb->setMaxResults($limit);
814
+        }
815
+
816
+        $qb->setFirstResult($offset);
817
+        $qb->orderBy('id');
818
+
819
+        $cursor = $qb->execute();
820
+        $shares = [];
821
+        while ($data = $cursor->fetch()) {
822
+            $shares[] = $this->createShareObject($data);
823
+        }
824
+        $cursor->closeCursor();
825
+
826
+        return $shares;
827
+    }
828
+
829
+    /**
830
+     * @inheritdoc
831
+     */
832
+    public function getShareById($id, $recipientId = null) {
833
+        $qb = $this->dbConnection->getQueryBuilder();
834
+
835
+        $qb->select('*')
836
+            ->from('share')
837
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
838
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
839
+
840
+        $cursor = $qb->execute();
841
+        $data = $cursor->fetch();
842
+        $cursor->closeCursor();
843
+
844
+        if ($data === false) {
845
+            throw new ShareNotFound();
846
+        }
847
+
848
+        try {
849
+            $share = $this->createShareObject($data);
850
+        } catch (InvalidShare $e) {
851
+            throw new ShareNotFound();
852
+        }
853
+
854
+        return $share;
855
+    }
856
+
857
+    /**
858
+     * Get shares for a given path
859
+     *
860
+     * @param \OCP\Files\Node $path
861
+     * @return IShare[]
862
+     */
863
+    public function getSharesByPath(Node $path) {
864
+        $qb = $this->dbConnection->getQueryBuilder();
865
+
866
+        $cursor = $qb->select('*')
867
+            ->from('share')
868
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
869
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
870
+            ->execute();
871
+
872
+        $shares = [];
873
+        while ($data = $cursor->fetch()) {
874
+            $shares[] = $this->createShareObject($data);
875
+        }
876
+        $cursor->closeCursor();
877
+
878
+        return $shares;
879
+    }
880
+
881
+    /**
882
+     * @inheritdoc
883
+     */
884
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
885
+        /** @var IShare[] $shares */
886
+        $shares = [];
887
+
888
+        //Get shares directly with this user
889
+        $qb = $this->dbConnection->getQueryBuilder();
890
+        $qb->select('*')
891
+            ->from('share');
892
+
893
+        // Order by id
894
+        $qb->orderBy('id');
895
+
896
+        // Set limit and offset
897
+        if ($limit !== -1) {
898
+            $qb->setMaxResults($limit);
899
+        }
900
+        $qb->setFirstResult($offset);
901
+
902
+        $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
903
+        $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
904
+
905
+        // Filter by node if provided
906
+        if ($node !== null) {
907
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
908
+        }
909
+
910
+        $cursor = $qb->execute();
911
+
912
+        while ($data = $cursor->fetch()) {
913
+            $shares[] = $this->createShareObject($data);
914
+        }
915
+        $cursor->closeCursor();
916
+
917
+
918
+        return $shares;
919
+    }
920
+
921
+    /**
922
+     * Get a share by token
923
+     *
924
+     * @param string $token
925
+     * @return IShare
926
+     * @throws ShareNotFound
927
+     */
928
+    public function getShareByToken($token) {
929
+        $qb = $this->dbConnection->getQueryBuilder();
930
+
931
+        $cursor = $qb->select('*')
932
+            ->from('share')
933
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
934
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
935
+            ->execute();
936
+
937
+        $data = $cursor->fetch();
938
+
939
+        if ($data === false) {
940
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
941
+        }
942
+
943
+        try {
944
+            $share = $this->createShareObject($data);
945
+        } catch (InvalidShare $e) {
946
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
947
+        }
948
+
949
+        return $share;
950
+    }
951
+
952
+    /**
953
+     * remove share from table
954
+     *
955
+     * @param string $shareId
956
+     */
957
+    protected function removeShareFromTable($shareId) {
958
+        $qb = $this->dbConnection->getQueryBuilder();
959
+        $qb->delete('share')
960
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
961
+        $qb->execute();
962
+    }
963
+
964
+    /**
965
+     * Create a share object from an database row
966
+     *
967
+     * @param array $data
968
+     * @return IShare
969
+     * @throws InvalidShare
970
+     * @throws ShareNotFound
971
+     */
972
+    protected function createShareObject($data) {
973
+        $share = new Share($this->rootFolder, $this->userManager);
974
+        $share->setId((int)$data['id'])
975
+            ->setShareType((int)$data['share_type'])
976
+            ->setPermissions((int)$data['permissions'])
977
+            ->setTarget($data['file_target'])
978
+            ->setMailSend((bool)$data['mail_send'])
979
+            ->setNote($data['note'])
980
+            ->setToken($data['token']);
981
+
982
+        $shareTime = new \DateTime();
983
+        $shareTime->setTimestamp((int)$data['stime']);
984
+        $share->setShareTime($shareTime);
985
+        $share->setSharedWith($data['share_with']);
986
+        $share->setPassword($data['password']);
987
+        $share->setSendPasswordByTalk((bool)$data['password_by_talk']);
988
+        $share->setHideDownload((bool)$data['hide_download']);
989
+
990
+        if ($data['uid_initiator'] !== null) {
991
+            $share->setShareOwner($data['uid_owner']);
992
+            $share->setSharedBy($data['uid_initiator']);
993
+        } else {
994
+            //OLD SHARE
995
+            $share->setSharedBy($data['uid_owner']);
996
+            $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
997
+
998
+            $owner = $path->getOwner();
999
+            $share->setShareOwner($owner->getUID());
1000
+        }
1001
+
1002
+        if ($data['expiration'] !== null) {
1003
+            $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
1004
+            if ($expiration !== false) {
1005
+                $share->setExpirationDate($expiration);
1006
+            }
1007
+        }
1008
+
1009
+        $share->setNodeId((int)$data['file_source']);
1010
+        $share->setNodeType($data['item_type']);
1011
+
1012
+        $share->setProviderId($this->identifier());
1013
+
1014
+        return $share;
1015
+    }
1016
+
1017
+    /**
1018
+     * Get the node with file $id for $user
1019
+     *
1020
+     * @param string $userId
1021
+     * @param int $id
1022
+     * @return \OCP\Files\File|\OCP\Files\Folder
1023
+     * @throws InvalidShare
1024
+     */
1025
+    private function getNode($userId, $id) {
1026
+        try {
1027
+            $userFolder = $this->rootFolder->getUserFolder($userId);
1028
+        } catch (NoUserException $e) {
1029
+            throw new InvalidShare();
1030
+        }
1031
+
1032
+        $nodes = $userFolder->getById($id);
1033
+
1034
+        if (empty($nodes)) {
1035
+            throw new InvalidShare();
1036
+        }
1037
+
1038
+        return $nodes[0];
1039
+    }
1040
+
1041
+    /**
1042
+     * A user is deleted from the system
1043
+     * So clean up the relevant shares.
1044
+     *
1045
+     * @param string $uid
1046
+     * @param int $shareType
1047
+     */
1048
+    public function userDeleted($uid, $shareType) {
1049
+        $qb = $this->dbConnection->getQueryBuilder();
1050
+
1051
+        $qb->delete('share')
1052
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
1053
+            ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
1054
+            ->execute();
1055
+    }
1056
+
1057
+    /**
1058
+     * This provider does not support group shares
1059
+     *
1060
+     * @param string $gid
1061
+     */
1062
+    public function groupDeleted($gid) {
1063
+    }
1064
+
1065
+    /**
1066
+     * This provider does not support group shares
1067
+     *
1068
+     * @param string $uid
1069
+     * @param string $gid
1070
+     */
1071
+    public function userDeletedFromGroup($uid, $gid) {
1072
+    }
1073
+
1074
+    /**
1075
+     * get database row of a give share
1076
+     *
1077
+     * @param $id
1078
+     * @return array
1079
+     * @throws ShareNotFound
1080
+     */
1081
+    protected function getRawShare($id) {
1082
+
1083
+        // Now fetch the inserted share and create a complete share object
1084
+        $qb = $this->dbConnection->getQueryBuilder();
1085
+        $qb->select('*')
1086
+            ->from('share')
1087
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
1088
+
1089
+        $cursor = $qb->execute();
1090
+        $data = $cursor->fetch();
1091
+        $cursor->closeCursor();
1092
+
1093
+        if ($data === false) {
1094
+            throw new ShareNotFound;
1095
+        }
1096
+
1097
+        return $data;
1098
+    }
1099
+
1100
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
1101
+        $qb = $this->dbConnection->getQueryBuilder();
1102
+        $qb->select('*')
1103
+            ->from('share', 's')
1104
+            ->andWhere($qb->expr()->orX(
1105
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1106
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1107
+            ))
1108
+            ->andWhere(
1109
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
1110
+            );
1111
+
1112
+        /**
1113
+         * Reshares for this user are shares where they are the owner.
1114
+         */
1115
+        if ($reshares === false) {
1116
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
1117
+        } else {
1118
+            $qb->andWhere(
1119
+                $qb->expr()->orX(
1120
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
1121
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
1122
+                )
1123
+            );
1124
+        }
1125
+
1126
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1127
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1128
+
1129
+        $qb->orderBy('id');
1130
+
1131
+        $cursor = $qb->execute();
1132
+        $shares = [];
1133
+        while ($data = $cursor->fetch()) {
1134
+            $shares[$data['fileid']][] = $this->createShareObject($data);
1135
+        }
1136
+        $cursor->closeCursor();
1137
+
1138
+        return $shares;
1139
+    }
1140
+
1141
+    /**
1142
+     * @inheritdoc
1143
+     */
1144
+    public function getAccessList($nodes, $currentAccess) {
1145
+        $ids = [];
1146
+        foreach ($nodes as $node) {
1147
+            $ids[] = $node->getId();
1148
+        }
1149
+
1150
+        $qb = $this->dbConnection->getQueryBuilder();
1151
+        $qb->select('share_with')
1152
+            ->from('share')
1153
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
1154
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1155
+            ->andWhere($qb->expr()->orX(
1156
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1157
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1158
+            ))
1159
+            ->setMaxResults(1);
1160
+        $cursor = $qb->execute();
1161
+
1162
+        $mail = $cursor->fetch() !== false;
1163
+        $cursor->closeCursor();
1164
+
1165
+        return ['public' => $mail];
1166
+    }
1167
+
1168
+    public function getAllShares(): iterable {
1169
+        $qb = $this->dbConnection->getQueryBuilder();
1170
+
1171
+        $qb->select('*')
1172
+            ->from('share')
1173
+            ->where(
1174
+                $qb->expr()->orX(
1175
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_EMAIL))
1176
+                )
1177
+            );
1178
+
1179
+        $cursor = $qb->execute();
1180
+        while ($data = $cursor->fetch()) {
1181
+            try {
1182
+                $share = $this->createShareObject($data);
1183
+            } catch (InvalidShare $e) {
1184
+                continue;
1185
+            } catch (ShareNotFound $e) {
1186
+                continue;
1187
+            }
1188
+
1189
+            yield $share;
1190
+        }
1191
+        $cursor->closeCursor();
1192
+    }
1193 1193
 }
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
 
218 218
 		$password = $passwordEvent->getPassword();
219 219
 		if ($password === null) {
220
-			$password = $this->secureRandom->generate(8, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
220
+			$password = $this->secureRandom->generate(8, ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS);
221 221
 		}
222 222
 
223 223
 		return $password;
@@ -388,7 +388,7 @@  discard block
 block discarded – undo
388 388
 		$text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
389 389
 
390 390
 		$emailTemplate->addBodyText(
391
-			htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
391
+			htmlspecialchars($text.' '.$this->l->t('Click the button below to open it.')),
392 392
 			$text
393 393
 		);
394 394
 		$emailTemplate->addBodyButton(
@@ -417,7 +417,7 @@  discard block
 block discarded – undo
417 417
 		$initiatorEmail = $initiatorUser->getEMailAddress();
418 418
 		if ($this->settingsManager->replyToInitiator() && $initiatorEmail !== null) {
419 419
 			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
420
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
420
+			$emailTemplate->addFooter($instanceName.($this->defaults->getSlogan() !== '' ? ' - '.$this->defaults->getSlogan() : ''));
421 421
 		} else {
422 422
 			$emailTemplate->addFooter();
423 423
 		}
@@ -481,7 +481,7 @@  discard block
 block discarded – undo
481 481
 		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
482 482
 		if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
483 483
 			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
484
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
484
+			$emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan());
485 485
 		} else {
486 486
 			$emailTemplate->addFooter();
487 487
 		}
@@ -541,7 +541,7 @@  discard block
 block discarded – undo
541 541
 		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
542 542
 		if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
543 543
 			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
544
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
544
+			$emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan());
545 545
 		} else {
546 546
 			$emailTemplate->addFooter();
547 547
 		}
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
 			->setValue('password', $qb->createNamedParameter($password))
678 678
 			->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
679 679
 			->setValue('stime', $qb->createNamedParameter(time()))
680
-			->setValue('hide_download', $qb->createNamedParameter((int)$hideDownload, IQueryBuilder::PARAM_INT));
680
+			->setValue('hide_download', $qb->createNamedParameter((int) $hideDownload, IQueryBuilder::PARAM_INT));
681 681
 
682 682
 		if ($expirationTime !== null) {
683 683
 			$qb->setValue('expiration', $qb->createNamedParameter($expirationTime, IQueryBuilder::PARAM_DATE));
@@ -692,7 +692,7 @@  discard block
 block discarded – undo
692 692
 		$qb->execute();
693 693
 		$id = $qb->getLastInsertId();
694 694
 
695
-		return (int)$id;
695
+		return (int) $id;
696 696
 	}
697 697
 
698 698
 	/**
@@ -725,7 +725,7 @@  discard block
 block discarded – undo
725 725
 			->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
726 726
 			->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
727 727
 			->set('note', $qb->createNamedParameter($share->getNote()))
728
-			->set('hide_download', $qb->createNamedParameter((int)$share->getHideDownload(), IQueryBuilder::PARAM_INT))
728
+			->set('hide_download', $qb->createNamedParameter((int) $share->getHideDownload(), IQueryBuilder::PARAM_INT))
729 729
 			->execute();
730 730
 
731 731
 		if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
@@ -971,21 +971,21 @@  discard block
 block discarded – undo
971 971
 	 */
972 972
 	protected function createShareObject($data) {
973 973
 		$share = new Share($this->rootFolder, $this->userManager);
974
-		$share->setId((int)$data['id'])
975
-			->setShareType((int)$data['share_type'])
976
-			->setPermissions((int)$data['permissions'])
974
+		$share->setId((int) $data['id'])
975
+			->setShareType((int) $data['share_type'])
976
+			->setPermissions((int) $data['permissions'])
977 977
 			->setTarget($data['file_target'])
978
-			->setMailSend((bool)$data['mail_send'])
978
+			->setMailSend((bool) $data['mail_send'])
979 979
 			->setNote($data['note'])
980 980
 			->setToken($data['token']);
981 981
 
982 982
 		$shareTime = new \DateTime();
983
-		$shareTime->setTimestamp((int)$data['stime']);
983
+		$shareTime->setTimestamp((int) $data['stime']);
984 984
 		$share->setShareTime($shareTime);
985 985
 		$share->setSharedWith($data['share_with']);
986 986
 		$share->setPassword($data['password']);
987
-		$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
988
-		$share->setHideDownload((bool)$data['hide_download']);
987
+		$share->setSendPasswordByTalk((bool) $data['password_by_talk']);
988
+		$share->setHideDownload((bool) $data['hide_download']);
989 989
 
990 990
 		if ($data['uid_initiator'] !== null) {
991 991
 			$share->setShareOwner($data['uid_owner']);
@@ -993,7 +993,7 @@  discard block
 block discarded – undo
993 993
 		} else {
994 994
 			//OLD SHARE
995 995
 			$share->setSharedBy($data['uid_owner']);
996
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
996
+			$path = $this->getNode($share->getSharedBy(), (int) $data['file_source']);
997 997
 
998 998
 			$owner = $path->getOwner();
999 999
 			$share->setShareOwner($owner->getUID());
@@ -1006,7 +1006,7 @@  discard block
 block discarded – undo
1006 1006
 			}
1007 1007
 		}
1008 1008
 
1009
-		$share->setNodeId((int)$data['file_source']);
1009
+		$share->setNodeId((int) $data['file_source']);
1010 1010
 		$share->setNodeType($data['item_type']);
1011 1011
 
1012 1012
 		$share->setProviderId($this->identifier());
@@ -1123,7 +1123,7 @@  discard block
 block discarded – undo
1123 1123
 			);
1124 1124
 		}
1125 1125
 
1126
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1126
+		$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1127 1127
 		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1128 1128
 
1129 1129
 		$qb->orderBy('id');
Please login to merge, or discard this patch.