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