Clear non printable characters from mail addresses
authorMichael Spahn <m.spahn@metaways.de>
Wed, 22 Mar 2017 09:31:27 +0000 (10:31 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 23 Mar 2017 10:51:34 +0000 (11:51 +0100)
see 0012766: trim invalid mails address format
https://forge.tine20.org/view.php?id=12766

Change-Id: Ib39da1e20445dc548afcad3cc6f3a943a03565ac
Reviewed-on: http://gerrit.tine20.com/customers/4398
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Felamimail/Frontend/JsonTest.php
tests/tine20/GetProtectedMethodTrait.php [new file with mode: 0644]
tine20/Felamimail/Model/Message.php

index c3f95f8..46030ee 100644 (file)
@@ -21,6 +21,8 @@ require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHe
  */
 class Felamimail_Frontend_JsonTest extends TestCase
 {
+    use GetProtectedMethodTrait;
+
     /**
      * @var Felamimail_Frontend_Json
      */
@@ -563,6 +565,20 @@ class Felamimail_Frontend_JsonTest extends TestCase
     }
 
     /**
+     * test mail sanitize
+     */
+    public function testSanitizeMail()
+    {
+        $expected = 'info@testest.de';
+        $obfuscatedMail = '  info@testest.de

';
+
+        $reflectionMethod = $this->getProtectedMethod(Felamimail_Model_Message::class, 'sanitizeMailAddress');
+        $result = $reflectionMethod->invokeArgs(new Felamimail_Model_Message(), [$obfuscatedMail]);
+
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * check email note
      *
      * @param Addressbook_Model_Contact $contact
diff --git a/tests/tine20/GetProtectedMethodTrait.php b/tests/tine20/GetProtectedMethodTrait.php
new file mode 100644 (file)
index 0000000..daf0daf
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Provides method to test protected methods
+ *
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author      Michael Spahn <m.spahn@metaways.de>
+ * @copyright   Copyright (c) 2017 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+trait GetProtectedMethodTrait
+{
+    /**
+     * GetProtectedMethod constructor.
+     * @param $object
+     * @param $method
+     * @return ReflectionMethod
+     */
+    public function getProtectedMethod($object, $method)
+    {
+        $class = new ReflectionClass($object);
+        $method = $class->getMethod($method);
+        $method->setAccessible(true);
+
+        return $method;
+    }
+}
\ No newline at end of file
index a989bc6..1e3ffcd 100644 (file)
@@ -672,6 +672,7 @@ class Felamimail_Model_Message extends Tinebase_Record_Abstract
         // explode email addresses if multiple
         $recipientType = array('to', 'cc', 'bcc');
         $delimiter = ';';
+
         foreach ($recipientType as $field) {
             if (!empty($recordData[$field])) {
                 $recipients = array();
@@ -683,19 +684,20 @@ class Felamimail_Model_Message extends Tinebase_Record_Abstract
                         $recipients = array_merge($recipients, explode($delimiter, $addresses));
                     } else {
                         // single recipient
-                        $recipients[] = \trim($addresses);
+                        $recipients[] =  $this->sanitizeMailAddress($addresses);
                     }
                 }
                 
                 foreach ($recipients as $key => &$recipient) {
                     // extract email address if name and address given
                     if (preg_match('/(.*)<(.*)>/', $recipient, $matches) > 0) {
-                        $recipient = \trim($matches[2]);
+                        $recipient = $this->sanitizeMailAddress($matches[2]);
                     }
                     if (empty($recipient)) {
                         unset($recipients[$key]);
                     }
                 }
+                unset($recipient);
 
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($recipients, true));
                 
@@ -703,7 +705,28 @@ class Felamimail_Model_Message extends Tinebase_Record_Abstract
             }
         }
     }
-    
+
+    /**
+     * Mails often copied from word and so on and contain problematic characters, non printable characters and whitespaces.
+     *
+     * @param $mail
+     * @return string
+     * @throws \InvalidArgumentException
+     */
+    protected function sanitizeMailAddress($mail)
+    {
+        // Ensure encoding
+        $mail = Tinebase_Helper::mbConvertTo($mail);
+
+        // Remove non printable
+        $mail = \preg_replace('/[[:^print:]]/u', '', $mail);
+
+        // Remove whitespaces
+        $mail = \trim($mail);
+
+        return $mail;
+    }
+
     /**
      * get body as plain text
      *