forked from Probesys/bileto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTicketsFromMailboxEmailsHandler.php
More file actions
425 lines (352 loc) · 15.3 KB
/
CreateTicketsFromMailboxEmailsHandler.php
File metadata and controls
425 lines (352 loc) · 15.3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
// This file is part of Bileto.
// Copyright 2022-2025 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace App\MessageHandler;
use App\ActivityMonitor;
use App\Entity;
use App\Message;
use App\Repository;
use App\Security;
use App\Service;
use App\TicketActivity;
use App\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Webklex\PHPIMAP;
#[AsMessageHandler]
class CreateTicketsFromMailboxEmailsHandler
{
public function __construct(
private Repository\MailboxEmailRepository $mailboxEmailRepository,
private Repository\MessageRepository $messageRepository,
private Repository\MessageDocumentRepository $messageDocumentRepository,
private Repository\OrganizationRepository $organizationRepository,
private Repository\TicketRepository $ticketRepository,
private Repository\UserRepository $userRepository,
private Service\MessageDocumentStorage $messageDocumentStorage,
private Service\TicketAssigner $ticketAssigner,
private Service\UserCreator $userCreator,
private Service\UserService $userService,
private Security\Authorizer $authorizer,
private ActivityMonitor\ActiveUser $activeUser,
private HtmlSanitizerInterface $appMessageSanitizer,
private LoggerInterface $logger,
private UrlGeneratorInterface $urlGenerator,
private EventDispatcherInterface $eventDispatcher,
private LockFactory $lockFactory,
) {
}
public function __invoke(Message\CreateTicketsFromMailboxEmails $message): void
{
$mailboxEmails = $this->mailboxEmailRepository->findAll();
foreach ($mailboxEmails as $mailboxEmail) {
$lock = $this->lockFactory->createLock("process-mailbox-email.{$mailboxEmail->getId()}", ttl: 1 * 60);
if (!$lock->acquire()) {
continue;
}
try {
$this->processMailboxEmail($mailboxEmail);
} catch (\Exception $e) {
$error = $e->getMessage() . "\n\n" . $e->getTraceAsString();
$mailboxEmail->setLastError($error);
$this->mailboxEmailRepository->save($mailboxEmail, true);
$this->logger->error("MailboxEmail #{$mailboxEmail->getId()} error: {$e->getMessage()}");
} finally {
$lock->release();
}
}
}
/**
* Create a message (and eventually a ticket) corresponding to the given
* mailbox email.
*/
private function processMailboxEmail(Entity\MailboxEmail $mailboxEmail): void
{
if ($mailboxEmail->isAutoreply()) {
$this->logger->notice("MailboxEmail #{$mailboxEmail->getId()} ignored: detected as auto reply");
$this->mailboxEmailRepository->remove($mailboxEmail, true);
return;
}
// First, get the user matching with the sender of the email.
$sender = $this->getSender($mailboxEmail);
if (!$sender) {
return;
}
// Set the active user so the created entities (e.g. ticket, message)
// will have the sender as `createdAt`.
$this->activeUser->change($sender);
// Then, get the ticket corresponding to the email. If the email
// doesn't answer to a ticket, a new one will be returned if possible.
$ticket = $this->getTicket($mailboxEmail, $sender);
if (!$ticket) {
$this->activeUser->change(null);
return;
}
$isNewTicket = $ticket->getId() === null;
if ($isNewTicket) {
$this->ticketRepository->save($ticket, flush: true);
}
// The important part: create the message by using the email
// information and attach it to the ticket.
$message = $this->createMessage($mailboxEmail, $ticket);
// Finally, dispatch the different events corresponding to what happened.
if ($isNewTicket) {
$ticketEvent = new TicketActivity\TicketEvent($ticket);
$this->eventDispatcher->dispatch($ticketEvent, TicketActivity\TicketEvent::CREATED);
}
$messageEvent = new TicketActivity\MessageEvent($message);
$this->eventDispatcher->dispatch($messageEvent, TicketActivity\MessageEvent::CREATED);
if (!$isNewTicket) {
$this->eventDispatcher->dispatch($messageEvent, TicketActivity\MessageEvent::CREATED_ANSWER);
}
$this->mailboxEmailRepository->remove($mailboxEmail, true);
$this->activeUser->change(null);
}
/**
* Return the user corresponding to the email sender address.
*
* If the user is unknown, but the email domain is handled by an
* organization, a new user is created and returned.
*
* Otherwise, the email is marked with an error and null is returned.
*/
private function getSender(Entity\MailboxEmail $mailboxEmail): ?Entity\User
{
$senderEmail = $mailboxEmail->getFrom();
$sender = $this->userRepository->findOneBy([
'email' => $senderEmail,
]);
if (!$sender) {
// We don't know the sender, but maybe there is a default organization
// matching the domain of his email?
$domain = Utils\Email::extractDomain($senderEmail);
$domainOrganization = $this->organizationRepository->findOneByDomainOrDefault($domain);
if (!$domainOrganization) {
$this->markError($mailboxEmail, 'unknown sender');
return null;
}
// There is a default organization? Then try to create the user.
try {
$sender = $this->userCreator->create(email: $senderEmail);
} catch (Service\UserCreatorException $e) {
$errors = Utils\ConstraintErrorsFormatter::format($e->getErrors());
$errors = implode(' ', $errors);
$this->markError($mailboxEmail, 'cannot create sender: ' . $errors);
return null;
}
}
return $sender;
}
/**
* Return the ticket corresponding to the email.
*
* It returns a ticket in two cases:
*
* - The email answers to an existing ticket and the sender has the
* permission to answer to it.
* - Otherwise, if the sender has a default organization, a new ticket is
* built. In this case, the code calling this method is in charge to save
* it in the database.
*
* In other cases, the email is marked with an error and null is returned.
*/
private function getTicket(
Entity\MailboxEmail $mailboxEmail,
Entity\User $sender,
): ?Entity\Ticket {
$ticket = $this->getAnsweredTicket($mailboxEmail);
if ($ticket && $this->canAnswerTicket($sender, $ticket)) {
// The easiest case: the email answers to a ticket and the sender
// can answer to it.
return $ticket;
}
if ($ticket) {
$this->logger->notice(
"MailboxEmail #{$mailboxEmail->getId()}: " .
"sender {$sender->getEmail()} cannot answer to ticket #{$ticket->getId()}"
);
$ticket = null;
}
$defaultOrganization = $this->userService->getDefaultOrganization($sender);
if (!$defaultOrganization) {
// By definition, the default organization is an organization in
// which the user can create tickets. If he has none, then he
// cannot create tickets.
$this->markError($mailboxEmail, 'sender has not permission to create tickets');
return null;
}
// Finally, build a ticket.
$subject = $mailboxEmail->getSubject();
$ticket = new Entity\Ticket();
$ticket->setTitle($subject);
$ticket->setOrganization($defaultOrganization);
$ticket->setRequester($sender);
$responsibleTeam = $this->ticketAssigner->getDefaultResponsibleTeam($defaultOrganization);
$ticket->setTeam($responsibleTeam);
foreach ($defaultOrganization->getObservers() as $observer) {
$ticket->addObserver($observer);
}
return $ticket;
}
/**
* Return a ticket referenced by the email.
*
* A ticket is referenced by an email either if:
*
* - The `In-Reply-To` header references a previous email sent by Bileto
* (i.e. referenced by a Message).
* - The subject or content is referencing a ticket id.
*/
private function getAnsweredTicket(Entity\MailboxEmail $mailboxEmail): ?Entity\Ticket
{
$replyId = $mailboxEmail->getInReplyTo();
if ($replyId !== null) {
// Verify if the email answers to a message stored in the database.
// We handle the case where the email answers to a GLPI email
// (after a migration from GLPI to Bileto for instance).
$glpiPattern = '/'
. 'GLPI'
. '_(?<uuid>[a-z0-9]+)'
. '-(?<itemType>[a-z]+)-(?<itemId>[0-9]+)' // this part is optional in GLPI, but required in our case
. '\/([a-z_]+)' // we don't care about the event, but we need to match it
. '(\.[0-9]+\.[0-9]+)?' // optional time and random values
. '@(?<hostname>.+)'
. '/i';
$result = preg_match($glpiPattern, $replyId, $matches);
if ($result === 1) {
// If the email comes from GLPI, we need to rebuild the
// reference to remove random parts of the header.
$reference = "glpi:GLPI_{$matches['uuid']}-{$matches['itemType']}-{$matches['itemId']}";
$reference .= "@{$matches['hostname']}";
} else {
$reference = "email:{$replyId}";
}
$replyMessage = $this->messageRepository->findOneByNotificationReference($reference);
if ($replyMessage) {
return $replyMessage->getTicket();
}
}
$ticketId = $mailboxEmail->extractTicketId();
if ($ticketId) {
return $this->ticketRepository->find($ticketId);
}
return null;
}
/**
* Return whether the user can answer the ticket.
*
* The user must have the orga:create:tickets:messages permission on the
* ticket's organization, and the ticket must not be closed.
*/
private function canAnswerTicket(Entity\User $user, Entity\Ticket $ticket): bool
{
$canAnswerTicket = $this->authorizer->isGrantedForUser(
$user,
'orga:create:tickets:messages',
$ticket,
);
return $canAnswerTicket && !$ticket->isClosed();
}
/**
* Create and return a message by using the email data (e.g. headers, body,
* attachments) and attach it to the ticket.
*/
private function createMessage(
Entity\MailboxEmail $mailboxEmail,
Entity\Ticket $ticket,
): Entity\Message {
// Extract the attachments and format the email body correctly.
$messageDocuments = $this->storeAttachments($mailboxEmail);
$this->messageDocumentRepository->save($messageDocuments, true);
$messageContent = $mailboxEmail->getBody();
// Inline attachments (i.e. <img>) have URLs of type: `cid:<id>`.
// Here we replace these URLs with the application URLs to the message documents.
$messageContent = $this->replaceAttachmentsUrls($messageContent, $messageDocuments);
// Sanitize the HTML only after replacing the URLs or it would
// remove the `src` attributes as the `cid:` scheme is forbidden.
$messageContent = $this->appMessageSanitizer->sanitize($messageContent);
// Finally, we create the message corresponding to the email.
$message = new Entity\Message();
$message->setPostedAt($mailboxEmail->getDate());
$message->setContent($messageContent);
$message->setTicket($ticket);
$message->setIsConfidential(false);
$message->setVia('email');
$emailId = $mailboxEmail->getMessageId();
$message->addEmailNotificationReference($emailId);
$this->messageRepository->save($message, true);
return $message;
}
/**
* Store the email attachments as MessageDocuments.
*
* @return array<string, Entity\MessageDocument>
*/
private function storeAttachments(Entity\MailboxEmail $mailboxEmail): array
{
$messageDocuments = [];
$tmpPath = sys_get_temp_dir();
foreach ($mailboxEmail->getAttachments() as $attachment) {
$id = $attachment->getId();
$originalFilename = $attachment->getName();
// PHP-IMAP can return invalid UTF-8 characters in some circumstances.
// mb_convert_encoding will replace these characters with the
// character "?".
// Bug issue: https://github.com/Webklex/php-imap/issues/459
$filename = mb_convert_encoding($originalFilename, 'UTF-8', 'UTF-8');
$status = $attachment->save($tmpPath, $filename);
if (!$status) {
$this->logger->warning(
"MailboxEmail #{$mailboxEmail->getId()} cannot import {$filename}: can't save in temporary dir"
);
continue;
}
$filepath = $tmpPath . '/' . $filename;
$file = new File($filepath, false);
try {
$messageDocuments[$id] = $this->messageDocumentStorage->store($file, $filename);
} catch (Service\MessageDocumentStorageError $e) {
$this->logger->warning(
"MailboxEmail #{$mailboxEmail->getId()} cannot import {$filename}: {$e->getMessage()}"
);
continue;
}
}
return $messageDocuments;
}
/**
* Replace (in the given content) the image src attributes of the inline
* attachments with the URLs of the corresponding message documents.
*
* @param array<string, Entity\MessageDocument> $messageDocuments
*/
private function replaceAttachmentsUrls(string $content, array $messageDocuments): string
{
$mapping = [];
foreach ($messageDocuments as $attachmentId => $messageDocument) {
$initialUrl = 'cid:' . $attachmentId;
$messageDocumentUrl = $this->urlGenerator->generate(
'message document',
[
'uid' => $messageDocument->getUid(),
'extension' => $messageDocument->getExtension(),
],
UrlGeneratorInterface::ABSOLUTE_URL
);
$mapping[$initialUrl] = $messageDocumentUrl;
}
return Utils\DomHelper::replaceImagesUrls($content, $mapping);
}
private function markError(Entity\MailboxEmail $mailboxEmail, string $error): void
{
$this->logger->warning("MailboxEmail #{$mailboxEmail->getId()} cannot be imported: {$error}");
$mailboxEmail->setLastError($error);
$this->mailboxEmailRepository->save($mailboxEmail, true);
}
}