From c23dc24120cd9c58086407a524740fea85c746c9 Mon Sep 17 00:00:00 2001
From: Adam Bramley <adam@catalyst.net.nz>
Date: Wed, 30 Mar 2011 11:16:18 +1300
Subject: [PATCH] Issue #488032: Fixed bug on edge case with message body as array, added validate function to email list.

---
 reroute_email.module |   36 ++++++++++++++++++++++++++----------
 1 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/reroute_email.module b/reroute_email.module
index 3eb5652..c42c812 100644
--- a/reroute_email.module
+++ b/reroute_email.module
@@ -34,17 +34,33 @@ function reroute_email_settings() {
     '#rows'          => 5,
     '#description'   => t('Provide a list of email addresses to pass through. Any email addresses not on this list will be rerouted to the first address on the list.')
   );
-  
+
   $form[REROUTE_EMAIL_ENABLE] = array(
     '#type'          => 'checkbox',
     '#title'         => t('Enable routing'),
     '#default_value' => variable_get(REROUTE_EMAIL_ENABLE, 1),
     '#description'   => t('Check this box if you want to enable email rerouting. Uncheck to disable rerouting.'),
-  );  
+  );
 
   return system_settings_form($form);
 }
 
+/**
+ * Implementation of hook_validate().
+ *
+ * @author Adam Bramley <adam@catalyst.net.nz>
+ */
+function reroute_email_settings_validate($form, $form_state) {
+  if ($form_state['values']['reroute_email_enable'] == 1) {
+    $addresslist = preg_split('/\s/', $form_state['values']['reroute_email_address'], -1, PREG_SPLIT_NO_EMPTY);
+    foreach ($addresslist as $address) {
+      if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $address)) {
+        form_set_error('reroute_email_address', 'All email addresses must be valid');
+      }
+    }
+  }
+}
+
 function reroute_email_mail_alter(&$message) {
   if (variable_get(REROUTE_EMAIL_ENABLE, 1)) {
     global $base_url;
@@ -57,7 +73,7 @@ function reroute_email_mail_alter(&$message) {
     if (empty($message)) {
       return;
     }
-  
+
     if (!is_array($message)) {
       return;
     }
@@ -74,7 +90,7 @@ function reroute_email_mail_alter(&$message) {
         unset($message['headers']['Cc']);
       }
     }
-  
+
     // Split the address string on whitespace, ignoring any empty results
     $addresslist = preg_split('/\s/', variable_get(REROUTE_EMAIL_ADDRESS, ini_get('sendmail_from')), -1, PREG_SPLIT_NO_EMPTY);
 
@@ -86,15 +102,15 @@ function reroute_email_mail_alter(&$message) {
       // Not on the list, so reroute to the first address in the list
       $message['to'] = $addresslist[0];
       // Format a message to show at the top
-      $msg[] = t("This email was rerouted.");
-      $msg[] = t("Web site: @site", array('@site' => $base_url));
-      $msg[] = t("Mail key: @key", array('@key' => $mailkey));
-      $msg[] = t("Originally to: @to", array('@to' => $to));
-      $msg[] = "-----------------------";
+      $msg = t("This email was rerouted.\n");
+      $msg .= t("Web site: @site\n", array('@site' => $base_url));
+      $msg .= t("Mail key: @key\n", array('@key' => $mailkey));
+      $msg .= t("Originally to: @to\n", array('@to' => $to));
+      $msg .= "-----------------------\n";
 
       // Prepend to the body of the email
       // -- I really think we should simply this a bit -- Khalid
-      $message['body'] = array_merge($msg, isset($message['body']) ? (is_array($message['body']) ? $message['body'] : array($message['body'])) : array());
+      $message['body'] = $msg . (is_array($message['body']) ? implode('\n', $message['body']) : $message['body']);
     }
   }
 }
-- 
1.7.0.4

