Completed
Pull Request — master (#332)
by Maxence
01:49
created

FileSharingBroadcaster::sharedByMail()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 3
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->uuid(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 (TokenDoesNotExistException $e) {
191
			} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException 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...
192
			}
193
		}
194
195
		return true;
196
	}
197
198
199
	/**
200
	 * {@inheritdoc}
201
	 */
202
	public function deleteShareToMember(SharingFrame $frame, Member $member) {
203
		return true;
204
	}
205
206
207
	/**
208
	 * {@inheritdoc}
209
	 */
210
	public function editShareToMember(SharingFrame $frame, Member $member) {
211
		return true;
212
	}
213
214
215
	/**
216
	 * @param Circle $circle
217
	 * @param Member $member
218
	 */
219
	public function sendMailAboutExistingShares(Circle $circle, Member $member) {
220
		$this->init();
221
		if ($member->getType() !== Member::TYPE_MAIL && $member->getType() !== Member::TYPE_CONTACT) {
222
			return;
223
		}
224
225
		$allShares = $this->sharesRequest->getSharesForCircle($member->getCircleId());
226
		$knownShares = array_map(
227
			function(SharesToken $shareToken) {
228
				return $shareToken->getShareId();
229
			},
230
			$this->tokensRequest->getTokensFromMember($member)
231
		);
232
233
		$unknownShares = [];
234
		foreach ($allShares as $share) {
235
			if (!in_array($share['id'], $knownShares)) {
236
				$unknownShares[] = $share;
237
			}
238
		}
239
240
		$author = $circle->getViewer()
241
						 ->getUserId();
242
		$this->sendMailExitingShares(
243
			$unknownShares, MiscService::getDisplay($author, Member::TYPE_USER), $member, $circle->getName()
244
		);
245
	}
246
247
248
	/**
249
	 * recreate the share from the JSON payload.
250
	 *
251
	 * @param array $data
252
	 *
253
	 * @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...
254
	 * @throws IllegalIDChangeException
255
	 */
256
	private function generateShare($data) {
257
		$this->logger->log(0, 'Regenerate shares from payload: ' . json_encode($data));
258
259
		$share = new Share($this->rootFolder, $this->userManager);
260
		$share->setId($data['id']);
261
		$share->setSharedBy($data['sharedBy']);
262
		$share->setSharedWith($data['sharedWith']);
263
		$share->setNodeId($data['nodeId']);
264
		$share->setShareOwner($data['shareOwner']);
265
		$share->setPermissions($data['permissions']);
266
		$share->setToken($data['token']);
267
		$share->setPassword($data['password']);
268
269
		return $share;
270
	}
271
272
273
	/**
274
	 * @param Circle $circle
275
	 * @param IShare $share
276
	 * @param string $email
277
	 * @param string $token
278
	 * @param string $password
279
	 */
280
	private function sharedByMail(Circle $circle, IShare $share, $email, $token, $password) {
281
		// genelink
282
		$link = $this->urlGenerator->linkToRouteAbsolute(
283
			'files_sharing.sharecontroller.showShare',
284
			['token' => $token]
285
		);
286
287
		try {
288
			$this->sendMail(
289
				$share->getNode()
290
					  ->getName(), $link,
291
				MiscService::getDisplay($share->getSharedBy(), Member::TYPE_USER),
292
				$circle->getName(), $email
293
			);
294
			$this->sendPasswordByMail(
295
				$share, MiscService::getDisplay($share->getSharedBy(), Member::TYPE_USER),
296
				$email, $password
297
			);
298
		} catch (Exception $e) {
299
			OC::$server->getLogger()
300
					   ->log(1, 'Circles::sharedByMail - mail were not sent: ' . $e->getMessage());
301
		}
302
	}
303
304
305
	/**
306
	 * @param $fileName
307
	 * @param string $link
308
	 * @param string $author
309
	 * @param $circleName
310
	 * @param string $email
311
	 *
312
	 * @throws Exception
313
	 */
314
	protected function sendMail($fileName, $link, $author, $circleName, $email) {
315
		$message = $this->mailer->createMessage();
316
317
		$this->logger->log(
318
			0, "Sending mail to circle '" . $circleName . "': " . $email . ' file: ' . $fileName
319
			   . ' - link: ' . $link
320
		);
321
322
		$subject = $this->l10n->t('%s shared »%s« with you.', [$author, $fileName]);
323
		$text = $this->l10n->t('%s shared »%s« with \'%s\'.', [$author, $fileName, $circleName]);
324
325
		$emailTemplate =
326
			$this->generateEmailTemplate($subject, $text, $fileName, $link, $author, $circleName);
327
328
		$instanceName = $this->defaults->getName();
329
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
330
331
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
332
		$message->setSubject($subject);
333
		$message->setPlainBody($emailTemplate->renderText());
334
		$message->setHtmlBody($emailTemplate->renderHtml());
335
		$message->setTo([$email]);
336
337
		$this->mailer->send($message);
338
	}
339
340
341
	/**
342
	 * @param IShare $share
343
	 * @param string $circleName
344
	 * @param string $email
345
	 *
346
	 * @param $password
347
	 *
348
	 * @throws NotFoundException
349
	 * @throws Exception
350
	 */
351
	protected function sendPasswordByMail(IShare $share, $circleName, $email, $password) {
352
		if (!$this->configService->sendPasswordByMail() || $password === '') {
353
			return;
354
		}
355
356
		$message = $this->mailer->createMessage();
357
358
		$this->logger->log(0, "Sending password mail to circle '" . $circleName . "': " . $email);
359
360
		$filename = $share->getNode()
361
						  ->getName();
362
		$initiator = $share->getSharedBy();
363
		$shareWith = $share->getSharedWith();
364
365
		$initiatorUser = $this->userManager->get($initiator);
366
		$initiatorDisplayName =
367
			($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...
368
		$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...
369
370
		$plainBodyPart = $this->l10n->t(
371
			"%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n",
372
			[$initiatorDisplayName, $filename]
373
		);
374
		$htmlBodyPart = $this->l10n->t(
375
			'%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.',
376
			[$initiatorDisplayName, $filename]
377
		);
378
379
		$emailTemplate = $this->mailer->createEMailTemplate(
380
			'sharebymail.RecipientPasswordNotification', [
381
														   'filename'       => $filename,
382
														   'password'       => $password,
383
														   'initiator'      => $initiatorDisplayName,
384
														   'initiatorEmail' => $initiatorEmailAddress,
385
														   'shareWith'      => $shareWith,
386
													   ]
387
		);
388
389
		$emailTemplate->setSubject(
390
			$this->l10n->t(
391
				'Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]
392
			)
393
		);
394
		$emailTemplate->addHeader();
395
		$emailTemplate->addHeading($this->l10n->t('Password to access »%s«', [$filename]), false);
396
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
397
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
398
		$emailTemplate->addBodyText($password);
399
400
		// The "From" contains the sharers name
401
		$instanceName = $this->defaults->getName();
402
		$senderName = $this->l10n->t(
403
			'%1$s via %2$s',
404
			[
405
				$initiatorDisplayName,
406
				$instanceName
407
			]
408
		);
409
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
410 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...
411
			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
412
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
413
		} else {
414
			$emailTemplate->addFooter();
415
		}
416
417
		$message->setTo([$email]);
418
		$message->useTemplate($emailTemplate);
419
		$this->mailer->send($message);
420
	}
421
422
423
	/**
424
	 * @param $subject
425
	 * @param $text
426
	 * @param $fileName
427
	 * @param $link
428
	 * @param string $author
429
	 * @param string $circleName
430
	 *
431
	 * @return IEMailTemplate
432
	 */
433
	private function generateEmailTemplate($subject, $text, $fileName, $link, $author, $circleName
434
	) {
435
		$emailTemplate = $this->mailer->createEMailTemplate(
436
			'circles.ShareNotification', [
437
										   'fileName'   => $fileName,
438
										   'fileLink'   => $link,
439
										   'author'     => $author,
440
										   'circleName' => $circleName,
441
									   ]
442
		);
443
444
		$emailTemplate->addHeader();
445
		$emailTemplate->addHeading($subject, false);
446
		$emailTemplate->addBodyText(
447
			htmlspecialchars($text) . '<br>' . htmlspecialchars(
448
				$this->l10n->t('Click the button below to open it.')
449
			), $text
450
		);
451
		$emailTemplate->addBodyButton(
452
			$this->l10n->t('Open »%s«', [htmlspecialchars($fileName)]), $link
453
		);
454
455
		return $emailTemplate;
456
	}
457
458
459
	/**
460
	 * @param array $unknownShares
461
	 * @param string $author
462
	 * @param Member $member
463
	 * @param string $circleName
464
	 */
465
	private function sendMailExitingShares(array $unknownShares, $author, Member $member, $circleName) {
466
		$password = '';
467
		if ($this->configService->enforcePasswordProtection()) {
468
			$password = $this->miscService->uuid(15);
469
		}
470
471
		$data = [];
472
		foreach ($unknownShares as $share) {
473
			$data[] = $this->getMailLinkFromShare($share, $member, $password);
474
		}
475
476
477
		if (sizeof($data) === 0) {
478
			return;
479
		}
480
481
		try {
482
			$template = $this->generateMailExitingShares($author, $circleName);
483
			$this->fillMailExistingShares($template, $data);
484
			$this->sendMailExistingShares($template, $author, $member->getUserId());
485
			$this->sendPasswordExistingShares($author, $member->getUserId(), $password);
486
		} catch (Exception $e) {
487
			$this->logger->log(2, 'Failed to send mail about existing share ' . $e->getMessage());
488
		}
489
	}
490
491
492
	/**
493
	 * @param $author
494
	 * @param string $email
495
	 *
496
	 * @param $password
497
	 *
498
	 * @throws Exception
499
	 */
500
	protected function sendPasswordExistingShares($author, $email, $password) {
501
		if (!$this->configService->sendPasswordByMail() || $password === '') {
502
			return;
503
		}
504
505
		$message = $this->mailer->createMessage();
506
507
		$authorUser = $this->userManager->get($author);
508
		$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...
509
		$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...
510
511
		$this->logger->log(0, "Sending password mail about existing files to '" . $email . "'");
512
513
		$plainBodyPart = $this->l10n->t(
514
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
515
			[$authorName]
516
		);
517
		$htmlBodyPart = $this->l10n->t(
518
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
519
			[$authorName]
520
		);
521
522
		$emailTemplate = $this->mailer->createEMailTemplate(
523
			'sharebymail.RecipientPasswordNotification', [
524
														   'password' => $password,
525
														   'author'   => $author
526
													   ]
527
		);
528
529
		$emailTemplate->setSubject(
530
			$this->l10n->t(
531
				'Password to access files shared to you by %1$s', [$authorName]
532
			)
533
		);
534
		$emailTemplate->addHeader();
535
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
536
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
537
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
538
		$emailTemplate->addBodyText($password);
539
540
		// The "From" contains the sharers name
541
		$instanceName = $this->defaults->getName();
542
		$senderName = $this->l10n->t(
543
			'%1$s via %2$s',
544
			[
545
				$authorName,
546
				$instanceName
547
			]
548
		);
549
550
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
551 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...
552
			$message->setReplyTo([$authorEmail => $authorName]);
553
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
554
		} else {
555
			$emailTemplate->addFooter();
556
		}
557
558
		$message->setTo([$email]);
559
		$message->useTemplate($emailTemplate);
560
		$this->mailer->send($message);
561
	}
562
563
	/**
564
	 * @param array $share
565
	 * @param Member $member
566
	 * @param string $password
567
	 *
568
	 * @return array
569
	 */
570
	private function getMailLinkFromShare(array $share, Member $member, $password) {
571
		$token = $this->tokensRequest->generateTokenForMember($member, $share['id'], $password);
572
		$link = $this->urlGenerator->linkToRouteAbsolute(
573
			'files_sharing.sharecontroller.showShare',
574
			['token' => $token]
575
		);
576
		$author = $share['uid_initiator'];
577
578
		$filename = basename($share['file_target']);
579
580
		return [
581
			'author'   => $author,
582
			'link'     => $link,
583
			'password' => $password,
584
			'filename' => $filename
585
		];
586
	}
587
588
589
	/**
590
	 * @param $author
591
	 * @param string $circleName
592
	 *
593
	 * @return IEMailTemplate
594
	 * @throws Exception
595
	 */
596
	protected function generateMailExitingShares($author, $circleName) {
597
		$this->logger->log(
598
			0, "Generating mail about existing share mail from '" . $author . "' in "
599
			   . $circleName
600
		);
601
602
		$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []);
603
		$emailTemplate->addHeader();
604
605
		$text = $this->l10n->t('%s shared multiple files with \'%s\'.', [$author, $circleName]);
606
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
607
608
		return $emailTemplate;
609
	}
610
611
612
	/**
613
	 * @param IEMailTemplate $emailTemplate
614
	 * @param array $data
615
	 */
616
	protected function fillMailExistingShares(IEMailTemplate $emailTemplate, array $data) {
617
		foreach ($data as $item) {
618
//			$text = $this->l10n->t('%s shared »%s« with you.', [$item['author'], $item['filename']]);
619
//			$emailTemplate->addBodyText(
620
//				htmlspecialchars($text) . '<br>' . htmlspecialchars(
621
//					$this->l10n->t('Click the button below to open it.')
622
//				), $text
623
//			);
624
			$emailTemplate->addBodyButton(
625
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
626
			);
627
		}
628
	}
629
630
631
	/**
632
	 * @param IEMailTemplate $emailTemplate
633
	 * @param $author
634
	 * @param $recipient
635
	 *
636
	 * @throws Exception
637
	 */
638
	protected function sendMailExistingShares(IEMailTemplate $emailTemplate, $author, $recipient) {
639
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
640
//		$emailTemplate->addHeading($subject, false);
641
642
		$instanceName = $this->defaults->getName();
643
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
644
645
		$message = $this->mailer->createMessage();
646
647
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
648
		$message->setSubject($subject);
649
		$message->setPlainBody($emailTemplate->renderText());
650
		$message->setHtmlBody($emailTemplate->renderHtml());
651
		$message->setTo([$recipient]);
652
653
		$this->mailer->send($message);
654
655
656
//		$message = $this->mailer->createMessage();
657
//
658
		//
659
		//
660
		//
661
662
663
//		$filename = $share->getNode()
664
//						  ->getName();
665
//		$initiator = $share->getSharedBy();
666
//		$shareWith = $share->getSharedWith();
667
//
668
//		$initiatorUser = $this->userManager->get($initiator);
669
//		$initiatorDisplayName =
670
//			($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
671
//		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
672
673
//		$plainBodyPart = $this->l10n->t(
674
//			"%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n",
675
//			[$initiatorDisplayName, $filename]
676
//		);
677
//		$htmlBodyPart = $this->l10n->t(
678
//			'%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.',
679
//			[$initiatorDisplayName, $filename]
680
//		);
681
//
682
//		$emailTemplate = $this->mailer->createEMailTemplate(
683
//			'sharebymail.RecipientPasswordNotification', [
684
//														   'filename'       => $filename,
685
//														   'password'       => $password,
686
//														   'initiator'      => $initiatorDisplayName,
687
//														   'initiatorEmail' => $initiatorEmailAddress,
688
//														   'shareWith'      => $shareWith,
689
//													   ]
690
//		);
691
//
692
//		$emailTemplate->setSubject(
693
//			$this->l10n->t(
694
//				'Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]
695
//			)
696
//		);
697
//		$emailTemplate->addHeader();
698
//		$emailTemplate->addHeading($this->l10n->t('Password to access »%s«', [$filename]), false);
699
//		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
700
//		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
701
//		$emailTemplate->addBodyText($password);
702
703
		// The "From" contains the sharers name
704
//		$instanceName = $this->defaults->getName();
705
//		$senderName = $this->l10n->t(
706
//			'%1$s via %2$s',
707
//			[
708
//				$initiatorDisplayName,
709
//				$instanceName
710
//			]
711
//		);
712
//		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
713
//		if ($initiatorEmailAddress !== null) {
714
//			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
715
//			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
716
//		} else {
717
//			$emailTemplate->addFooter();
718
//		}
719
//
720
//		$message->setTo([$email]);
721
//		$message->useTemplate($emailTemplate);
722
//		$this->mailer->send($message);
723
	}
724
725
726
}
727