ticket system rework for secure import, email stays unread until import is successfull emailbackup_mails will be reverted on error

This commit is contained in:
OpenXE 2022-12-21 21:34:00 +01:00
parent 0648e57b63
commit 71861079e3

View File

@ -493,12 +493,17 @@ class TicketImportHelper
$this->logger->debug('Start import', ['message' => $message->getSubject()]); $this->logger->debug('Start import', ['message' => $message->getSubject()]);
$this->importMessage($message); $result = $this->importMessage($message);
$insertedMailsCount++;
if ($this->mailAccount->isDeleteAfterImportEnabled()) { if ($result === true) {
$this->mailClient->deleteMessage((int)$messageNumber); $insertedMailsCount++;
if ($this->mailAccount->isDeleteAfterImportEnabled()) {
$this->mailClient->deleteMessage((int)$messageNumber);
} else {
$this->mailClient->setFlags((int)$messageNumber, ['\\Seen']);
}
} else { } else {
$this->mailClient->setFlags((int)$messageNumber, ['\\Seen']); $this->logger->error('Error during email import', ['']);
} }
} catch (Throwable $e) { } catch (Throwable $e) {
$this->logger->error('Error during email import', ['exception' => $e]); $this->logger->error('Error during email import', ['exception' => $e]);
@ -517,12 +522,10 @@ class TicketImportHelper
/** /**
* @param MailMessageInterface $message * @param MailMessageInterface $message
* *
* @return void * @return true on success
*/ */
public function importMessage(MailMessageInterface $message): void public function importMessage(MailMessageInterface $message): bool
{ {
$DEBUG = 0;
// extract email data // extract email data
$subject = $this->formatter->encodeToUtf8($message->getSubject()); $subject = $this->formatter->encodeToUtf8($message->getSubject());
$from = $this->formatter->encodeToUtf8($message->getSender()->getEmail()); $from = $this->formatter->encodeToUtf8($message->getSender()->getEmail());
@ -549,7 +552,7 @@ class TicketImportHelper
$action_html = nl2br($action); $action_html = nl2br($action);
} }
//check if email exists in database // Import database emailbackup
$date = $message->getDate(); $date = $message->getDate();
if (is_null($date)) { // This should not be happening -> Todo check getDate function if (is_null($date)) { // This should not be happening -> Todo check getDate function
$this->logger->debug('Null date',['subject' => $message->getSubject()]); $this->logger->debug('Null date',['subject' => $message->getSubject()]);
@ -563,21 +566,21 @@ class TicketImportHelper
FROM `emailbackup_mails` FROM `emailbackup_mails`
WHERE `checksum`='$frommd5' WHERE `checksum`='$frommd5'
AND `empfang`='$empfang' AND `empfang`='$empfang'
AND `ticketnachricht` != 0
AND `webmail`='" . $this->mailAccount->getId() . "'"; AND `webmail`='" . $this->mailAccount->getId() . "'";
$this->logger->debug('Importing message '.$from.' '.$fromname); $this->logger->debug('Importing message '.$from.' '.$fromname);
if ($this->db->Select($sql) == 0) { $result = $this->db->Select($sql);
$emailbackup_mails_id = null;
if ($result == 0) {
$this->logger->debug('Importing message',['']); $this->logger->debug('Importing message',['']);
$attachments = $message->getAttachments(); $attachments = $message->getAttachments();
$anhang = count($attachments) > 0 ? 1 : 0; $anhang = count($attachments) > 0 ? 1 : 0;
$mailacc = $this->mailAccount->getEmailAddress(); $mailacc = $this->mailAccount->getEmailAddress();
if (empty($mailacc) && count($message->getRecipients()) > 0) {
$mailacc = array_values($message->getRecipients())[0]->getEmail();
}
$mailaccid = $this->mailAccount->getId(); $mailaccid = $this->mailAccount->getId();
if (!$this->erpApi->isMailAdr($from)) { if (!$this->erpApi->isMailAdr($from)) {
@ -604,23 +607,24 @@ class TicketImportHelper
'$empfang','$anhang','$frommd5' '$empfang','$anhang','$frommd5'
)"; )";
$id = null; $this->db->InsertWithoutLog($sql);
if ($DEBUG) { $emailbackup_mails_id = $this->db->GetInsertID();
echo $sql; } else {
} else { $this->logger->debug('Message already imported.',['']);
$this->db->InsertWithoutLog($sql); return(true);
$id = $this->db->GetInsertID();
}
} }
if ($DEBUG) { $this->logger->debug('Message emailbackup_mails imported.',['id' => $emailbackup_mails_id]);
echo "ticket suchen oder anlegen\n"; // END database import emailbackup
}
// Find ticket and add or create new ticket
$ticketNumber = null; $ticketNumber = null;
$ticketexists = null; $ticketexists = null;
if (preg_match("/Ticket #[0-9]{12}/i", $subject, $matches)) { if (preg_match("/Ticket #[0-9]{12}/i", $subject, $matches)) {
$ticketNumber = str_replace('Ticket #', '', $matches[0]); $ticketNumber = str_replace('Ticket #', '', $matches[0]);
$this->logger->debug('Check for number',['ticketnummer' => $ticketNumber]);
$ticketexists = $this->db->Select( $ticketexists = $this->db->Select(
"SELECT schluessel "SELECT schluessel
FROM ticket FROM ticket
@ -648,7 +652,7 @@ class TicketImportHelper
$this->logger->debug('Add message to existing ticket',['ticketnummer' => $ticketNumber]); $this->logger->debug('Add message to existing ticket',['ticketnummer' => $ticketNumber]);
} }
// Add message to new or existing ticket // Database import ticket: Add message to new or existing ticket
$ticketnachricht = $this->addTicketMessage( $ticketnachricht = $this->addTicketMessage(
(string) $ticketNumber, (string) $ticketNumber,
$timestamp, $timestamp,
@ -661,11 +665,11 @@ class TicketImportHelper
$from $from
); );
if ($ticketnachricht > 0 && $id > 0) { if ($ticketnachricht > 0 && $emailbackup_mails_id > 0) {
$this->db->Update( $this->db->Update(
"UPDATE `emailbackup_mails` "UPDATE `emailbackup_mails`
SET ticketnachricht='$ticketnachricht' SET ticketnachricht='$ticketnachricht'
WHERE id='$id' LIMIT 1" WHERE id='$emailbackup_mails_id' LIMIT 1"
); );
@ -711,56 +715,58 @@ class TicketImportHelper
} }
} }
} }
} else {
$this->logger->error("Message not imported!", ['Time' => $timestamp, 'Subject' => $subject, 'From' => $from]);
$this->db->Delete("DELETE FROM emailbackup_mails WHERE id = ".$emailbackup_mails_id);
return(false);
} }
// END database import ticket
// Prüfen ob Ordner vorhanden ansonsten anlegen // File management folder with raw text
$ordner = $this->config->WFuserdata . '/emailbackup/' . $this->config->WFdbname . "/$id"; $ordner = $this->config->WFuserdata . '/emailbackup/' . $this->config->WFdbname . "/$emailbackup_mails_id";
if (!is_dir($ordner) && $id > 0) { if (!is_dir($ordner) && $emailbackup_mails_id > 0) {
if (!mkdir($ordner, 0777, true) && !is_dir($ordner)) { if (!mkdir($ordner, 0777, true) && !is_dir($ordner)) {
$this->logger->error("Folder \"{folder}\" was not created", ['folder' => $ordner]);
$this->db->Delete("DELETE FROM emailbackup_mails WHERE id = ".$emailbackup_mails_id);
return(false);
} }
$raw_full_email = $message->getRawContent(); $raw_full_email = $message->getRawContent();
file_put_contents($ordner . '/mail.txt', $raw_full_email); file_put_contents($ordner . '/mail.txt', $raw_full_email);
} }
//speichere anhang als datei // File management attachments
if ($anhang == 1 && $id > 0) { if ($anhang == 1 && $emailbackup_mails_id > 0) {
$ordner = $this->config->WFuserdata . '/emailbackup/' . $this->config->WFdbname; $ordner = $this->config->WFuserdata . '/emailbackup/' . $this->config->WFdbname;
if (!is_dir($ordner)) { if (!is_dir($ordner)) {
if (!mkdir($ordner, 0777, true) && !is_dir($ordner)) { if (!mkdir($ordner, 0777, true) && !is_dir($ordner)) {
$this->logger->error("Folder \"{folder}\" was not created", ['folder' => $ordner]); $this->logger->error("Folder \"{folder}\" was not created", ['folder' => $ordner]);
$this->db->Delete("DELETE FROM emailbackup_mails WHERE id = ".$emailbackup_mails_id);
return(false);
} }
} }
// Prüfen ob Ordner vorhanden ansonsten anlegen // Prüfen ob Ordner vorhanden ansonsten anlegen
$ordner = $this->config->WFuserdata . '/emailbackup/' . $this->config->WFdbname . "/$id"; $ordner = $this->config->WFuserdata . '/emailbackup/' . $this->config->WFdbname . "/$emailbackup_mails_id";
if (!is_dir($ordner)) { if (!is_dir($ordner)) {
if ($DEBUG) { if (!mkdir($ordner, 0777, true) && !is_dir($ordner)) {
echo "mkdir $ordner\n"; $this->logger->error("Folder \"{folder}\" was not created", ['folder' => $ordner]);
} else { $this->db->Delete("DELETE FROM emailbackup_mails WHERE id = ".$emailbackup_mails_id);
if (!mkdir($ordner, 0777, true) && !is_dir($ordner)) { return(false);
$this->logger->error("Folder \"{folder}\" was not created", ['folder' => $ordner]);
}
} }
} }
$this->logger->debug('Add attachments',['ticketnummer' => $ticketNumber, 'nachricht' => $ticketnachricht, 'count' => count($attachments)]); $this->logger->debug('Add '.count($attachments).' attachments',['']);
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if ($attachment->getFileName() !== '') { if ($attachment->getFileName() !== '') {
if ($DEBUG) { $handle = fopen($ordner . '/' . $attachment->getFileName(), 'wb');
} else { if ($handle) {
$handle = fopen($ordner . '/' . $attachment->getFileName(), 'wb'); fwrite($handle, $attachment->getContent());
if ($handle) { fclose($handle);
fwrite($handle, $attachment->getContent());
fclose($handle);
}
} }
//Schreibe Anhänge in Datei-Tabelle //Schreibe Anhänge in Datei-Tabelle
$datei = $ordner . '/' . $attachment->getFileName(); $datei = $ordner . '/' . $attachment->getFileName();
$dateiname = $attachment->getFileName(); $dateiname = $attachment->getFileName();
$this->logger->debug("Attachment", ['filename' => $dateiname]);
if (stripos(strtoupper($dateiname), '=?UTF-8') !== false) { if (stripos(strtoupper($dateiname), '=?UTF-8') !== false) {
$dateiname = $this->formatter->encodeToUtf8($dateiname); $dateiname = $this->formatter->encodeToUtf8($dateiname);
$dateiname = htmlspecialchars_decode($dateiname); $dateiname = htmlspecialchars_decode($dateiname);
@ -774,42 +780,31 @@ class TicketImportHelper
$dateiname = htmlspecialchars_decode($dateiname); $dateiname = htmlspecialchars_decode($dateiname);
} }
$this->logger->debug("Attachment cleaned", ['filename' => $dateiname]); $tmpid = $this->erpApi->CreateDatei(
$dateiname,
$dateiname,
'',
'',
$datei,
'Support Mail',
true,
$this->config->WFuserdata . '/dms/' . $this->config->WFdbname
);
if ($DEBUG) { $this->logger->debug('Add attachment',['filename' => $dateiname, 'ticketnummer' => $ticketNumber,'id' => $tmpid, 'nachricht' => $ticketnachricht]);
echo "CreateDatei($dateiname,{$dateiname},\"\",\"\",\"datei\",\"Support Mail\",true,"
. $this->config->WFuserdata . "/dms/" . $this->config->WFdbname . ")\n";
} else {
$tmpid = $this->erpApi->CreateDatei(
$dateiname,
$dateiname,
'',
'',
$datei,
'Support Mail',
true,
$this->config->WFuserdata . '/dms/' . $this->config->WFdbname
);
}
if ($DEBUG) { $this->erpApi->AddDateiStichwort(
echo "AddDateiStichwort $tmpid,'Anhang','Ticket',$ticketnachricht,true)\n"; $tmpid,
} else { 'Anhang',
'Ticket',
$this->logger->debug('Add attachment',['ticketnummer' => $ticketNumber,'id' => $tmpid, 'nachricht' => $ticketnachricht]); $ticketnachricht,
true
$this->erpApi->AddDateiStichwort( );
$tmpid,
'Anhang',
'Ticket',
$ticketnachricht,
true
);
}
} }
} }
} } // END File management
// Autoresponder
if ( if (
$this->mailAccount->isAutoresponseEnabled() $this->mailAccount->isAutoresponseEnabled()
&& $this->mailAccount->getAutoresponseText() !== '' && $this->mailAccount->getAutoresponseText() !== ''
@ -843,5 +838,7 @@ class TicketImportHelper
$text $text
); );
} }
return(true);
} }
} }