2017-07-23 02:39:54 +08:00
|
|
|
<?php
|
|
|
|
function relayhost($_action, $_data = null) {
|
|
|
|
global $pdo;
|
|
|
|
global $lang;
|
2018-08-04 02:31:33 +08:00
|
|
|
$_data_log = $_data;
|
2017-07-23 02:39:54 +08:00
|
|
|
switch ($_action) {
|
|
|
|
case 'add':
|
|
|
|
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'access_denied'
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$hostname = trim($_data['hostname']);
|
|
|
|
$username = str_replace(':', '\:', trim($_data['username']));
|
|
|
|
$password = str_replace(':', '\:', trim($_data['password']));
|
|
|
|
if (empty($hostname)) {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('invalid_host', htmlspecialchars($host))
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO `relayhosts` (`hostname`, `username` ,`password`, `active`)
|
|
|
|
VALUES (:hostname, :username, :password, :active)");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':hostname' => $hostname,
|
|
|
|
':username' => $username,
|
2017-10-10 02:31:08 +08:00
|
|
|
':password' => str_replace(':', '\:', $password),
|
2017-07-23 02:39:54 +08:00
|
|
|
':active' => '1'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('mysql_error', $e)
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'success',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('relayhost_added', htmlspecialchars(implode(', ', $hosts)))
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'edit':
|
|
|
|
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'access_denied'
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$ids = (array)$_data['id'];
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$is_now = relayhost('details', $id);
|
|
|
|
if (!empty($is_now)) {
|
|
|
|
$hostname = (!empty($_data['hostname'])) ? trim($_data['hostname']) : $is_now['hostname'];
|
2018-01-01 16:17:49 +08:00
|
|
|
$username = (isset($_data['username'])) ? trim($_data['username']) : $is_now['username'];
|
|
|
|
$password = (isset($_data['password'])) ? trim($_data['password']) : $is_now['password'];
|
2017-07-23 02:39:54 +08:00
|
|
|
$active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'relayhost_invalid'
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$stmt = $pdo->prepare("UPDATE `relayhosts` SET
|
|
|
|
`hostname` = :hostname,
|
|
|
|
`username` = :username,
|
|
|
|
`password` = :password,
|
|
|
|
`active` = :active
|
|
|
|
WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':id' => $id,
|
|
|
|
':hostname' => $hostname,
|
|
|
|
':username' => $username,
|
|
|
|
':password' => $password,
|
|
|
|
':active' => $active
|
|
|
|
));
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('mysql_error', $e)
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'success',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('object_modified', htmlspecialchars(implode(', ', $hostnames)))
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'access_denied'
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$ids = (array)$_data['id'];
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
try {
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM `relayhosts` WHERE `id`= :id");
|
|
|
|
$stmt->execute(array(':id' => $id));
|
|
|
|
$stmt = $pdo->prepare("UPDATE `domain` SET `relayhost` = '0' WHERE `relayhost`= :id");
|
|
|
|
$stmt->execute(array(':id' => $id));
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('mysql_error', $e)
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'success',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('relayhost_removed', htmlspecialchars(implode(', ', $hostnames)))
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'get':
|
|
|
|
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$relayhosts = array();
|
|
|
|
try {
|
|
|
|
$stmt = $pdo->query("SELECT `id`, `hostname`, `username` FROM `relayhosts`");
|
|
|
|
$relayhosts = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
catch(PDOException $e) {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('mysql_error', $e)
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $relayhosts;
|
|
|
|
break;
|
|
|
|
case 'details':
|
|
|
|
if ($_SESSION['mailcow_cc_role'] != "admin" || !isset($_data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$relayhostdata = array();
|
|
|
|
try {
|
|
|
|
$stmt = $pdo->prepare("SELECT `id`,
|
|
|
|
`hostname`,
|
|
|
|
`username`,
|
|
|
|
`password`,
|
|
|
|
`active` AS `active_int`,
|
|
|
|
CONCAT(LEFT(`password`, 3), '...') AS `password_short`,
|
|
|
|
CASE `active` WHEN 1 THEN '".$lang['mailbox']['yes']."' ELSE '".$lang['mailbox']['no']."' END AS `active`
|
|
|
|
FROM `relayhosts`
|
|
|
|
WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(':id' => $_data));
|
|
|
|
$relayhostdata = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
if (!empty($relayhostdata)) {
|
|
|
|
$stmt = $pdo->prepare("SELECT GROUP_CONCAT(`domain` SEPARATOR ', ') AS `used_by_domains` FROM `domain` WHERE `relayhost` = :id");
|
|
|
|
$stmt->execute(array(':id' => $_data));
|
|
|
|
$used_by_domains = $stmt->fetch(PDO::FETCH_ASSOC)['used_by_domains'];
|
|
|
|
$used_by_domains = (empty($used_by_domains)) ? '' : $used_by_domains;
|
|
|
|
$relayhostdata['used_by_domains'] = $used_by_domains;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(PDOException $e) {
|
|
|
|
$_SESSION['return'] = array(
|
|
|
|
'type' => 'danger',
|
2018-08-04 02:31:33 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('mysql_error', $e)
|
2017-07-23 02:39:54 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $relayhostdata;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|