Browse Source

[Web] Fixes some issues with recipient maps (address rewriting)

André 7 years ago
parent
commit
48829d8393

+ 14 - 4
data/web/edit.php

@@ -673,17 +673,27 @@ if (isset($_SESSION['mailcow_cc_role'])) {
     elseif (isset($_GET['recipient_map']) && !empty($_GET["recipient_map"])) {
         $map = intval($_GET["recipient_map"]);
         $result = recipient_map('details', $map);
+        if (substr($result['recipient_map_old'], 0, 1) == '@') {
+          $result['recipient_map_old'] = substr($result['recipient_map_old'], 1);
+        }
         if (!empty($result)) {
           ?>
-          <h4>Recipient map: <?=$result['recipient_map_old'];?></h4>
+          <h4><?=$lang['mailbox']['recipient_map']?>: <?=$result['recipient_map_old'];?></h4>
           <br />
           <form class="form-horizontal" data-id="edit_recipient_map" role="form" method="post">
             <input type="hidden" value="0" name="active">
             <div class="form-group">
-              <label class="control-label col-sm-2" for="recipient_map_new">New destination</label>
+              <label class="control-label col-sm-2" for="recipient_map_new"><?=$lang['mailbox']['recipient_map_old'];?></label>
+              <div class="col-sm-10">
+                <input value="<?=$result['recipient_map_old'];?>" type="text" class="form-control" name="recipient_map_old" id="recipient_map_old">
+                <small><?=$lang['mailbox']['recipient_map_old_info'];?></small>
+              </div>
+            </div>
+            <div class="form-group">
+              <label class="control-label col-sm-2" for="recipient_map_new"><?=$lang['mailbox']['recipient_map_new'];?></label>
               <div class="col-sm-10">
-                <textarea id="recipient_map_new" class="form-control" autocapitalize="none" autocorrect="off" rows="10" id="recipient_map_new" name="recipient_map_new" required><?=$result['recipient_map_new'];?></textarea>
-                <small>Recipient map destinations can only be valid email addresses. Separated by whitespace, semicolon, new line or comma.</small>
+                <input value="<?=$result['recipient_map_new'];?>" type="text" class="form-control" name="recipient_map_new" id="recipient_map_new">
+                <small><?=$lang['mailbox']['recipient_map_new_info'];?></small>
               </div>
             </div>
             <div class="form-group">

+ 46 - 63
data/web/inc/functions.address_rewriting.inc.php

@@ -290,15 +290,11 @@ function recipient_map($_action, $_data = null, $attr = null) {
   switch ($_action) {
     case 'add':
       $old_dest = strtolower(trim($_data['recipient_map_old']));
-      $new_dest = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['recipient_map_new']));
-      $active = intval($_data['active']);
-      if (empty($new_dest)) {
-        $_SESSION['return'] = array(
-          'type' => 'danger',
-          'msg' => 'Recipient map destination cannot be empty'
-        );
-        return false;
+      if (substr($old_dest, 0, 1) == '@') {
+        $old_dest = substr($old_dest, 1);
       }
+      $new_dest = strtolower(trim($_data['recipient_map_new']));
+      $active = intval($_data['active']);
       if (is_valid_domain_name($old_dest)) {
         $old_dest_sane = '@' . idn_to_ascii($old_dest);
       }
@@ -308,42 +304,25 @@ function recipient_map($_action, $_data = null, $attr = null) {
       else {
         $_SESSION['return'] = array(
           'type' => 'danger',
-          'msg' => 'Invalid original recipient specified: ' . $old_dest
+          'msg' => sprintf($lang['danger']['invalid_recipient_map_old'], htmlspecialchars($old_dest))
         );
         return false;
       }
-      foreach ($new_dest as &$new_dest_e) {
-        if (!filter_var($new_dest_e, FILTER_VALIDATE_EMAIL)) {
-          $new_dest_e = null;
-        }
-        $new_dest_e = strtolower($new_dest_e);
-      }
-      $new_dest = array_filter($new_dest);
-      $new_dest = implode(",", $new_dest);
-      if (empty($new_dest)) {
+      if (!filter_var($new_dest, FILTER_VALIDATE_EMAIL)) {
         $_SESSION['return'] = array(
           'type' => 'danger',
-          'msg' => 'Recipient map destination cannot be empty'
+          'msg' => sprintf($lang['danger']['invalid_recipient_map_new'], htmlspecialchars($new_dest))
         );
         return false;
       }
-      try {
-        $stmt = $pdo->prepare("SELECT `id` FROM `recipient_maps`
-          WHERE `old_dest` = :old_dest");
-        $stmt->execute(array(':old_dest' => $old_dest_sane));
-        $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
-      }
-      catch(PDOException $e) {
-        $_SESSION['return'] = array(
-          'type' => 'danger',
-          'msg' => 'MySQL: '.$e
-        );
-        return false;
+      $rmaps = recipient_map('get');
+      foreach ($rmaps as $rmap) {
+        $old_dests_existing[] = recipient_map('details', $rmap)['recipient_map_old'];
       }
-      if ($num_results != 0) {
+      if (in_array($old_dest_sane, $old_dests_existing)) {
         $_SESSION['return'] = array(
           'type' => 'danger',
-          'msg' => 'A Recipient map entry "' . htmlspecialchars($old_dest_sane) . '" exists'
+          'msg' => sprintf($lang['danger']['recipient_map_entry_exists'], htmlspecialchars($old_dest))
         );
         return false;
       }
@@ -365,7 +344,7 @@ function recipient_map($_action, $_data = null, $attr = null) {
       }
       $_SESSION['return'] = array(
         'type' => 'success',
-        'msg' => 'Recipient map entry saved'
+        'msg' => sprintf($lang['success']['recipient_map_entry_saved'], htmlspecialchars($old_dest_sane))
       );
     break;
     case 'edit':
@@ -375,7 +354,10 @@ function recipient_map($_action, $_data = null, $attr = null) {
         if (!empty($is_now)) {
           $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
           $new_dest = (!empty($_data['recipient_map_new'])) ? $_data['recipient_map_new'] : $is_now['recipient_map_new'];
-          $old_dest = $is_now['old_dest'];
+          $old_dest = (!empty($_data['recipient_map_old'])) ? $_data['recipient_map_old'] : $is_now['recipient_map_old'];
+          if (substr($old_dest, 0, 1) == '@') {
+            $old_dest = substr($old_dest, 1);
+          }
         }
         else {
           $_SESSION['return'] = array(
@@ -384,46 +366,47 @@ function recipient_map($_action, $_data = null, $attr = null) {
           );
           return false;
         }
-        $new_dest = array_map('trim', preg_split( "/( |,|;|\n)/", $new_dest));
-        $active = intval($_data['active']);
-        foreach ($new_dest as &$new_dest_e) {
-          if (!filter_var($new_dest_e, FILTER_VALIDATE_EMAIL)) {
-            $new_dest_e = null;;
-          }
-          $new_dest_e = strtolower($new_dest_e);
-        }
-        $new_dest = array_filter($new_dest);
-        $new_dest = implode(",", $new_dest);
-        if (empty($new_dest)) {
-          $_SESSION['return'] = array(
-            'type' => 'danger',
-            'msg' => 'Recipient map destination cannot be empty'
-          );
-          return false;
+        if (is_valid_domain_name($old_dest)) {
+          $old_dest_sane = '@' . idn_to_ascii($old_dest);
         }
-        try {
-          $stmt = $pdo->prepare("SELECT `id` FROM `recipient_maps`
-            WHERE `old_dest` = :old_dest");
-          $stmt->execute(array(':old_dest' => $old_dest));
-          $id_now = $stmt->fetch(PDO::FETCH_ASSOC)['id'];
+        elseif (filter_var($old_dest, FILTER_VALIDATE_EMAIL)) {
+          $old_dest_sane = $old_dest;
         }
-        catch(PDOException $e) {
+        else {
           $_SESSION['return'] = array(
             'type' => 'danger',
-            'msg' => 'MySQL: '.$e
+            'msg' => sprintf($lang['danger']['invalid_recipient_map_old'], htmlspecialchars($old_dest))
           );
           return false;
         }
-        if (isset($id_now) && $id_now != $id) {
+        $active = intval($_data['active']);
+        if (!filter_var($new_dest, FILTER_VALIDATE_EMAIL)) {
           $_SESSION['return'] = array(
             'type' => 'danger',
-            'msg' => 'A Recipient map entry ' . htmlspecialchars($old_dest) . ' exists'
+            'msg' => sprintf($lang['danger']['invalid_recipient_map_new'], htmlspecialchars($new_dest))
           );
           return false;
         }
+        $rmaps = recipient_map('get');
+        foreach ($rmaps as $rmap) {
+          $old_dests_existing[] = recipient_map('details', $rmap)['recipient_map_old'];
+        }
+        if (in_array($old_dest_sane, $old_dests_existing) &&
+          recipient_map('details', $id)['recipient_map_old'] != $old_dest_sane) {
+            $_SESSION['return'] = array(
+              'type' => 'danger',
+              'msg' => sprintf($lang['danger']['recipient_map_entry_exists'], htmlspecialchars($old_dest_sane))
+            );
+            return false;
+        }
         try {
-          $stmt = $pdo->prepare("UPDATE `recipient_maps` SET `new_dest` = :new_dest, `active` = :active WHERE `id`= :id");
+          $stmt = $pdo->prepare("UPDATE `recipient_maps` SET
+            `old_dest` = :old_dest,
+            `new_dest` = :new_dest,
+            `active` = :active
+              WHERE `id`= :id");
           $stmt->execute(array(
+            ':old_dest' => $old_dest_sane,
             ':new_dest' => $new_dest,
             ':active' => $active,
             ':id' => $id
@@ -439,7 +422,7 @@ function recipient_map($_action, $_data = null, $attr = null) {
       }
       $_SESSION['return'] = array(
         'type' => 'success',
-        'msg' => 'Recipient map entry edited'
+        'msg' => sprintf($lang['success']['recipient_map_entry_saved'], htmlspecialchars($old_dest))
       );
     break;
     case 'details':
@@ -507,7 +490,7 @@ function recipient_map($_action, $_data = null, $attr = null) {
       }
       $_SESSION['return'] = array(
         'type' => 'success',
-        'msg' => 'Deleted Recipient map id/s ' . implode(', ', $ids)
+        'msg' => sprintf($lang['success']['recipient_map_entry_deleted'], htmlspecialchars($old_dest))
       );
       return true;
     break;

+ 10 - 1
data/web/lang/lang.de.php

@@ -553,9 +553,18 @@ $lang['mailbox']['add_bcc_entry'] = "BCC-Eintrag hinzufügen";
 $lang['mailbox']['bcc_info'] = "Eine empfängerabhängige Map wird verwendet, wenn die BCC-Map Eintragung auf den Eingang einer E-Mail auf das lokale Ziel reagieren soll. Senderabhängige Maps verfahren nach dem gleichen Prinzip.<br/>
   Das lokale Ziel wird bei Fehlzustellungen an ein BCC-Ziel nicht informiert.";
 $lang['mailbox']['address_rewriting'] = 'Adressumschreibung';
+
 $lang['mailbox']['recipient_maps'] = 'Empfängerumschreibungen';
+$lang['mailbox']['recipient_map'] = 'Empfängerumschreibung';
 $lang['mailbox']['recipient_map_info'] = 'Empfängerumschreibung ersetzen den Empfänger einer E-Mail vor dem Versand.';
+$lang['mailbox']['recipient_map_old_info'] = 'Der originale Empfänger muss eine E-Mail-Adresse oder ein Domainname sein.';
+$lang['mailbox']['recipient_map_new_info'] = 'Der neue Empfänger muss eine E-Mail-Adresse sein.';
 $lang['mailbox']['recipient_map_old'] = 'Original Empfänger';
 $lang['mailbox']['recipient_map_new'] = 'Neuer Empfänger';
 $lang['mailbox']['add_recipient_map_entry'] = 'Empfängerumschreibung hinzufügen';
-$lang['mailbox']['add_sender_map_entry'] = 'Senderumschreibung hinzufügen';
+$lang['danger']['invalid_recipient_map_new'] = 'Neuer Empfänger %s ist ungültig';
+$lang['danger']['invalid_recipient_map_old'] = 'Originaler Empfänger %s ist ungültig';
+$lang['danger']['recipient_map_entry_exists'] = 'Eine Empfängerumschreibung für %s existiert bereits';
+$lang['success']['recipient_map_entry_saved'] = 'Empfängerumschreibung für Objekt %s wurde gespeichert';
+$lang['success']['recipient_map_entry_deleted'] = 'Empfängerumschreibung für Objekt %s wurde gelöscht';
+

+ 8 - 1
data/web/lang/lang.en.php

@@ -560,11 +560,18 @@ $lang['mailbox']['bcc_info'] = "BCC maps are used to silently forward copies of
   The local destination will not be informed about a failed delivery.";
 $lang['mailbox']['address_rewriting'] = 'Address rewriting';
 $lang['mailbox']['recipient_maps'] = 'Recipient maps';
+$lang['mailbox']['recipient_map'] = 'Recipient map';
 $lang['mailbox']['recipient_map_info'] = 'Recipient maps are used to replace the destination address on a message before it is delivered.';
+$lang['mailbox']['recipient_map_old_info'] = 'A recipient maps original destination must be valid email addresses or a domain name.';
+$lang['mailbox']['recipient_map_new_info'] = 'Recipient map destination must be a valid email address.';
 $lang['mailbox']['recipient_map_old'] = 'Original recipient';
 $lang['mailbox']['recipient_map_new'] = 'New recipient';
+$lang['danger']['invalid_recipient_map_new'] = 'Invalid new recipient specified: %s';
+$lang['danger']['invalid_recipient_map_old'] = 'Invalid original recipient specified: %s';
+$lang['danger']['recipient_map_entry_exists'] = 'A Recipient map entry for %s exists';
+$lang['success']['recipient_map_entry_saved'] = 'Recipient map entry for %s has been saved';
+$lang['success']['recipient_map_entry_deleted'] = 'Recipient map entry for %s has been deleted';
 $lang['mailbox']['add_recipient_map_entry'] = 'Add recipient map';
-$lang['mailbox']['add_sender_map_entry'] = 'Add sender map';
 
 $lang['oauth2']['scope_ask_permission'] = 'An application asked for the following permissions';
 $lang['oauth2']['profile'] = 'Profile';

+ 12 - 10
data/web/modals/mailbox.php

@@ -614,17 +614,19 @@ if (!isset($_SESSION['mailcow_cc_role'])) {
       <div class="modal-body">
 				<form class="form-horizontal" data-cached-form="true" role="form" data-id="add_recipient_map">
           <div class="form-group">
-            <label class="control-label col-sm-2" for="recipient_map_old"><?=$lang['mailbox']['recipient_map_old'];?>:</label>
-						<div class="col-sm-10">
-							<textarea autocorrect="off" spellcheck="false" autocapitalize="none" class="form-control" rows="2" id="recipient_map_old" name="recipient_map_old" required></textarea>
-						</div>
+            <label class="control-label col-sm-2" for="recipient_map_old"><?=$lang['mailbox']['recipient_map_old'];?></label>
+            <div class="col-sm-10">
+            <input type="text" class="form-control" name="recipient_map_old" id="recipient_map_old">
+            <small><?=$lang['mailbox']['recipient_map_old_info'];?></small>
+            </div>
+          </div>
+          <div class="form-group">
+            <label class="control-label col-sm-2" for="recipient_map_new"><?=$lang['mailbox']['recipient_map_new'];?></label>
+            <div class="col-sm-10">
+            <input type="text" class="form-control" name="recipient_map_new" id="recipient_map_new">
+            <small><?=$lang['mailbox']['recipient_map_new_info'];?></small>
+            </div>
           </div>
-					<div class="form-group">
-						<label class="control-label col-sm-2" for="recipient_map_new"><?=$lang['mailbox']['recipient_map_new'];?>:</label>
-						<div class="col-sm-10">
-							<textarea autocorrect="off" spellcheck="false" autocapitalize="none" class="form-control" rows="2" id="recipient_map_new" name="recipient_map_new" required></textarea>
-						</div>
-					</div>
 					<div class="form-group">
 						<div class="col-sm-offset-2 col-sm-10">
 							<div class="checkbox">