2021-05-21 08:49:41 +02:00
< ? php
declare ( strict_types = 1 );
namespace Xentral\Modules\Ticket\Task ;
use Config as LegacyConfig ;
use DB as LegacyDataBase ;
use erpAPI ;
use Throwable ;
use Ticket as TicketModule ;
use Xentral\Components\Logger\LoggerAwareTrait ;
use Xentral\Components\MailClient\Client\MailClientInterface ;
use Xentral\Components\MailClient\Data\MailMessageInterface ;
use Xentral\Modules\SystemMailer\Data\EmailBackupAccount ;
use Xentral\Modules\Ticket\Importer\TicketFormatter ;
2022-07-21 23:44:48 +02:00
use Xentral\Modules\Ticket\Exception\NumberGeneratorException ;
2022-07-27 18:05:24 +02:00
use Xentral\Modules\Ticket\Exception\InvalidArgumentException ;
2021-05-21 08:49:41 +02:00
/**
2022-07-21 23:44:48 +02:00
* Utility functions for tickets cronjob for improved testability
* This is used to create and update tickets from emailbackup , called by the tickets . php cronjob
* It uses the TicketService to process the tickets
2021-05-21 08:49:41 +02:00
*/
class TicketImportHelper
{
use LoggerAwareTrait ;
/** @var LegacyDataBase $db */
private $db ;
/** @var erpAPI $erpApi */
private $erpApi ;
/** @var LegacyConfig $config */
private $config ;
/** @var TicketModule $ticketModule */
private $ticketModule ;
/** @var TicketFormatter $formatter */
private $formatter ;
/** @var MailClientInterface $mailClient */
private $mailClient ;
/** @var EmailBackupAccount $mailAccount */
private $mailAccount ;
/** @var int $projectId */
private $projectId ;
/**
* @ param LegacyDataBase $db
* @ param erpAPI $erpApi
2022-07-21 23:44:48 +02:00
* @ param TicketModule $ticketModule
2021-05-21 08:49:41 +02:00
* @ param LegacyConfig $config
* @ param TicketFormatter $formatter
* @ param MailClientInterface $mailClient
* @ param EmailBackupAccount $mailAccount
* @ param int $projectId
*/
public function __construct (
LegacyDataBase $db ,
erpAPI $erpApi ,
TicketModule $ticketModule ,
LegacyConfig $config ,
TicketFormatter $formatter ,
MailClientInterface $mailClient ,
EmailBackupAccount $mailAccount ,
int $projectId
) {
$this -> db = $db ;
$this -> erpApi = $erpApi ;
$this -> ticketModule = $ticketModule ;
$this -> formatter = $formatter ;
$this -> mailClient = $mailClient ;
$this -> mailAccount = $mailAccount ;
$this -> projectId = $projectId ;
$this -> config = $config ;
}
2022-07-21 23:44:48 +02:00
/* Function from AddressWrapper ...
Still using legacy db
*/
public function tryGetAddressIdByEmailAddress ( string $emailAddress ) : ? int
{
$searchByEmail = ' SELECT a . id FROM `adresse` AS `a`
WHERE a . email LIKE \ '' . $emailAddress . ' \ ' AND a . geloescht = 0
ORDER BY a . id DESC ' ;
$id = $this -> db -> Select ( $searchByEmail );
if ( $id !== null && $id > 0 ) {
2022-08-05 17:08:41 +02:00
return ( int ) $id ;
2022-07-21 23:44:48 +02:00
}
$searchByResponsePerson = ' SELECT ap . adresse FROM `ansprechpartner` AS `ap`
WHERE ap . email LIKE \ '' . $emailAddress . ' \ '
ORDER BY ap . id DESC ' ;
$id = $this -> db -> Select ( $searchByResponsePerson );
if ( $id !== null && $id > 0 ) {
2022-08-05 17:08:41 +02:00
return ( int ) $id ;
2022-07-21 23:44:48 +02:00
}
$searchByContactInfo = ' SELECT ak . adresse FROM `adresse_kontakte` AS `ak`
WHERE ak . kontakt LIKE \ '' . $emailAddress . '\' ORDER BY ak.id DESC' ;
$id = $this -> db -> Select ( $searchByContactInfo );
2022-08-05 17:08:41 +02:00
return ( int ) $id ;
2022-07-21 23:44:48 +02:00
}
/* Some functions taken from TicketService (sorry...) */
/**
* @ param string $ticketNumber
*
* @ return void
*/
public function markTicketMessagesCompleted ( string $ticketNumber ) : void
{
$this -> setTicketMessagesStatus ( $ticketNumber , 'abgeschlossen' ); // TicketGateway::STATUS_COMPLETED);
}
/**
* @ param string $ticketNumber
* @ param string $status
*
* @ return void
*/
private function setTicketMessagesStatus ( string $ticketNumber , string $status ) : void
{
$sql = " UPDATE `ticket_nachricht` SET `status` = ' " . $status . " ' WHERE `ticket` = ' " . $ticketNumber . " '; " ;
$this -> db -> Update ( $sql );
}
/**
* @ param string $ticketNumber
*
* @ throws InvalidArgumentException
*
* @ return void
*/
public function resetTicketStatus ( string $ticketNumber ) : void
{
$this -> ensureTicketNumberExists ( $ticketNumber );
$sql = " UPDATE `ticket` SET
`status` = 'neu' ,
`zugewiesen` = '0' ,
2022-07-31 18:23:43 +02:00
`inbearbeitung` = '0' ,
`zeit` = now ()
2022-07-26 17:36:24 +02:00
WHERE `schluessel` LIKE '".$ticketNumber."' " ;
2022-07-21 23:44:48 +02:00
$this -> db -> Update ( $sql );
}
/**
* @ param string $ticketNumber
*
* @ return void
*/
public function updateTicketMessagesCount ( string $ticketNumber ) : void
{
$this -> ensureTicketNumberExists ( $ticketNumber );
$count = $this -> getMessageCountByTicketNumber ( $ticketNumber );
$count ++ ;
$sql = " UPDATE `ticket` SET `nachrichten_anz` = ' " . $count . " ' WHERE schluessel = ' " . $tickerNumber . " '; " ;
$this -> db -> Update ( $sql );
}
/**
* @ param string $ticketNumber
*
* @ return void
*/
private function ensureTicketNumberExists ( string $ticketNumber ) : void
{
2022-07-27 18:05:24 +02:00
if ( $ticketNumber == '' ) {
throw new InvalidArgumentException (
sprintf ( 'ticket number empty' )
);
}
if ( ! $this -> db -> Select ( 'SELECT id FROM ticket WHERE schluessel = ' . $ticketNumber )) {
2022-07-21 23:44:48 +02:00
throw new InvalidArgumentException (
sprintf ( 'ticket number "%s" does not exist' , $ticketNumber )
);
}
}
/**
* @ throws NumberGeneratorException
*
* @ return string
*/
private function generateRandomTicketNumber () : string
{
$random = rand ( 300 , 700 );
$loopCounter = 0 ;
while ( true ) {
$candidate = sprintf ( '%s%04d' , date ( 'Ymd' ), $random ++ );
/* if ( ! $this -> gateway -> existsTicketNumber ( $candidate )) {
return $candidate ;
} */
if ( ! $this -> db -> Select ( 'SELECT id FROM ticket WHERE schluessel = ' . $candidate )) {
return ( $candidate );
}
2023-02-01 19:02:16 +01:00
if ( $loopCounter > 9999 ) {
2022-07-21 23:44:48 +02:00
throw new NumberGeneratorException ( 'ticket number generation failed' );
}
$loopCounter ++ ;
}
}
2022-08-07 11:50:13 +02:00
/* Functions from Gateway */
2022-07-21 23:44:48 +02:00
/**
* @ param string $ticketNumber
*
* @ return int
*/
public function getMessageCountByTicketNumber ( string $ticketNumber ) : int
{
$sql = " SELECT COUNT(tm.id) FROM `ticket_nachricht` AS `tm` WHERE tm.ticket = ' " . $ticketNumber . " '; " ;
$count = $this -> db -> Select ( $sql );
if ( $count === null ) {
return 0 ;
}
return ( int ) $count ;
}
2022-08-07 11:50:13 +02:00
/**
* @ param string $recipientMail
* @ param string $senderMail
* @ param string $senderName
* @ param string $subject
*
* @ return array
*/
public function getTicketRules (
string $recipientMail ,
string $senderMail ,
string $senderName ,
string $subject
) : array {
/*
$sql = " SELECT
tr . spam AS `is_spam` ,
tr . dsgvo AS `is_gdpr_relevant` ,
tr . prio AS `priority` ,
tr . persoenlich AS `is_private` ,
tr . warteschlange AS `queue_id`
FROM `ticket_regeln` AS `tr`
WHERE
tr . aktiv = 1
AND ( tr . empfaenger_email LIKE : source_email OR empfaenger_email = '' )
AND (
tr . sender_email LIKE : sender_email
OR ( tr . sender_email LIKE '@%' AND : sender_email LIKE CONCAT ( '%' , tr . sender_email ))
OR tr . sender_email = ''
)
AND ( tr . name LIKE : sender_name OR tr . name = '' )
AND ( tr . betreff LIKE : subject OR tr . betreff = '' ) " ;
$values = [
'source_email' => $recipientMail ,
'sender_email' => $senderMail ,
'sender_name' => $senderName ,
'subject' => $subject ,
];
return $this -> db -> fetchAll ( $sql , $values );
*/
// Legacy DB implementation:
$sql = " SELECT
tr . id ,
tr . spam AS `is_spam` ,
tr . dsgvo AS `is_gdpr_relevant` ,
tr . prio AS `priority` ,
tr . persoenlich AS `is_private` ,
2023-11-25 11:17:11 +01:00
tr . adresse ,
2022-08-07 11:50:13 +02:00
tr . warteschlange AS `queue_id`
FROM `ticket_regeln` AS `tr`
WHERE
tr . aktiv = 1
2023-01-28 16:32:34 +01:00
AND ( '".$this->db->real_escape_string($recipientMail)."' LIKE tr . empfaenger_email OR tr . empfaenger_email = '' )
AND ( '".$this->db->real_escape_string($senderMail)."' LIKE tr . sender_email OR tr . sender_email = '' )
AND ( '".$this->db->real_escape_string($senderMail)."' LIKE tr . name OR tr . name = '' )
AND ( '".$this->db->real_escape_string($subject)."' LIKE tr . betreff OR tr . betreff = '' ) " ;
2022-08-07 11:50:13 +02:00
$this -> logger -> debug ( 'ticket rule' ,[ 'sql' => $sql ]);
$result = $this -> db -> SelectArr ( $sql );
if ( $result != null ) {
$this -> logger -> debug ( 'ticket rules' ,[ 'count' , count ( $result )]);
return ( $result );
} else {
$this -> logger -> debug ( 'no ticket rules applicable' ,[ '' ]);
return ( array ());
}
}
/* END Functions from Gateway */
2022-07-21 23:44:48 +02:00
public function createTicket (
int $projectId ,
string $senderName ,
string $senderAddress ,
string $subject ,
int $timestamp ,
string $replyToName ,
string $replyToAddress
2022-07-27 18:05:24 +02:00
) : string
2022-07-21 23:44:48 +02:00
{
2022-08-05 17:08:41 +02:00
$AddressId = $this -> tryGetAddressIdByEmailAddress ( $senderAddress );
2022-07-21 23:44:48 +02:00
$ticketNumber = $this -> generateRandomTicketNumber ();
if ( $projectId < 1 ) {
$projectId = $this -> mailAccount -> getProjectId ();
}
if ( $this -> mailAccount -> isTicketMarkAsFinishedEnabled ()) {
$status = 'abgeschlossen' ; //TicketGateway::STATUS_COMPLETED;
} else {
$status = 'neu' ; //TicketGateway::STATUS_NEW;
}
2022-08-05 17:08:41 +02:00
$queue_id = $this -> mailAccount -> getTicketQueueId ();
if ( ! empty ( $queue_id )) {
2023-06-23 15:54:33 +02:00
$queue_label = $this -> db -> Select ( " SELECT label FROM warteschlangen WHERE label = ' " . $queue_id . " ' LIMIT 1 " );
2022-08-05 17:08:41 +02:00
}
2023-01-04 12:27:34 +01:00
2022-07-21 23:44:48 +02:00
$insertTicket = " INSERT INTO `ticket` (
`schluessel` , `zeit` , `projekt` , `quelle` , `status` , `kunde` ,
`mailadresse` , `prio` , `betreff` , `warteschlange` , `adresse`
) VALUES (
'".$ticketNumber."' ,
'".date(' Y - m - d H : i : s ', $timestamp)."' ,
'".$projectId."' ,
'".$this->mailAccount->getEmailAddress()."' ,
'".$status."' ,
2023-01-04 12:27:34 +01:00
'".$this->db->real_escape_string($senderName)."' ,
'".$this->db->real_escape_string($senderAddress)."' ,
2022-07-21 23:44:48 +02:00
'".' 3 '."' ,
2023-01-04 12:27:34 +01:00
'".$this->db->real_escape_string($subject)."' ,
2022-08-05 17:08:41 +02:00
'".$queue_label."' ,
'".$AddressId."' ); " ;
2022-07-21 23:44:48 +02:00
$this -> db -> Insert ( $insertTicket );
$ticketId = $this -> db -> GetInsertID ();
2022-08-05 17:08:41 +02:00
$this -> logger -> debug ( 'inserted ticket' ,[ 'id' => $ticketId , 'ticketnr' => $ticketNumber , 'projekt' => $projectId , 'warteschlange' => $this -> mailAccount -> getTicketQueueId (), 'adresse' => $AddressId ]);
2022-07-21 23:44:48 +02:00
// todo als rueckgabe ticketnachricht
2022-07-27 18:05:24 +02:00
return $ticketNumber ;
2022-07-21 23:44:48 +02:00
}
public function addTicketMessage (
string $ticketNumber ,
int $timestamp ,
string $message ,
string $subject ,
string $senderName ,
string $senderAddress ,
string $status ,
string $replyToName ,
string $replyToAddress
) : int
{
// $this->db->beginTransaction();
try {
$sql = " INSERT INTO `ticket_nachricht` (
`ticket` , `zeit` , `text` , `betreff` , `medium` ,
`verfasser` , `mail` , `status` , `verfasser_replyto` , `mail_replyto`
) VALUES (
'".$ticketNumber."' ,
'".date(' Y - m - d H : i : s ', $timestamp)."' ,
2023-01-04 12:27:34 +01:00
'".$this->db->real_escape_string($message)."' ,
'".$this->db->real_escape_string($subject)."' ,
2022-07-21 23:44:48 +02:00
'".' email '."' ,
2023-01-04 12:27:34 +01:00
'".$this->db->real_escape_string($senderName)."' ,
'".$this->db->real_escape_string($senderAddress)."' ,
2022-07-21 23:44:48 +02:00
'".$status."' ,
2023-01-04 12:27:34 +01:00
'".$this->db->real_escape_string($replyToName)."' ,
'".$this->db->real_escape_string($replyToAddress)."' ); " ;
2022-07-21 23:44:48 +02:00
$this -> logger -> debug ( 'database insert' ,[ 'query' => $sql ]);
$this -> db -> Insert ( $sql );
$messageId = $this -> db -> GetInsertID ();
2022-07-27 18:05:24 +02:00
$this -> logger -> debug ( 'inserted' ,[ 'id' => $messageId , 'schluessel' => $ticketNumber ]);
2022-07-21 23:44:48 +02:00
$this -> updateTicketMessagesCount ( $ticketNumber );
$this -> resetTicketStatus ( $ticketNumber );
// $this->db->commit();
2022-08-07 12:07:55 +02:00
$result = $messageId ;
2022-08-07 11:50:13 +02:00
$this -> markTicketMessagesCompleted ( $ticketNumber );
2022-07-21 23:44:48 +02:00
} catch ( Throwable $e ) {
// $this->db->rollBack();
2022-08-07 12:07:55 +02:00
$result = 0 ;
2022-07-21 23:44:48 +02:00
$this -> logger -> error ( 'Failed to insert ticket message into db' , [ 'exception' => $e ]);
2022-08-07 11:50:13 +02:00
}
2022-08-07 12:07:55 +02:00
return ( $result );
2022-08-07 11:50:13 +02:00
}
public function applyTicketRules ( int $ticketMessageId ) : void
{
/* $ticketData = $this->gateway->tryGetTicketDataByByMessage($ticketMessageId); */
$ticketData = $this -> db -> SelectArr ( " SELECT t.id, tn.mail as sender_email, tn.verfasser as sender_name, tn.betreff as subject, t.quelle as source_email FROM ticket t INNER JOIN ticket_nachricht tn ON tn.ticket = t.schluessel WHERE tn.id = ' " . $ticketMessageId . " ' " )[ 0 ];
if ( $ticketData === null ) {
throw new InvalidArgumentException ( 'cannot find ticket by message id' );
}
$ticketId = $ticketData [ 'id' ];
$senderMail = $ticketData [ 'sender_email' ];
$senderName = $ticketData [ 'sender_name' ];
$subject = $ticketData [ 'subject' ];
$source = $ticketData [ 'source_email' ];
$this -> logger -> debug ( 'check ticket rules' ,[ 'tn_id' => $ticketMessageId , 'sender_email' => $senderMail , 'subject' => $subject ]);
//TODO: richtig loggen: $this->app->erp->LogFile("Empfaengermail: $quelle Sendermail: $mailadresse Kunde: $kunde Betreff: $betreff");
$ruleArray = $this -> getTicketRules ( $source , $senderMail , $senderName , $subject );
if ( empty ( $ruleArray )) {
return ;
}
foreach ( $ruleArray as $rule ) {
2023-08-03 12:01:23 +02:00
$this -> logger -> debug ( 'ticket rule applies' ,[ 'rule_id' => $rule [ 'id' ], 'rule' => print_r ( $rule , true )]);
2022-08-07 11:50:13 +02:00
/*
$update = $this -> db -> update ();
$update -> table ( 'ticket' );
if ( $rule [ 'is_spam' ] === 1 ) {
$update -> set ( 'inbearbeitung' , 0 );
$update -> set ( 'zugewiesen' , 1 );
$this -> db -> perform (
'UPDATE `ticket_nachricht` SET `status` = :status WHERE `ticket` = :ticket_id' ,
[ 'status' => TicketGateway :: STATUS_SPAM , 'ticket_id' => $ticketId ]
);
}
$update -> set ( 'dsgvo' , $rule [ 'is_gdpr_relevant' ]);
$update -> set ( 'privat' , $rule [ 'is_private' ] );
$update -> set ( 'prio' , $rule [ 'priority' ] );
$update -> where ( 'id = :ticket_id' );
$sql = $update -> getStatement ();
$this -> db -> perform ( $sql , [ 'ticket_id' => $ticketId ]);
*/
2023-08-03 12:01:23 +02:00
if ( $rule [ 'is_spam' ] == 1 ) {
$status = 'spam' ;
} else {
$status = 'neu' ;
2022-08-07 11:50:13 +02:00
}
2023-11-25 11:17:11 +01:00
$sql = " UPDATE `ticket` SET `dsgvo` = ' " . $rule [ 'is_gdpr_relevant' ] . " ', `privat` = ' " . $rule [ 'is_private' ] . " ', `prio` = ' " . $rule [ 'priority' ] . " ',`adresse` = ' " . $rule [ 'adresse' ] . " ', `warteschlange` = ' " . $rule [ 'queue_id' ] . " ', `status` = ' " . $status . " ' WHERE `id` = ' " . $ticketId . " ' " ;
2023-08-03 12:01:23 +02:00
$this -> logger -> debug ( 'ticket rule sql' ,[ 'sql' => $sql ]);
2022-08-07 11:50:13 +02:00
$this -> db -> Update ( $sql );
2022-07-21 23:44:48 +02:00
}
}
2022-08-07 11:50:13 +02:00
2022-07-21 23:44:48 +02:00
/* End TicketService */
2021-05-21 08:49:41 +02:00
/**
* @ param array $inboxMessageIds
*
* @ return int amount of imported tickets
*/
public function importMessages ( array $inboxMessageIds ) : int
{
$insertedMailsCount = 0 ;
foreach ( $inboxMessageIds as $messageNumber ) {
2023-02-01 19:02:16 +01:00
$this -> logger -> debug ( " Fetch $messageNumber " , [ '' ]);
2021-05-21 08:49:41 +02:00
try {
$message = $this -> mailClient -> fetchMessage (( int ) $messageNumber );
} catch ( Throwable $e ) {
$this -> logger -> error ( 'Failed to fetch email from server' , [ 'exception' => $e ]);
continue ;
}
try {
2022-08-07 11:50:13 +02:00
2023-02-01 19:02:16 +01:00
// $this->logger->debug('Start import', ['message' => substr(print_r($message,true),1000)]);
2023-02-02 16:39:00 +01:00
$this -> logger -> debug ( 'Start import ' . $messageNumber , []);
2022-08-07 11:50:13 +02:00
2022-12-21 21:34:00 +01:00
$result = $this -> importMessage ( $message );
if ( $result === true ) {
$insertedMailsCount ++ ;
if ( $this -> mailAccount -> isDeleteAfterImportEnabled ()) {
$this -> mailClient -> deleteMessage (( int ) $messageNumber );
} else {
$this -> mailClient -> setFlags (( int ) $messageNumber , [ '\\Seen' ]);
}
2021-05-21 08:49:41 +02:00
} else {
2023-02-02 16:39:00 +01:00
$this -> logger -> error ( 'Error during email import ' . $messageNumber , [ 'message' => substr ( print_r ( $message , true ), 0 , 1000 )]);
2023-02-01 19:02:16 +01:00
continue ;
2021-05-21 08:49:41 +02:00
}
} catch ( Throwable $e ) {
2023-04-17 18:21:43 +02:00
$exception_message = $e -> getMessage ();
$this -> logger -> error ( 'Error during email import ' . $messageNumber , [ 'exc-message' => $exception_message , 'message2' => substr ( print_r ( $message , true ), 0 , 1000 )]);
2021-05-21 08:49:41 +02:00
continue ;
}
}
$this -> logger -> debug (
'{imported_count} of {total} emails imported successfully' ,
[ 'imported_count' => $insertedMailsCount , 'total' => count ( $inboxMessageIds )]
);
return $insertedMailsCount ;
}
/**
* @ param MailMessageInterface $message
*
2022-12-21 21:34:00 +01:00
* @ return true on success
2021-05-21 08:49:41 +02:00
*/
2022-12-21 21:34:00 +01:00
public function importMessage ( MailMessageInterface $message ) : bool
2021-05-21 08:49:41 +02:00
{
// extract email data
$subject = $this -> formatter -> encodeToUtf8 ( $message -> getSubject ());
$from = $this -> formatter -> encodeToUtf8 ( $message -> getSender () -> getEmail ());
2022-07-21 23:44:48 +02:00
$fromname = $this -> formatter -> encodeToUtf8 ( $message -> getSender () -> getName ());
2021-05-21 08:49:41 +02:00
$ccs = $message -> getCcRecipients ();
$cc_recv = [];
foreach ( $ccs as $cc ) {
$cc_recv [] = [
'email' => $this -> formatter -> encodeToUtf8 ( $cc -> getEmail ()),
'name' => $this -> formatter -> encodeToUtf8 ( $cc -> getName ()),
];
}
$plainTextBody = $message -> getPlainTextBody ();
if ( $plainTextBody === null ) {
$plainTextBody = '' ;
}
$htmlBody = $message -> getHtmlBody ();
if ( $htmlBody === null ) {
$htmlBody = '' ;
2023-01-12 00:01:20 +01:00
}
if ( $plainTextBody == '' && $htmlBody == '' ) {
$simple_content = $message -> getContent ();
if ( empty ( $simple_content )) {
2023-02-01 19:02:16 +01:00
$this -> logger -> debug ( 'Empty mail' ,[]);
2023-01-12 00:01:20 +01:00
} else {
$plainTextBody = $simple_content ;
$htmlBody = nl2br ( htmlentities ( $simple_content ));
}
2021-05-21 08:49:41 +02:00
}
2023-01-12 00:01:20 +01:00
$this -> logger -> debug ( 'Text' ,[ 'plain' => $plainTextBody , 'html' => $htmlBody , 'simple_content' => $simple_content ]);
2021-05-21 08:49:41 +02:00
$action = $this -> formatter -> encodeToUtf8 ( $plainTextBody );
$action_html = $this -> formatter -> encodeToUtf8 ( $htmlBody );
if ( strlen ( $action_html ) < strlen ( $action )) {
$action_html = nl2br ( $action );
}
2023-01-12 00:01:20 +01:00
$this -> logger -> debug ( 'Text (converted)' ,[ 'plain' => $action , 'html' => $action_html ]);
2022-12-21 21:34:00 +01:00
// Import database emailbackup
2023-04-17 18:21:43 +02:00
try {
$date = $message -> getDate ();
}
catch ( exception $e ) {
$this -> logger -> debug ( 'Invalid date' ,[ 'exc-message' => $e -> getMessage (), 'subject' => $message -> getSubject (), $message -> getHeader ( 'date' ) -> getValue ()]);
2023-01-04 12:27:34 +01:00
return ( false );
2022-07-24 21:10:33 +02:00
}
2023-04-17 18:21:43 +02:00
$timestamp = $date -> getTimestamp ();
$frommd5 = md5 ( $from . $subject . $timestamp );
2021-05-21 08:49:41 +02:00
$empfang = $date -> format ( 'Y-m-d H:i:s' );
$sql = " SELECT COUNT(id)
FROM `emailbackup_mails`
WHERE `checksum` = '$frommd5'
AND `empfang` = '$empfang'
2022-12-21 21:34:00 +01:00
AND `ticketnachricht` != 0
2021-05-21 08:49:41 +02:00
AND `webmail` = '" . $this->mailAccount->getId() . "' " ;
2022-07-21 23:44:48 +02:00
$this -> logger -> debug ( 'Importing message ' . $from . ' ' . $fromname );
2022-12-21 21:34:00 +01:00
$result = $this -> db -> Select ( $sql );
$emailbackup_mails_id = null ;
if ( $result == 0 ) {
2022-07-21 23:44:48 +02:00
2023-02-01 19:02:16 +01:00
// $this->logger->debug('Importing message',['message' => substr(print_r($message,true),1000)]);
$this -> logger -> debug ( 'Importing message attachments' ,[]);
2022-07-21 23:44:48 +02:00
2023-02-01 16:55:54 +01:00
try {
$attachments = $message -> getAttachments ();
}
catch ( Throwable $e ) {
$this -> logger -> error ( 'Error while getting attachments' ,[ 'exception' => $e ]);
return ( false );
}
2021-05-21 08:49:41 +02:00
$anhang = count ( $attachments ) > 0 ? 1 : 0 ;
$mailacc = $this -> mailAccount -> getEmailAddress ();
$mailaccid = $this -> mailAccount -> getId ();
if ( ! $this -> erpApi -> isMailAdr ( $from )) {
$from = $this -> erpApi -> filterMailAdr ( $from );
}
//fuege gegenenfalls ein
$sql = " INSERT INTO `emailbackup_mails`
(
`webmail` ,
`subject` ,
`sender` ,
`action` ,
`action_html` ,
`empfang` ,
`anhang` ,
`checksum`
) VALUES (
'$mailaccid' ,
'" . $this->db->real_escape_string($subject) . "' ,
'" . $this->db->real_escape_string($from) . "' ,
'" . $this->db->real_escape_string($action) . "' ,
'" . $this->db->real_escape_string($action_html) . "' ,
'$empfang' , '$anhang' , '$frommd5'
) " ;
2022-12-21 21:34:00 +01:00
$this -> db -> InsertWithoutLog ( $sql );
$emailbackup_mails_id = $this -> db -> GetInsertID ();
} else {
$this -> logger -> debug ( 'Message already imported.' ,[ '' ]);
return ( true );
}
$this -> logger -> debug ( 'Message emailbackup_mails imported.' ,[ 'id' => $emailbackup_mails_id ]);
// END database import emailbackup
// Find ticket and add or create new ticket
2022-07-27 18:05:24 +02:00
$ticketNumber = null ;
2022-07-21 23:44:48 +02:00
$ticketexists = null ;
if ( preg_match ( " /Ticket #[0-9] { 12}/i " , $subject , $matches )) {
2022-07-27 18:05:24 +02:00
$ticketNumber = str_replace ( 'Ticket #' , '' , $matches [ 0 ]);
2022-12-21 21:34:00 +01:00
$this -> logger -> debug ( 'Check for number' ,[ 'ticketnummer' => $ticketNumber ]);
2022-07-21 23:44:48 +02:00
$ticketexists = $this -> db -> Select (
" SELECT schluessel
FROM ticket
2022-07-27 18:05:24 +02:00
WHERE schluessel LIKE '" . $ticketNumber . "'
2022-07-21 23:44:48 +02:00
AND schluessel != '' LIMIT 1 "
);
}
$ticketnachricht = null ;
if ( ! $ticketexists ) {
$this -> logger -> debug ( 'New ticket' ,[ '' ]);
$ticketNumber = $this -> createTicket (
$this -> projectId ,
$fromname ,
$from ,
$subject ,
$timestamp ,
$fromname ,
$from
);
2022-07-24 21:10:33 +02:00
} else {
2022-07-27 18:05:24 +02:00
$this -> logger -> debug ( 'Add message to existing ticket' ,[ 'ticketnummer' => $ticketNumber ]);
2022-07-21 23:44:48 +02:00
}
2022-12-21 21:34:00 +01:00
// Database import ticket: Add message to new or existing ticket
2022-07-21 23:44:48 +02:00
$ticketnachricht = $this -> addTicketMessage (
2022-07-27 18:05:24 +02:00
( string ) $ticketNumber ,
2022-07-21 23:44:48 +02:00
$timestamp ,
$action_html , //?
$subject ,
$fromname ,
$from ,
2022-07-26 17:36:24 +02:00
'neu' ,
2022-07-21 23:44:48 +02:00
$fromname ,
$from
);
2021-05-21 08:49:41 +02:00
2023-08-03 12:01:23 +02:00
// Only for new tickets: apply filter rules
if ( ! $ticketexists ) {
$this -> applyTicketRules ( $ticketnachricht );
}
2022-12-21 21:34:00 +01:00
if ( $ticketnachricht > 0 && $emailbackup_mails_id > 0 ) {
2022-07-21 23:44:48 +02:00
$this -> db -> Update (
" UPDATE `emailbackup_mails`
SET ticketnachricht = '$ticketnachricht'
2022-12-21 21:34:00 +01:00
WHERE id = '$emailbackup_mails_id' LIMIT 1 "
2022-07-21 23:44:48 +02:00
);
2022-08-01 22:55:52 +02:00
// Add all the ccs to the header table
2022-07-21 23:44:48 +02:00
if ( is_array ( $cc_recv )) {
foreach ( $cc_recv as $mail ) {
if ( $mail [ 'name' ] != '' ) {
$cc_value =
$this -> db -> real_escape_string ( $mail [ 'name' ])
. ' <'
. $this -> db -> real_escape_string ( $mail [ 'email' ])
. " > " ;
2021-05-21 08:49:41 +02:00
} else {
2022-07-21 23:44:48 +02:00
$cc_value = $this -> db -> real_escape_string ( $mail [ 'email' ]);
}
if ( $cc_value != '' ) {
$sql = " INSERT INTO ticket_header
( `id` , `ticket_nachricht` , `type` , `value` )
VALUES
( '' , '$ticketnachricht' , 'cc' , '" . $cc_value . "' ) " ;
$this -> db -> InsertWithoutLog ( $sql );
2021-05-21 08:49:41 +02:00
}
}
2022-07-21 23:44:48 +02:00
$cc_recv = [];
2021-05-21 08:49:41 +02:00
}
2022-08-01 22:55:52 +02:00
2022-08-07 12:07:55 +02:00
$this -> logger -> debug ( 'Add recipients to header' ,[ 'count' => count ( $message -> getRecipients ())]);
2022-08-01 22:55:52 +02:00
// Add all the recipients to the header table
2022-08-03 10:30:05 +02:00
if ( count ( $message -> getRecipients ()) > 0 ) {
2022-08-01 22:55:52 +02:00
foreach ( $message -> getRecipients () as $recipient ) {
$recipient_address = $this -> db -> real_escape_string ( $recipient -> getEmail ());
2022-08-07 12:07:55 +02:00
$this -> logger -> debug ( 'Add recipient to header' ,[ 'address' => $recipient_address ]);
2022-08-01 22:55:52 +02:00
if ( $recipient_address != '' ) {
$sql = " INSERT INTO ticket_header
( `id` , `ticket_nachricht` , `type` , `value` )
VALUES
( '' , '$ticketnachricht' , 'to' , '" . $recipient_address . "' ) " ;
$this -> db -> InsertWithoutLog ( $sql );
}
}
}
2022-12-21 21:34:00 +01:00
} 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 );
2022-07-21 23:44:48 +02:00
}
2022-12-21 21:34:00 +01:00
// END database import ticket
// File management folder with raw text
$ordner = $this -> config -> WFuserdata . '/emailbackup/' . $this -> config -> WFdbname . " / $emailbackup_mails_id " ;
if ( ! is_dir ( $ordner ) && $emailbackup_mails_id > 0 ) {
2022-07-21 23:44:48 +02:00
if ( ! mkdir ( $ordner , 0777 , true ) && ! is_dir ( $ordner )) {
2022-12-21 21:34:00 +01:00
$this -> logger -> error ( " Folder \" { folder} \" was not created " , [ 'folder' => $ordner ]);
$this -> db -> Delete ( " DELETE FROM emailbackup_mails WHERE id = " . $emailbackup_mails_id );
return ( false );
2021-05-21 08:49:41 +02:00
}
2022-07-21 23:44:48 +02:00
$raw_full_email = $message -> getRawContent ();
file_put_contents ( $ordner . '/mail.txt' , $raw_full_email );
}
2021-05-21 08:49:41 +02:00
2022-12-21 21:34:00 +01:00
// File management attachments
if ( $anhang == 1 && $emailbackup_mails_id > 0 ) {
2022-07-21 23:44:48 +02:00
$ordner = $this -> config -> WFuserdata . '/emailbackup/' . $this -> config -> WFdbname ;
if ( ! is_dir ( $ordner )) {
2021-05-21 08:49:41 +02:00
if ( ! mkdir ( $ordner , 0777 , true ) && ! is_dir ( $ordner )) {
2022-07-21 23:44:48 +02:00
$this -> logger -> error ( " Folder \" { folder} \" was not created " , [ 'folder' => $ordner ]);
2022-12-21 21:34:00 +01:00
$this -> db -> Delete ( " DELETE FROM emailbackup_mails WHERE id = " . $emailbackup_mails_id );
return ( false );
2021-05-21 08:49:41 +02:00
}
}
2022-07-21 23:44:48 +02:00
// Prüfen ob Ordner vorhanden ansonsten anlegen
2022-12-21 21:34:00 +01:00
$ordner = $this -> config -> WFuserdata . '/emailbackup/' . $this -> config -> WFdbname . " / $emailbackup_mails_id " ;
2022-07-21 23:44:48 +02:00
if ( ! is_dir ( $ordner )) {
2022-12-21 21:34:00 +01:00
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 );
2021-05-21 08:49:41 +02:00
}
2022-07-21 23:44:48 +02:00
}
2022-12-21 21:34:00 +01:00
$this -> logger -> debug ( 'Add ' . count ( $attachments ) . ' attachments' ,[ '' ]);
2022-07-27 18:05:24 +02:00
2022-07-21 23:44:48 +02:00
foreach ( $attachments as $attachment ) {
if ( $attachment -> getFileName () !== '' ) {
2022-12-21 21:34:00 +01:00
$handle = fopen ( $ordner . '/' . $attachment -> getFileName (), 'wb' );
if ( $handle ) {
fwrite ( $handle , $attachment -> getContent ());
fclose ( $handle );
2021-05-21 08:49:41 +02:00
}
2022-07-21 23:44:48 +02:00
//Schreibe Anhänge in Datei-Tabelle
$datei = $ordner . '/' . $attachment -> getFileName ();
$dateiname = $attachment -> getFileName ();
2022-07-27 18:05:24 +02:00
if ( stripos ( strtoupper ( $dateiname ), '=?UTF-8' ) !== false ) {
2022-07-21 23:44:48 +02:00
$dateiname = $this -> formatter -> encodeToUtf8 ( $dateiname );
$dateiname = htmlspecialchars_decode ( $dateiname );
}
2022-07-27 18:05:24 +02:00
if ( stripos ( strtoupper ( $dateiname ), '=?ISO-8859' ) !== false ) {
2022-07-21 23:44:48 +02:00
$dateiname = $this -> formatter -> encodeToUtf8 ( $dateiname );
$dateiname = htmlspecialchars_decode ( $dateiname );
}
2022-08-20 11:59:32 +02:00
if ( stripos ( strtoupper ( $dateiname ), 'UTF-8\'\'' ) === 0 ) {
2022-08-14 20:18:15 +02:00
$dateiname = $this -> formatter -> encodeToUtf8 ( urldecode ( substr ( $dateiname , 7 )));
$dateiname = htmlspecialchars_decode ( $dateiname );
}
2022-12-21 21:34:00 +01:00
$tmpid = $this -> erpApi -> CreateDatei (
$dateiname ,
$dateiname ,
'' ,
'' ,
$datei ,
'Support Mail' ,
true ,
$this -> config -> WFuserdata . '/dms/' . $this -> config -> WFdbname
);
$this -> logger -> debug ( 'Add attachment' ,[ 'filename' => $dateiname , 'ticketnummer' => $ticketNumber , 'id' => $tmpid , 'nachricht' => $ticketnachricht ]);
$this -> erpApi -> AddDateiStichwort (
$tmpid ,
'Anhang' ,
'Ticket' ,
$ticketnachricht ,
true
);
2021-05-21 08:49:41 +02:00
}
}
2022-12-21 21:34:00 +01:00
} // END File management
2022-07-21 23:44:48 +02:00
2022-12-21 21:34:00 +01:00
// Autoresponder
2022-07-21 23:44:48 +02:00
if (
$this -> mailAccount -> isAutoresponseEnabled ()
&& $this -> mailAccount -> getAutoresponseText () !== ''
&& (
2023-06-23 08:42:28 +02:00
// $this->erpApi->AutoresponderBlacklist($from) !== 1 ||
$this -> mailAccount -> isAutoresponseLimitEnabled () === false
2022-07-21 23:44:48 +02:00
)
) {
2021-05-21 08:49:41 +02:00
$text = $this -> mailAccount -> getAutoresponseText ();
$betreff = $this -> mailAccount -> getAutoresponseSubject ();
2022-07-21 23:44:48 +02:00
if ( empty ( $text )) $text = '' ;
if ( empty ( $betreff )) $betreff = '' ;
2022-07-27 18:05:24 +02:00
$text = str_replace ( '{TICKET}' , $ticketNumber , $text );
2021-05-21 08:49:41 +02:00
$text = str_replace ( '{BETREFF}' , $subject , $text );
2022-07-27 18:05:24 +02:00
$betreff = str_replace ( '{TICKET}' , $ticketNumber , $betreff );
2021-05-21 08:49:41 +02:00
$betreff = str_replace ( '{BETREFF}' , $subject , $betreff );
2022-07-21 23:44:48 +02:00
if ( ! $this -> erpApi -> isHTML ( $text )) {
$text = str_replace ( " \r \n " , '<br>' , $text );
2021-05-21 08:49:41 +02:00
}
2022-07-21 23:44:48 +02:00
$this -> erpApi -> MailSend (
$this -> mailAccount -> getSenderEmailAddress (),
'' ,
$from ,
$name_sender ,
$betreff ,
$text
);
2021-05-21 08:49:41 +02:00
}
2022-12-21 21:34:00 +01:00
return ( true );
2022-07-21 23:44:48 +02:00
}
2021-05-21 08:49:41 +02:00
}