Completed
Pull Request — master (#336)
by Maxence
01:42
created

FileSharingBroadcaster::sendMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\Circles\Circles;
29
30
use Exception;
31
use OC;
32
use OC\Share20\Share;
33
use OCA\Circles\AppInfo\Application;
34
use OCA\Circles\Db\SharesRequest;
35
use OCA\Circles\Db\TokensRequest;
36
use OCA\Circles\Exceptions\TokenDoesNotExistException;
37
use OCA\Circles\IBroadcaster;
38
use OCA\Circles\Model\Circle;
39
use OCA\Circles\Model\Member;
40
use OCA\Circles\Model\SharesToken;
41
use OCA\Circles\Model\SharingFrame;
42
use OCA\Circles\Service\ConfigService;
43
use OCA\Circles\Service\MiscService;
44
use OCP\AppFramework\QueryException;
45
use OCP\Defaults;
46
use OCP\Files\IRootFolder;
47
use OCP\Files\NotFoundException;
48
use OCP\IL10N;
49
use OCP\ILogger;
50
use OCP\IURLGenerator;
51
use OCP\IUser;
52
use OCP\IUserManager;
53
use OCP\Mail\IEMailTemplate;
54
use OCP\Mail\IMailer;
55
use OCP\Share\Exceptions\IllegalIDChangeException;
56
use OCP\Share\IShare;
57
use OCP\Util;
58
59
60
class FileSharingBroadcaster implements IBroadcaster {
61
62
63
	/** @var bool */
64
	private $initiated = false;
65
66
	/** @var IL10N */
67
	private $l10n = null;
68
69
	/** @var IMailer */
70
	private $mailer;
71
72
	/** @var IRootFolder */
73
	private $rootFolder;
74
75
	/** @var IUserManager */
76
	private $userManager;
77
78
	/** @var ILogger */
79
	private $logger;
80
81
	/** @var Defaults */
82
	private $defaults;
83
84
	/** @var IURLGenerator */
85
	private $urlGenerator;
86
87
	/** @var SharesRequest */
88
	private $sharesRequest;
89
90
	/** @var TokensRequest */
91
	private $tokensRequest;
92
93
	/** @var ConfigService */
94
	private $configService;
95
96
	/** @var MiscService */
97
	private $miscService;
98
99
100
	/**
101
	 * {@inheritdoc}
102
	 */
103
	public function init() {
104
		if ($this->initiated) {
105
			return;
106
		}
107
108
		$this->initiated = true;
109
		$this->l10n = OC::$server->getL10N(Application::APP_NAME);
110
		$this->mailer = OC::$server->getMailer();
111
		$this->rootFolder = OC::$server->getLazyRootFolder();
112
		$this->userManager = OC::$server->getUserManager();
113
		$this->logger = OC::$server->getLogger();
114
		$this->urlGenerator = OC::$server->getURLGenerator();
115
		try {
116
			$this->defaults = OC::$server->query(Defaults::class);
117
			$this->sharesRequest = OC::$server->query(SharesRequest::class);
118
			$this->tokensRequest = OC::$server->query(TokensRequest::class);
119
			$this->configService = OC::$server->query(ConfigService::class);
120
			$this->miscService = OC::$server->query(MiscService::class);
121
		} catch (QueryException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\QueryException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
122
			\OC::$server->getLogger()
123
						->log(1, 'Circles: cannot init FileSharingBroadcaster - ' . $e->getMessage());
124
		}
125
	}
126
127
128
	/**
129
	 * {@inheritdoc}
130
	 */
131
	public function end() {
132
	}
133
134
135
	/**
136
	 * {@inheritdoc}
137
	 */
138
	public function createShareToCircle(SharingFrame $frame, Circle $circle) {
139
		if ($frame->is0Circle()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !$frame->is0Circle();.
Loading history...
140
			return false;
141
		}
142
143
		return true;
144
	}
145
146
147
	/**
148
	 * {@inheritdoc}
149
	 */
150
	public function deleteShareToCircle(SharingFrame $frame, Circle $circle) {
151
		return true;
152
	}
153
154
155
	/**
156
	 * {@inheritdoc}
157
	 */
158
	public function editShareToCircle(SharingFrame $frame, Circle $circle) {
159
		return true;
160
	}
161
162
163
	/**
164
	 * {@inheritdoc}
165
	 * @throws IllegalIDChangeException
166
	 */
167
	public function createShareToMember(SharingFrame $frame, Member $member) {
168
		if (!$frame->is0Circle()) {
169
			return false;
170
		}
171
172
		$payload = $frame->getPayload();
173
		if (!key_exists('share', $payload)) {
174
			return false;
175
		}
176
177
		$share = $this->generateShare($payload['share']);
178
		if ($member->getType() === Member::TYPE_MAIL || $member->getType() === Member::TYPE_CONTACT) {
179
			try {
180
				$circle = $frame->getCircle();
181
				$password = '';
182
183
				if ($this->configService->enforcePasswordProtection()) {
184
					$password = $this->miscService->token(15);
185
				}
186
				$token = $this->tokensRequest->generateTokenForMember($member, $share->getId(), $password);
187
				if ($token !== '') {
188
					$this->sharedByMail($circle, $share, $member->getUserId(), $token, $password);
189
				}
190
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
191
			}
192
		}
193
194
		return true;
195
	}
196
197
198
	/**
199
	 * {@inheritdoc}
200
	 */
201
	public function deleteShareToMember(SharingFrame $frame, Member $member) {
202
		return true;
203
	}
204
205
206
	/**
207
	 * {@inheritdoc}
208
	 */
209
	public function editShareToMember(SharingFrame $frame, Member $member) {
210
		return true;
211
	}
212
213
214
	/**
215
	 * @param Circle $circle
216
	 * @param Member $member
217
	 */
218
	public function sendMailAboutExistingShares(Circle $circle, Member $member) {
219
		$this->init();
220
		if ($member->getType() !== Member::TYPE_MAIL && $member->getType() !== Member::TYPE_CONTACT) {
221
			return;
222
		}
223
224
		$allShares = $this->sharesRequest->getSharesForCircle($member->getCircleId());
225
		$knownShares = array_map(
226
			function(SharesToken $shareToken) {
227
				return $shareToken->getShareId();
228
			},
229
			$this->tokensRequest->getTokensFromMember($member)
230
		);
231
232
		$unknownShares = [];
233
		foreach ($allShares as $share) {
234
			if (!in_array($share['id'], $knownShares)) {
235
				$unknownShares[] = $share;
236
			}
237
		}
238
239
		$author = $circle->getViewer()
240
						 ->getUserId();
241
		$this->sendMailExitingShares(
242
			$unknownShares, MiscService::getDisplay($author, Member::TYPE_USER), $member, $circle->getName()
243
		);
244
	}
245
246
247
	/**
248
	 * recreate the share from the JSON payload.
249
	 *
250
	 * @param array $data
251
	 *
252
	 * @return IShare
0 ignored issues
show
Documentation introduced by
Should the return type not be Share?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
253
	 * @throws IllegalIDChangeException
254
	 */
255
	private function generateShare($data) {
256
		$this->logger->log(0, 'Regenerate shares from payload: ' . json_encode($data));
257
258
		$share = new Share($this->rootFolder, $this->userManager);
259
		$share->setId($data['id']);
260
		$share->setSharedBy($data['sharedBy']);
261
		$share->setSharedWith($data['sharedWith']);
262
		$share->setNodeId($data['nodeId']);
263
		$share->setShareOwner($data['shareOwner']);
264
		$share->setPermissions($data['permissions']);
265
		$share->setToken($data['token']);
266
		$share->setPassword($data['password']);
267
268
		return $share;
269
	}
270
271
272
	/**
273
	 * @param Circle $circle
274
	 * @param IShare $share
275
	 * @param string $email
276
	 * @param string $token
277
	 * @param string $password
278
	 */
279
	private function sharedByMail(Circle $circle, IShare $share, $email, $token, $password) {
280
		// genelink
281
		$link = $this->urlGenerator->linkToRouteAbsolute(
282
			'files_sharing.sharecontroller.showShare',
283
			['token' => $token]
284
		);
285
286
		try {
287
			$this->sendMail(
288
				$share->getNode()
289
					  ->getName(), $link,
290
				MiscService::getDisplay($share->getSharedBy(), Member::TYPE_USER),
291
				$circle->getName(), $email
292
			);
293
			$this->sendPasswordByMail(
294
				$share, MiscService::getDisplay($share->getSharedBy(), Member::TYPE_USER),
295
				$email, $password
296
			);
297
		} catch (Exception $e) {
298
			OC::$server->getLogger()
299
					   ->log(1, 'Circles::sharedByMail - mail were not sent: ' . $e->getMessage());
300
		}
301
	}
302
303
304
	/**
305
	 * @param $fileName
306
	 * @param string $link
307
	 * @param string $author
308
	 * @param $circleName
309
	 * @param string $email
310
	 *
311
	 * @throws Exception
312
	 */
313
	protected function sendMail($fileName, $link, $author, $circleName, $email) {
314
		$message = $this->mailer->createMessage();
315
316
		$this->logger->log(
317
			0, "Sending mail to circle '" . $circleName . "': " . $email . ' file: ' . $fileName
318
			   . ' - link: ' . $link
319
		);
320
321
		$subject = $this->l10n->t('%s shared »%s« with you.', [$author, $fileName]);
322
		$text = $this->l10n->t('%s shared »%s« with \'%s\'.', [$author, $fileName, $circleName]);
323
324
		$emailTemplate =
325
			$this->generateEmailTemplate($subject, $text, $fileName, $link, $author, $circleName);
326
327
		$instanceName = $this->defaults->getName();
328
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
329
330
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
331
		$message->setSubject($subject);
332
		$message->setPlainBody($emailTemplate->renderText());
333
		$message->setHtmlBody($emailTemplate->renderHtml());
334
		$message->setTo([$email]);
335
336
		$this->mailer->send($message);
337
	}
338
339
340
	/**
341
	 * @param IShare $share
342
	 * @param string $circleName
343
	 * @param string $email
344
	 *
345
	 * @param $password
346
	 *
347
	 * @throws NotFoundException
348
	 * @throws Exception
349
	 */
350
	protected function sendPasswordByMail(IShare $share, $circleName, $email, $password) {
351
		if (!$this->configService->sendPasswordByMail() || $password === '') {
352
			return;
353
		}
354
355
		$message = $this->mailer->createMessage();
356
357
		$this->logger->log(0, "Sending password mail to circle '" . $circleName . "': " . $email);
358
359
		$filename = $share->getNode()
360
						  ->getName();
361
		$initiator = $share->getSharedBy();
362
		$shareWith = $share->getSharedWith();
363
364
		$initiatorUser = $this->userManager->get($initiator);
365
		$initiatorDisplayName =
366
			($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
367
		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
368
369
		$plainBodyPart = $this->l10n->t(
370
			"%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n",
371
			[$initiatorDisplayName, $filename]
372
		);
373
		$htmlBodyPart = $this->l10n->t(
374
			'%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.',
375
			[$initiatorDisplayName, $filename]
376
		);
377
378
		$emailTemplate = $this->mailer->createEMailTemplate(
379
			'sharebymail.RecipientPasswordNotification', [
380
														   'filename'       => $filename,
381
														   'password'       => $password,
382
														   'initiator'      => $initiatorDisplayName,
383
														   'initiatorEmail' => $initiatorEmailAddress,
384
														   'shareWith'      => $shareWith,
385
													   ]
386
		);
387
388
		$emailTemplate->setSubject(
389
			$this->l10n->t(
390
				'Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]
391
			)
392
		);
393
		$emailTemplate->addHeader();
394
		$emailTemplate->addHeading($this->l10n->t('Password to access »%s«', [$filename]), false);
395
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
396
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
397
		$emailTemplate->addBodyText($password);
398
399
		// The "From" contains the sharers name
400
		$instanceName = $this->defaults->getName();
401
		$senderName = $this->l10n->t(
402
			'%1$s via %2$s',
403
			[
404
				$initiatorDisplayName,
405
				$instanceName
406
			]
407
		);
408
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
409 View Code Duplication
		if ($initiatorEmailAddress !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
410
			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
411
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
412
		} else {
413
			$emailTemplate->addFooter();
414
		}
415
416
		$message->setTo([$email]);
417
		$message->useTemplate($emailTemplate);
418
		$this->mailer->send($message);
419
	}
420
421
422
	/**
423
	 * @param $subject
424
	 * @param $text
425
	 * @param $fileName
426
	 * @param $link
427
	 * @param string $author
428
	 * @param string $circleName
429
	 *
430
	 * @return IEMailTemplate
431
	 */
432
	private function generateEmailTemplate($subject, $text, $fileName, $link, $author, $circleName
433
	) {
434
		$emailTemplate = $this->mailer->createEMailTemplate(
435
			'circles.ShareNotification', [
436
										   'fileName'   => $fileName,
437
										   'fileLink'   => $link,
438
										   'author'     => $author,
439
										   'circleName' => $circleName,
440
									   ]
441
		);
442
443
		$emailTemplate->addHeader();
444
		$emailTemplate->addHeading($subject, false);
445
		$emailTemplate->addBodyText(
446
			htmlspecialchars($text) . '<br>' . htmlspecialchars(
447
				$this->l10n->t('Click the button below to open it.')
448
			), $text
449
		);
450
		$emailTemplate->addBodyButton(
451
			$this->l10n->t('Open »%s«', [htmlspecialchars($fileName)]), $link
452
		);
453
454
		return $emailTemplate;
455
	}
456
457
458
	/**
459
	 * @param array $unknownShares
460
	 * @param string $author
461
	 * @param Member $member
462
	 * @param string $circleName
463
	 */
464
	private function sendMailExitingShares(array $unknownShares, $author, Member $member, $circleName) {
465
		$password = '';
466
		if ($this->configService->enforcePasswordProtection()) {
467
			try {
468
				$password = $this->miscService->token(15);
469
			} catch (Exception $e) {
470
				return;
471
			}
472
		}
473
474
		$data = [];
475
		foreach ($unknownShares as $share) {
476
			$data[] = $this->getMailLinkFromShare($share, $member, $password);
477
		}
478
479
480
		if (sizeof($data) === 0) {
481
			return;
482
		}
483
484
		try {
485
			$template = $this->generateMailExitingShares($author, $circleName);
486
			$this->fillMailExistingShares($template, $data);
487
			$this->sendMailExistingShares($template, $author, $member->getUserId());
488
			$this->sendPasswordExistingShares($author, $member->getUserId(), $password);
489
		} catch (Exception $e) {
490
			$this->logger->log(2, 'Failed to send mail about existing share ' . $e->getMessage());
491
		}
492
	}
493
494
495
	/**
496
	 * @param $author
497
	 * @param string $email
498
	 *
499
	 * @param $password
500
	 *
501
	 * @throws Exception
502
	 */
503
	protected function sendPasswordExistingShares($author, $email, $password) {
504
		if (!$this->configService->sendPasswordByMail() || $password === '') {
505
			return;
506
		}
507
508
		$message = $this->mailer->createMessage();
509
510
		$authorUser = $this->userManager->get($author);
511
		$authorName = ($authorUser instanceof IUser) ? $authorUser->getDisplayName() : $author;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
512
		$authorEmail = ($authorUser instanceof IUser) ? $authorUser->getEMailAddress() : null;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
513
514
		$this->logger->log(0, "Sending password mail about existing files to '" . $email . "'");
515
516
		$plainBodyPart = $this->l10n->t(
517
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
518
			[$authorName]
519
		);
520
		$htmlBodyPart = $this->l10n->t(
521
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
522
			[$authorName]
523
		);
524
525
		$emailTemplate = $this->mailer->createEMailTemplate(
526
			'sharebymail.RecipientPasswordNotification', [
527
														   'password' => $password,
528
														   'author'   => $author
529
													   ]
530
		);
531
532
		$emailTemplate->setSubject(
533
			$this->l10n->t(
534
				'Password to access files shared to you by %1$s', [$authorName]
535
			)
536
		);
537
		$emailTemplate->addHeader();
538
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
539
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
540
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
541
		$emailTemplate->addBodyText($password);
542
543
		// The "From" contains the sharers name
544
		$instanceName = $this->defaults->getName();
545
		$senderName = $this->l10n->t(
546
			'%1$s via %2$s',
547
			[
548
				$authorName,
549
				$instanceName
550
			]
551
		);
552
553
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
554 View Code Duplication
		if ($authorEmail !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
555
			$message->setReplyTo([$authorEmail => $authorName]);
556
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
557
		} else {
558
			$emailTemplate->addFooter();
559
		}
560
561
		$message->setTo([$email]);
562
		$message->useTemplate($emailTemplate);
563
		$this->mailer->send($message);
564
	}
565
566
	/**
567
	 * @param array $share
568
	 * @param Member $member
569
	 * @param string $password
570
	 *
571
	 * @return array
572
	 */
573
	private function getMailLinkFromShare(array $share, Member $member, $password) {
574
		$token = $this->tokensRequest->generateTokenForMember($member, $share['id'], $password);
575
		$link = $this->urlGenerator->linkToRouteAbsolute(
576
			'files_sharing.sharecontroller.showShare',
577
			['token' => $token]
578
		);
579
		$author = $share['uid_initiator'];
580
581
		$filename = basename($share['file_target']);
582
583
		return [
584
			'author'   => $author,
585
			'link'     => $link,
586
			'password' => $password,
587
			'filename' => $filename
588
		];
589
	}
590
591
592
	/**
593
	 * @param $author
594
	 * @param string $circleName
595
	 *
596
	 * @return IEMailTemplate
597
	 * @throws Exception
598
	 */
599
	protected function generateMailExitingShares($author, $circleName) {
600
		$this->logger->log(
601
			0, "Generating mail about existing share mail from '" . $author . "' in "
602
			   . $circleName
603
		);
604
605
		$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []);
606
		$emailTemplate->addHeader();
607
608
		$text = $this->l10n->t('%s shared multiple files with \'%s\'.', [$author, $circleName]);
609
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
610
611
		return $emailTemplate;
612
	}
613
614
615
	/**
616
	 * @param IEMailTemplate $emailTemplate
617
	 * @param array $data
618
	 */
619
	protected function fillMailExistingShares(IEMailTemplate $emailTemplate, array $data) {
620
		foreach ($data as $item) {
621
//			$text = $this->l10n->t('%s shared »%s« with you.', [$item['author'], $item['filename']]);
622
//			$emailTemplate->addBodyText(
623
//				htmlspecialchars($text) . '<br>' . htmlspecialchars(
624
//					$this->l10n->t('Click the button below to open it.')
625
//				), $text
626
//			);
627
			$emailTemplate->addBodyButton(
628
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
629
			);
630
		}
631
	}
632
633
634
	/**
635
	 * @param IEMailTemplate $emailTemplate
636
	 * @param $author
637
	 * @param $recipient
638
	 *
639
	 * @throws Exception
640
	 */
641
	protected function sendMailExistingShares(IEMailTemplate $emailTemplate, $author, $recipient) {
642
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
643
//		$emailTemplate->addHeading($subject, false);
644
645
		$instanceName = $this->defaults->getName();
646
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
647
648
		$message = $this->mailer->createMessage();
649
650
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
651
		$message->setSubject($subject);
652
		$message->setPlainBody($emailTemplate->renderText());
653
		$message->setHtmlBody($emailTemplate->renderHtml());
654
		$message->setTo([$recipient]);
655
656
		$this->mailer->send($message);
657
658
659
//		$message = $this->mailer->createMessage();
660
//
661
		//
662
		//
663
		//
664
665
666
//		$filename = $share->getNode()
667
//						  ->getName();
668
//		$initiator = $share->getSharedBy();
669
//		$shareWith = $share->getSharedWith();
670
//
671
//		$initiatorUser = $this->userManager->get($initiator);
672
//		$initiatorDisplayName =
673
//			($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
674
//		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
675
676
//		$plainBodyPart = $this->l10n->t(
677
//			"%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n",
678
//			[$initiatorDisplayName, $filename]
679
//		);
680
//		$htmlBodyPart = $this->l10n->t(
681
//			'%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.',
682
//			[$initiatorDisplayName, $filename]
683
//		);
684
//
685
//		$emailTemplate = $this->mailer->createEMailTemplate(
686
//			'sharebymail.RecipientPasswordNotification', [
687
//														   'filename'       => $filename,
688
//														   'password'       => $password,
689
//														   'initiator'      => $initiatorDisplayName,
690
//														   'initiatorEmail' => $initiatorEmailAddress,
691
//														   'shareWith'      => $shareWith,
692
//													   ]
693
//		);
694
//
695
//		$emailTemplate->setSubject(
696
//			$this->l10n->t(
697
//				'Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]
698
//			)
699
//		);
700
//		$emailTemplate->addHeader();
701
//		$emailTemplate->addHeading($this->l10n->t('Password to access »%s«', [$filename]), false);
702
//		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
703
//		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
704
//		$emailTemplate->addBodyText($password);
705
706
		// The "From" contains the sharers name
707
//		$instanceName = $this->defaults->getName();
708
//		$senderName = $this->l10n->t(
709
//			'%1$s via %2$s',
710
//			[
711
//				$initiatorDisplayName,
712
//				$instanceName
713
//			]
714
//		);
715
//		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
716
//		if ($initiatorEmailAddress !== null) {
717
//			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
718
//			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
719
//		} else {
720
//			$emailTemplate->addFooter();
721
//		}
722
//
723
//		$message->setTo([$email]);
724
//		$message->useTemplate($emailTemplate);
725
//		$this->mailer->send($message);
726
	}
727
728
729
}
730