-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailService.php
More file actions
76 lines (66 loc) · 2.58 KB
/
MailService.php
File metadata and controls
76 lines (66 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
// SPDX-FileCopyrightText: 2025 Lennart Dohmann <lennart.dohmann@gdata.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\GDataVaas\Service;
use Coduo\PHPHumanizer\NumberHumanizer;
use Exception;
use OCA\GDataVaas\AppInfo\Application;
use OCP\IAppConfig;
use OCP\Mail\IMailer;
use Psr\Log\LoggerInterface;
use VaasSdk\VaasVerdict;
class MailService {
private IMailer $mailer;
private IAppConfig $config;
private LoggerInterface $logger;
public function __construct(IMailer $mailer, IAppConfig $config, LoggerInterface $logger) {
$this->mailer = $mailer;
$this->config = $config;
$this->logger = $logger;
}
/**
* @param VaasVerdict $verdict
* @param string $path
* @param string $owner
* @param int $size
* @return void
* @throws Exception
*/
public function notifyMaliciousUpload(VaasVerdict $verdict, string $path, string $owner, int $size): void {
$htmlMessage
= "<p>User <strong>$owner</strong> tried to upload an infected file to <strong>$path</strong>:</p><br>";
$htmlMessage .= '<p>Verdict: <strong>' . $verdict->verdict->value . '</strong></p>';
$htmlMessage .= '<p>Detection: <strong>' . $verdict->detection . '</strong></p>';
$htmlMessage .= '<p>Filetype: <strong>' . $verdict->fileType . '</strong></p>';
$htmlMessage .= '<p>Mimetype: <strong>' . $verdict->mimeType . '</strong></p>';
$htmlMessage .= '<p>SHA256: <strong>' . $verdict->sha256 . '</strong></p>';
$htmlMessage .= '<p>Size: <strong>' . NumberHumanizer::binarySuffix($size, 'de') . '</strong></p>';
$plainMessage = "User $owner tried to upload an infected file to $path:\n";
$plainMessage .= 'Verdict: ' . $verdict->verdict->value . "\n";
$plainMessage .= 'Detection: ' . $verdict->detection . "\n";
$plainMessage .= 'Filetype: ' . $verdict->fileType . "\n";
$plainMessage .= 'Mimetype: ' . $verdict->mimeType . "\n";
$plainMessage .= 'SHA256: ' . $verdict->sha256 . "\n";
$plainMessage .= 'Size: ' . NumberHumanizer::binarySuffix($size, 'de') . "\n";
$msg = $this->mailer->createMessage();
$msg->setSubject('Infected file uploaded');
$msg->setHtmlBody($htmlMessage);
$msg->setPlainBody($plainMessage);
$receiver = $this->getNotifyMails();
$msg->setTo($receiver);
$this->mailer->send($msg);
$this->logger->debug('Mail sent to ' . implode(', ', $receiver));
}
/**
* @return array
*/
private function getNotifyMails(): array {
$notifyMails = $this->config->getValueString(Application::APP_ID, 'notifyMails');
$notifyMails = preg_replace('/\s+/', '', $notifyMails);
if (empty($notifyMails)) {
return [];
}
return explode(',', $notifyMails);
}
}