From eed70bf673ba6459cad822396d16fb8d40ad9817 Mon Sep 17 00:00:00 2001
From: Xenomporio <>
Date: Thu, 4 Aug 2022 16:50:22 +0200
Subject: [PATCH] Bugfix for email address handling in ticket system

---
 www/lib/class.erpapi.php | 15 ++++++++++++---
 www/pages/ticket.php     | 20 ++++++++++++++------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/www/lib/class.erpapi.php b/www/lib/class.erpapi.php
index 7e4c28d2..fca8ca7b 100644
--- a/www/lib/class.erpapi.php
+++ b/www/lib/class.erpapi.php
@@ -32458,6 +32458,7 @@ function ChargenMHDAuslagern($artikel, $menge, $lagerplatztyp, $lpid,$typ,$wert,
 
 /*
 * Return 0 = not ok, return 1 = ok
+* $to, $to_name, $cc, $bcc can be csv or arrays
 */
 function MailSendFinal($from,$from_name,$to,$to_name,$betreff,$text,$files="",$projekt="",$signature=true,$cc="",$bcc="", $system = false)
 {
@@ -32551,23 +32552,30 @@ function MailSendFinal($from,$from_name,$to,$to_name,$betreff,$text,$files="",$p
       
       $recipients = [];
 
+      $to_csv = "";
+      $to_name_csv = "";
+
       // Prepare names and addresses
       if (is_array($to)) {
         foreach ($to as $item)  {
           $to_array[] = $item;
+          $to_csv .= $item;
         }
       } else if (!empty($to)) {
         foreach (explode(',',str_replace(" ","",$to)) as $item)  {
           $to_array[] = $item;
+          $to_csv .= $item;
         }
       }
       if (is_array($to_name)) {
         foreach ($to_name as $item)  {
           $to_name_array[] = $item;
+          $to_name_csv .= $item;
         }
       } else if (!empty($to_name)) {
         foreach (explode(',',str_replace(" ","",$to_name)) as $item)  {
           $to_name_array[] = $item;
+          $to_name_csv .= $item;
         }
       }
 
@@ -32639,7 +32647,8 @@ function MailSendFinal($from,$from_name,$to,$to_name,$betreff,$text,$files="",$p
     }    
 
     if($sysMailerSent === false) {
-      $this->MailLogFile($from,$from_name,$to,$to_name,$betreff,$text,$files,$projekt,$signature,$cc,$bcc,$system);
+
+      $this->MailLogFile($from,$from_name,$to_csv,$to_name_csv,$betreff,$text,$files,$projekt,$signature,$cc,$bcc,$system);
       $this->mail_error =  "Mailer Error: " . $sendmail_error;
 
       if(isset($this->app->User) && $this->app->User && method_exists($this->app->User, 'GetID'))
@@ -32649,14 +32658,14 @@ function MailSendFinal($from,$from_name,$to,$to_name,$betreff,$text,$files="",$p
       return 0;
     } else {
 
-      $this->MailLogFile($from,$from_name,$to,$to_name,$betreff,$text,$files,$projekt,$signature,$cc,$bcc,$system);
+      $this->MailLogFile($from,$from_name,$to_csv,$to_name_csv,$betreff,$text,$files,$projekt,$signature,$cc,$bcc,$system);
 
       // Put the mail in IMAP sent folder
       // Note that this is implemented with laminas-mail and only this
       // The created mail may differ from the sent one because it is created by different libraries 
 
       $imap_aktiv = $this->app->DB->Select("SELECT imap_sentfolder_aktiv FROM emailbackup WHERE email='".$from."' AND imap_sentfolder!='' AND geloescht!=1 LIMIT 1");
-      if($imap_aktiv=="1" && !preg_match("/Xentral Kopie/",$to_name) && !preg_match("/WaWision Kopie/",$to_name))
+      if($imap_aktiv=="1" && !preg_match("/Xentral Kopie/",$to_name_csv) && !preg_match("/WaWision Kopie/",$to_name_csv))
       {
 
         // This will build the mail as EmailMessage (Xentral\Components\Mailer\Data) and then rebuild it with laminas-mail message to produce the raw output
diff --git a/www/pages/ticket.php b/www/pages/ticket.php
index 5288b255..51e7fdd2 100644
--- a/www/pages/ticket.php
+++ b/www/pages/ticket.php
@@ -279,10 +279,12 @@ class Ticket {
                         continue;
                     }
                     $this->app->Tpl->Set("NACHRICHT_BETREFF",$message['betreff']." (Entwurf)");
+                } else {
+                  $this->app->Tpl->Set("NACHRICHT_BETREFF",$message['betreff']);
                 }
-                $this->app->Tpl->Set("NACHRICHT_SENDER",$message['mail_replyto']." (".$message['verfasser'].")");
-                $this->app->Tpl->Set("NACHRICHT_RECIPIENTS",$message['mail']);
-                $this->app->Tpl->Set("NACHRICHT_CC_RECIPIENTS",$message['mail_cc']);  
+                $this->app->Tpl->Set("NACHRICHT_SENDER",htmlentities($message['verfasser']." <".$message['mail_replyto'].">"));
+                $this->app->Tpl->Set("NACHRICHT_RECIPIENTS",htmlentities($message['mail']));
+                $this->app->Tpl->Set("NACHRICHT_CC_RECIPIENTS",htmlentities($message['mail_cc']));  
                 $this->app->Tpl->Set("NACHRICHT_FLOAT","right");
                 $this->app->Tpl->Set("NACHRICHT_ZEIT",$message['zeitausgang']);            
                 $this->app->Tpl->Set("NACHRICHT_NAME",$message['verfasser']);
@@ -630,8 +632,14 @@ class Ticket {
             // Attachments
             $files = $this->app->erp->GetDateiSubjektObjektDateiname('Anhang','Ticket',$drafted_messages[0]['id'],"");
 
+            $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
+            preg_match_all($pattern, $drafted_messages[0]['mail'], $matches);
+            $to = $matches[0];
+
             if ($drafted_messages[0]['mail_cc'] != '') {
-              $cc = explode(',',$drafted_messages[0]['mail_cc']);            
+              $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
+              preg_match_all($pattern, $drafted_messages[0]['mail_cc'], $matches);
+              $cc = $matches[0];
             } else {
               $cc = null;
             }
@@ -640,8 +648,8 @@ class Ticket {
                 $this->app->erp->MailSend(
                   $drafted_messages[0]['mail_replyto'],
                   $drafted_messages[0]['verfasser_replyto'],
-                  $drafted_messages[0]['mail'],
-                  $drafted_messages[0]['mail'],
+                  $to,
+                  $to,
                   $drafted_messages[0]['betreff'],
                   $drafted_messages[0]['text'],
                   $files,