Expose Postfix's recipient_canonical_maps through web UI
This commit is contained in:
@@ -289,4 +289,237 @@ function bcc($_action, $_data = null, $attr = null) {
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function recipient_map($_action, $_data = null, $attr = null) {
|
||||
global $pdo;
|
||||
global $lang;
|
||||
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
||||
return false;
|
||||
}
|
||||
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 (is_valid_domain_name($old_dest)) {
|
||||
$old_dest_sane = '@' . idn_to_ascii($old_dest);
|
||||
}
|
||||
elseif (filter_var($old_dest, FILTER_VALIDATE_EMAIL)) {
|
||||
$old_dest_sane = $old_dest;
|
||||
}
|
||||
else {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'Invalid original recipient specified: ' . $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)) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'Recipient map destination cannot be empty'
|
||||
);
|
||||
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;
|
||||
}
|
||||
if ($num_results != 0) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'A Recipient map entry "' . htmlspecialchars($old_dest_sane) . '" exists'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$stmt = $pdo->prepare("INSERT INTO `recipient_maps` (`old_dest`, `new_dest`, `active`) VALUES
|
||||
(:old_dest, :new_dest, :active)");
|
||||
$stmt->execute(array(
|
||||
':old_dest' => $old_dest_sane,
|
||||
':new_dest' => $new_dest,
|
||||
':active' => $active
|
||||
));
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'success',
|
||||
'msg' => 'Recipient map entry saved'
|
||||
);
|
||||
break;
|
||||
case 'edit':
|
||||
$ids = (array)$_data['id'];
|
||||
foreach ($ids as $id) {
|
||||
$is_now = recipient_map('details', $id);
|
||||
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'];
|
||||
}
|
||||
else {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => sprintf($lang['danger']['access_denied'])
|
||||
);
|
||||
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;
|
||||
}
|
||||
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'];
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if (isset($id_now) && $id_now != $id) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'A Recipient map entry ' . htmlspecialchars($old_dest) . ' exists'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$stmt = $pdo->prepare("UPDATE `recipient_maps` SET `new_dest` = :new_dest, `active` = :active WHERE `id`= :id");
|
||||
$stmt->execute(array(
|
||||
':new_dest' => $new_dest,
|
||||
':active' => $active,
|
||||
':id' => $id
|
||||
));
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'success',
|
||||
'msg' => 'Recipient map entry edited'
|
||||
);
|
||||
break;
|
||||
case 'details':
|
||||
$mapdata = array();
|
||||
$id = intval($_data);
|
||||
try {
|
||||
$stmt = $pdo->prepare("SELECT `id`,
|
||||
`old_dest` AS `recipient_map_old`,
|
||||
`new_dest` AS `recipient_map_new`,
|
||||
`active` AS `active_int`,
|
||||
CASE `active` WHEN 1 THEN '".$lang['mailbox']['yes']."' ELSE '".$lang['mailbox']['no']."' END AS `active`,
|
||||
`created`,
|
||||
`modified` FROM `recipient_maps`
|
||||
WHERE `id` = :id");
|
||||
$stmt->execute(array(':id' => $id));
|
||||
$mapdata = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return $mapdata;
|
||||
break;
|
||||
case 'get':
|
||||
$mapdata = array();
|
||||
$all_items = array();
|
||||
$id = intval($_data);
|
||||
try {
|
||||
$stmt = $pdo->query("SELECT `id` FROM `recipient_maps`");
|
||||
$all_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
foreach ($all_items as $i) {
|
||||
$mapdata[] = $i['id'];
|
||||
}
|
||||
$all_items = null;
|
||||
return $mapdata;
|
||||
break;
|
||||
case 'delete':
|
||||
$ids = (array)$_data['id'];
|
||||
foreach ($ids as $id) {
|
||||
if (!is_numeric($id)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$stmt = $pdo->prepare("DELETE FROM `recipient_maps` WHERE `id`= :id");
|
||||
$stmt->execute(array(':id' => $id));
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'success',
|
||||
'msg' => 'Deleted Recipient map id/s ' . implode(', ', $ids)
|
||||
);
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
}
|
@@ -3,7 +3,7 @@ function init_db_schema() {
|
||||
try {
|
||||
global $pdo;
|
||||
|
||||
$db_version = "02012018_1515";
|
||||
$db_version = "20012021_2202";
|
||||
|
||||
$stmt = $pdo->query("SHOW TABLES LIKE 'versions'");
|
||||
$num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
|
||||
@@ -394,6 +394,25 @@ function init_db_schema() {
|
||||
),
|
||||
"attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
|
||||
),
|
||||
"recipient_maps" => array(
|
||||
"cols" => array(
|
||||
"id" => "INT NOT NULL AUTO_INCREMENT",
|
||||
"old_dest" => "VARCHAR(255) NOT NULL",
|
||||
"new_dest" => "VARCHAR(255) NOT NULL",
|
||||
"created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
|
||||
"modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
|
||||
"active" => "TINYINT(1) NOT NULL DEFAULT '0'"
|
||||
),
|
||||
"keys" => array(
|
||||
"primary" => array(
|
||||
"" => array("id")
|
||||
),
|
||||
"key" => array(
|
||||
"local_dest" => array("old_dest"),
|
||||
)
|
||||
),
|
||||
"attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
|
||||
),
|
||||
"tfa" => array(
|
||||
"cols" => array(
|
||||
"id" => "INT NOT NULL AUTO_INCREMENT",
|
||||
|
@@ -81,7 +81,7 @@ include $_SERVER['DOCUMENT_ROOT'] . '/lang/lang.'.$_SESSION['mailcow_locale'].'.
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailbox.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.customize.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.bcc.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.address_rewriting.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quarantaine.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php';
|
||||
|
Reference in New Issue
Block a user