2019-12-02 18:02:19 +08:00
|
|
|
<?php
|
|
|
|
function app_passwd($_action, $_data = null) {
|
|
|
|
global $pdo;
|
|
|
|
global $lang;
|
|
|
|
$_data_log = $_data;
|
2020-01-10 02:15:09 +08:00
|
|
|
!isset($_data_log['app_passwd']) ?: $_data_log['app_passwd'] = '*';
|
|
|
|
!isset($_data_log['app_passwd2']) ?: $_data_log['app_passwd2'] = '*';
|
2019-12-02 18:02:19 +08:00
|
|
|
if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'access_denied'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$username = $_data['username'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$username = $_SESSION['mailcow_cc_username'];
|
|
|
|
}
|
|
|
|
switch ($_action) {
|
|
|
|
case 'add':
|
2021-05-07 20:49:21 +08:00
|
|
|
$app_name = htmlspecialchars(trim($_data['app_name']));
|
|
|
|
$password = $_data['app_passwd'];
|
|
|
|
$password2 = $_data['app_passwd2'];
|
2019-12-02 18:02:19 +08:00
|
|
|
$active = intval($_data['active']);
|
2021-10-29 03:57:19 +08:00
|
|
|
$protocols = (array)$_data['protocols'];
|
|
|
|
$imap_access = (in_array('imap_access', $protocols)) ? 1 : 0;
|
|
|
|
$dav_access = (in_array('dav_access', $protocols)) ? 1 : 0;
|
|
|
|
$smtp_access = (in_array('smtp_access', $protocols)) ? 1 : 0;
|
|
|
|
$eas_access = (in_array('eas_access', $protocols)) ? 1 : 0;
|
2021-10-29 12:15:10 +08:00
|
|
|
$pop3_access = (in_array('pop3_access', $protocols)) ? 1 : 0;
|
|
|
|
$sieve_access = (in_array('sieve_access', $protocols)) ? 1 : 0;
|
2019-12-02 18:02:19 +08:00
|
|
|
$domain = mailbox('get', 'mailbox_details', $username)['domain'];
|
|
|
|
if (empty($domain)) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'access_denied'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-03 03:29:01 +08:00
|
|
|
if (!preg_match('/' . $GLOBALS['PASSWD_REGEP'] . '/', $password)) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'password_complexity'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($password != $password2) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'password_mismatch'
|
|
|
|
);
|
|
|
|
return false;
|
2019-12-02 18:02:19 +08:00
|
|
|
}
|
2019-12-03 03:29:01 +08:00
|
|
|
$password_hashed = hash_password($password);
|
|
|
|
if (empty($app_name)) {
|
2019-12-02 18:02:19 +08:00
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'app_name_empty'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-29 12:15:10 +08:00
|
|
|
$stmt = $pdo->prepare("INSERT INTO `app_passwd` (`name`, `mailbox`, `domain`, `password`, `imap_access`, `smtp_access`, `eas_access`, `dav_access`, `pop3_access`, `sieve_access`, `active`)
|
|
|
|
VALUES (:app_name, :mailbox, :domain, :password, :imap_access, :smtp_access, :eas_access, :dav_access, :pop3_access, :sieve_access, :active)");
|
2019-12-22 05:25:09 +08:00
|
|
|
$stmt->execute(array(
|
|
|
|
':app_name' => $app_name,
|
|
|
|
':mailbox' => $username,
|
|
|
|
':domain' => $domain,
|
|
|
|
':password' => $password_hashed,
|
2021-10-29 03:57:19 +08:00
|
|
|
':imap_access' => $imap_access,
|
|
|
|
':smtp_access' => $smtp_access,
|
|
|
|
':eas_access' => $eas_access,
|
|
|
|
':dav_access' => $dav_access,
|
2021-10-29 12:15:10 +08:00
|
|
|
':pop3_access' => $pop3_access,
|
|
|
|
':sieve_access' => $sieve_access,
|
2019-12-22 05:25:09 +08:00
|
|
|
':active' => $active
|
|
|
|
));
|
2019-12-02 18:02:19 +08:00
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'app_passwd_added'
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'edit':
|
|
|
|
$ids = (array)$_data['id'];
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$is_now = app_passwd('details', $id);
|
|
|
|
if (!empty($is_now)) {
|
2019-12-03 03:29:01 +08:00
|
|
|
$app_name = (!empty($_data['app_name'])) ? $_data['app_name'] : $is_now['name'];
|
2019-12-02 18:02:19 +08:00
|
|
|
$password = (!empty($_data['password'])) ? $_data['password'] : null;
|
|
|
|
$password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
|
2021-10-29 03:57:19 +08:00
|
|
|
if (isset($_data['protocols'])) {
|
|
|
|
$protocols = (array)$_data['protocols'];
|
|
|
|
$imap_access = (in_array('imap_access', $protocols)) ? 1 : 0;
|
|
|
|
$dav_access = (in_array('dav_access', $protocols)) ? 1 : 0;
|
|
|
|
$smtp_access = (in_array('smtp_access', $protocols)) ? 1 : 0;
|
|
|
|
$eas_access = (in_array('eas_access', $protocols)) ? 1 : 0;
|
2021-10-29 12:15:10 +08:00
|
|
|
$pop3_access = (in_array('pop3_access', $protocols)) ? 1 : 0;
|
|
|
|
$sieve_access = (in_array('sieve_access', $protocols)) ? 1 : 0;
|
2021-10-29 03:57:19 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$imap_access = $is_now['imap_access'];
|
|
|
|
$smtp_access = $is_now['smtp_access'];
|
|
|
|
$dav_access = $is_now['dav_access'];
|
|
|
|
$eas_access = $is_now['eas_access'];
|
2021-10-29 12:15:10 +08:00
|
|
|
$pop3_access = $is_now['pop3_access'];
|
|
|
|
$sieve_access = $is_now['sieve_access'];
|
2021-10-29 03:57:19 +08:00
|
|
|
}
|
2020-09-18 01:49:15 +08:00
|
|
|
$active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
|
2019-12-02 18:02:19 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
2019-12-03 03:29:01 +08:00
|
|
|
'msg' => array('app_passwd_id_invalid', $id)
|
2019-12-02 18:02:19 +08:00
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-07 20:49:21 +08:00
|
|
|
$app_name = htmlspecialchars(trim($app_name));
|
2019-12-02 18:02:19 +08:00
|
|
|
if (!empty($password) && !empty($password2)) {
|
|
|
|
if (!preg_match('/' . $GLOBALS['PASSWD_REGEP'] . '/', $password)) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
|
|
|
|
'msg' => 'password_complexity'
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($password != $password2) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
|
|
|
|
'msg' => 'password_mismatch'
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$password_hashed = hash_password($password);
|
|
|
|
$stmt = $pdo->prepare("UPDATE `app_passwd` SET
|
|
|
|
`password` = :password_hashed
|
|
|
|
WHERE `mailbox` = :username AND `id` = :id");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':password_hashed' => $password_hashed,
|
|
|
|
':username' => $username,
|
|
|
|
':id' => $id
|
|
|
|
));
|
|
|
|
}
|
2021-10-29 03:57:19 +08:00
|
|
|
|
2019-12-22 05:25:09 +08:00
|
|
|
$stmt = $pdo->prepare("UPDATE `app_passwd` SET
|
|
|
|
`name` = :app_name,
|
|
|
|
`mailbox` = :username,
|
2021-10-29 03:57:19 +08:00
|
|
|
`imap_access` = :imap_access,
|
|
|
|
`smtp_access` = :smtp_access,
|
|
|
|
`eas_access` = :eas_access,
|
|
|
|
`dav_access` = :dav_access,
|
2021-10-29 12:15:10 +08:00
|
|
|
`pop3_access` = :pop3_access,
|
|
|
|
`sieve_access` = :sieve_access,
|
2019-12-22 05:25:09 +08:00
|
|
|
`active` = :active
|
|
|
|
WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':app_name' => $app_name,
|
|
|
|
':username' => $username,
|
2021-10-29 03:57:19 +08:00
|
|
|
':imap_access' => $imap_access,
|
|
|
|
':smtp_access' => $smtp_access,
|
|
|
|
':eas_access' => $eas_access,
|
|
|
|
':dav_access' => $dav_access,
|
2021-10-29 12:15:10 +08:00
|
|
|
':pop3_access' => $pop3_access,
|
|
|
|
':sieve_access' => $sieve_access,
|
2019-12-22 05:25:09 +08:00
|
|
|
':active' => $active,
|
|
|
|
':id' => $id
|
|
|
|
));
|
2019-12-02 18:02:19 +08:00
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
2021-09-02 00:11:00 +08:00
|
|
|
'msg' => array('object_modified', htmlspecialchars(implode(', ', $ids)))
|
2019-12-02 18:02:19 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
$ids = (array)$_data['id'];
|
|
|
|
foreach ($ids as $id) {
|
2019-12-22 05:25:09 +08:00
|
|
|
$stmt = $pdo->prepare("SELECT `mailbox` FROM `app_passwd` WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(':id' => $id));
|
|
|
|
$mailbox = $stmt->fetch(PDO::FETCH_ASSOC)['mailbox'];
|
|
|
|
if (empty($mailbox)) {
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => 'app_passwd_id_invalid'
|
|
|
|
);
|
|
|
|
return false;
|
2019-12-02 18:02:19 +08:00
|
|
|
}
|
2019-12-22 05:25:09 +08:00
|
|
|
if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $mailbox)) {
|
2019-12-02 18:02:19 +08:00
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
2019-12-22 05:25:09 +08:00
|
|
|
'msg' => 'access_denied'
|
2019-12-02 18:02:19 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-22 05:25:09 +08:00
|
|
|
$stmt = $pdo->prepare("DELETE FROM `app_passwd` WHERE `id`= :id");
|
|
|
|
$stmt->execute(array(':id' => $id));
|
2019-12-02 18:02:19 +08:00
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_data_log),
|
|
|
|
'msg' => array('app_passwd_removed', htmlspecialchars($id))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'get':
|
|
|
|
$app_passwds = array();
|
|
|
|
$stmt = $pdo->prepare("SELECT `id`, `name` FROM `app_passwd` WHERE `mailbox` = :username");
|
|
|
|
$stmt->execute(array(':username' => $username));
|
|
|
|
$app_passwds = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
return $app_passwds;
|
|
|
|
break;
|
|
|
|
case 'details':
|
|
|
|
$app_passwd_data = array();
|
2021-10-29 03:57:19 +08:00
|
|
|
$stmt = $pdo->prepare("SELECT *
|
2019-12-02 18:02:19 +08:00
|
|
|
FROM `app_passwd`
|
2019-12-22 05:25:09 +08:00
|
|
|
WHERE `id` = :id");
|
2021-09-02 00:11:00 +08:00
|
|
|
$stmt->execute(array(':id' => $_data));
|
2019-12-02 18:02:19 +08:00
|
|
|
$app_passwd_data = $stmt->fetch(PDO::FETCH_ASSOC);
|
2019-12-22 05:25:09 +08:00
|
|
|
if (empty($app_passwd_data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $app_passwd_data['mailbox'])) {
|
|
|
|
$app_passwd_data = array();
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-07 20:49:21 +08:00
|
|
|
$app_passwd_data['name'] = htmlspecialchars(trim($app_passwd_data['name']));
|
2019-12-02 18:02:19 +08:00
|
|
|
return $app_passwd_data;
|
|
|
|
break;
|
|
|
|
}
|
2021-09-02 00:11:00 +08:00
|
|
|
}
|