-
Notifications
You must be signed in to change notification settings - Fork 119
/
mail_edit.php
382 lines (331 loc) · 14.1 KB
/
mail_edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* i-MSCP - internet Multi Server Control Panel
* Copyright (C) 2010-2017 by i-MSCP Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
use iMSCP\Crypt as Crypt;
/***********************************************************************************************************************
* Functions
*/
/**
* Get Email account data
*
* @param int $mailId Mail account unique identifier
* @return array Email account data
*/
function client_getEmailAccountData($mailId)
{
static $mailData = NULL;
if (NULL !== $mailData) {
return $mailData;
}
$stmt = exec_query('SELECT * FROM mail_users WHERE mail_id = ? AND domain_id = ?', array(
$mailId, get_user_domain_id($_SESSION['user_id'])
));
if (!$stmt->rowCount()) {
showBadRequestErrorPage();
}
return $stmt->fetchRow();
}
/**
* Edit mail account
*
* @throws iMSCP_Exception
* @return bool TRUE on success, FALSE otherwise
*/
function client_editMailAccount()
{
if (!isset($_POST['password'])
|| !isset($_POST['password_rep'])
|| !isset($_POST['quota'])
|| !isset($_POST['forward_list'])
|| !isset($_POST['account_type'])
|| !in_array($_POST['account_type'], array('1', '2', '3'), true)
) {
showBadRequestErrorPage();
}
$mailData = client_getEmailAccountData(clean_input($_GET['id']));
$mainDmnProps = get_domain_default_props($_SESSION['user_id']);
$password = $forwardList = '_no_';
$mailType = '';
$mailQuotaLimitBytes = NULL;
if (!preg_match('/^(.*?)_(?:mail|forward)/', $mailData['mail_type'], $match)) {
throw new iMSCP_Exception('Could not determine mail type');
}
$domainType = $match[1];
$mailTypeNormal = in_array($_POST['account_type'], array('1', '3'));
$mailTypeForward = in_array($_POST['account_type'], array('2', '3'));
if (!$mailTypeNormal && !$mailTypeForward) {
showBadRequestErrorPage();
}
$mailAddr = $mailData['mail_addr'];
if ($mailTypeNormal) {
$password = clean_input($_POST['password']);
$passwordRep = clean_input($_POST['password_rep']);
if ($mailData['mail_pass'] == '_no_' || $password != '' || $passwordRep != '') {
if ($password == '') {
set_page_message(tr('Password is missing.'), 'error');
return false;
}
if ($passwordRep == '') {
set_page_message(tr('You must confirm your password.'), 'error');
return false;
}
if ($password !== $passwordRep) {
set_page_message(tr("Passwords do not match."), 'error');
return false;
}
if (!checkPasswordSyntax($password)) {
return false;
}
$password = Crypt::sha512($password);
} else {
$password = $mailData['mail_pass'];
}
// Check for quota
$customerEmailQuotaLimitBytes = (int)$mainDmnProps['mail_quota'];
$mailQuotaLimitBytes = intval($_POST['quota']) * 1048576; // MiB to Bytes
if ($customerEmailQuotaLimitBytes > 0) {
if ($mailQuotaLimitBytes < 1) {
set_page_message(tr('Incorrect Email quota.'), 'error');
return false;
}
$stmt = exec_query(
'
SELECT IFNULL(SUM(quota), 0) AS quota
FROM mail_users
WHERE mail_id <> ?
AND domain_id = ?
AND quota IS NOT NULL
',
array($mailData['mail_id'], $mainDmnProps['domain_id'])
);
$customerMailboxesQuotaSumBytes = (int)$stmt->fetchRow(PDO::FETCH_COLUMN);
if ($customerMailboxesQuotaSumBytes >= $customerEmailQuotaLimitBytes) {
showBadRequestErrorPage(); # Customer should never goes here excepted if it try to bypass js code
}
if ($mailQuotaLimitBytes > floor($customerEmailQuotaLimitBytes - $customerMailboxesQuotaSumBytes)) {
set_page_message(
tr('Email quota cannot be bigger than %s', bytesHuman($mailQuotaLimitBytes, NULL, 0)), 'error'
);
return false;
}
}
switch ($domainType) {
case 'normal':
$mailType = MT_NORMAL_MAIL;
break;
case 'subdom':
$mailType = MT_SUBDOM_MAIL;
break;
case 'alias':
$mailType = MT_ALIAS_MAIL;
break;
case 'alssub':
$mailType = MT_ALSSUB_MAIL;
}
}
if ($mailTypeForward) {
$forwardList = clean_input($_POST['forward_list']);
if ($forwardList == '') {
set_page_message(tr('Forward list is empty.'), 'error');
return false;
}
$forwardList = preg_split("/[\n,]+/", $forwardList);
foreach ($forwardList as $key => &$forwardEmailAddr) {
$forwardEmailAddr = encode_idna(mb_strtolower(trim($forwardEmailAddr)));
if ($forwardEmailAddr == '') {
unset($forwardList[$key]);
} elseif (!chk_email($forwardEmailAddr)) {
set_page_message(tr('Wrong mail syntax in forward list.'), 'error');
return false;
} elseif ($forwardEmailAddr == $mailAddr) {
set_page_message(tr('You cannot forward %s on itself.', $mailAddr), 'error');
return false;
}
}
$forwardList = implode(',', array_unique($forwardList));
switch ($domainType) {
case 'normal':
$mailType .= (($mailType != '') ? ',' : '') . MT_NORMAL_FORWARD;
break;
case 'subdom':
$mailType .= (($mailType != '') ? ',' : '') . MT_SUBDOM_FORWARD;
break;
case 'alias':
$mailType .= (($mailType != '') ? ',' : '') . MT_ALIAS_FORWARD;
break;
case 'alssub':
$mailType .= (($mailType != '') ? ',' : '') . MT_ALSSUB_FORWARD;
}
}
iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onBeforeEditMail, array(
'mailId' => $mailData['mail_id']
));
exec_query(
'UPDATE mail_users SET mail_pass = ?, mail_forward = ?, mail_type = ?, status = ?, quota = ? WHERE mail_id = ?',
array($password, $forwardList, $mailType, 'tochange', $mailQuotaLimitBytes, $mailData['mail_id'])
);
# Force synching of quota info on next load (or remove cached data in case of normal account changed to forward account)
$postfixConfig = new iMSCP_Config_Handler_File(
utils_normalizePath(iMSCP_Registry::get('config')->CONF_DIR . '/postfix/postfix.data')
);
list($user, $domain) = explode('@', $mailAddr);
unset($_SESSION['maildirsize'][utils_normalizePath($postfixConfig['MTA_VIRTUAL_MAIL_DIR'] . "/$domain/$user/maildirsize")]);
iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onAfterEditMail, array(
'mailId' => $mailData['mail_id']
));
send_request();
write_log("{$_SESSION['user_logged']}: Updated Email account: $mailAddr", E_USER_NOTICE);
set_page_message(tr('Email account successfully scheduled for update.'), 'success');
return true;
}
/**
* Generate page
*
* @param iMSCP_pTemplate $tpl
*/
function client_generatePage($tpl)
{
$mailId = clean_input($_GET['id']);
$mainDmnProps = get_domain_default_props($_SESSION['user_id']);
$mailData = client_getEmailAccountData($mailId);
list($username, $domainName) = explode('@', $mailData['mail_addr']);
$stmt = exec_query(
'
SELECT IFNULL(SUM(quota), 0) AS quota
FROM mail_users
WHERE mail_id <> ?
AND domain_id = ?
AND quota IS NOT NULL
',
array($mailId, $mainDmnProps['domain_id'])
);
$customerMailboxesQuotaSumBytes = (int)$stmt->fetchRow(PDO::FETCH_COLUMN);
$customerEmailQuotaLimitBytes = (int)$mainDmnProps['mail_quota'];
if ($customerEmailQuotaLimitBytes < 1) {
$tpl->assign(array(
'TR_QUOTA' => tohtml(tr('Quota in MiB (0 for unlimited)')),
'MIN_QUOTA' => 0,
'MAX_QUOTA' => tohtml(floor(PHP_INT_MAX / 1048576), 'htmlAttr'),
'QUOTA' => isset($_POST['quota'])
? tohtml(intval($_POST['quota']), 'htmlAttr') : tohtml(floor($mailData['quota'] / 1048576), 'htmlAttr')
));
$mailTypeForwardOnly = false;
} else {
if ($customerEmailQuotaLimitBytes > $customerMailboxesQuotaSumBytes) {
$mailQuotaLimitBytes = floor($customerEmailQuotaLimitBytes - $customerMailboxesQuotaSumBytes);
$mailMaxQuotaLimitMib = floor($mailQuotaLimitBytes / 1048576);
$mailQuotaLimitMiB = ($mailData['quota'] > 0 && $mailData['quota'] < $mailQuotaLimitBytes)
? floor($mailData['quota'] / 1048576) : min(10, $mailMaxQuotaLimitMib);
$mailTypeForwardOnly = false;
} else {
set_page_message(tr('You cannot change this account to normal email account because you have already assigned all your email quota to other mailboxes. If you want to change this account to normal email account, you must first lower the quota assigned to one of your other mailboxes.'), 'static_info');
set_page_message(tr('For the time being, you can only edit your forwarded email account.'), 'static_info');
$mailQuotaLimitBytes = 1048576; // Only for sanity. Customer won't be able to switch to normal email account
$mailMaxQuotaLimitMib = 1;
$mailQuotaLimitMiB = 1;
$mailTypeForwardOnly = true;
}
$tpl->assign(array(
'TR_QUOTA' => tohtml(tr('Quota in MiB (Max: %s)', bytesHuman($mailQuotaLimitBytes, NULL, 0))),
'MIN_QUOTA' => 1,
'MAX_QUOTA' => tohtml($mailMaxQuotaLimitMib, 'htmlAttr'),
'QUOTA' => isset($_POST['quota'])
? tohtml(intval($_POST['quota']), 'htmlAttr') : tohtml($mailQuotaLimitMiB, 'htmlAttr')
));
}
$mailType = '';
if (!isset($_POST['account_type'])
|| !in_array($_POST['account_type'], array('1', '2', '3'))
) {
if (preg_match('/_mail/', $mailData['mail_type'])) {
$mailType = '1';
}
if (preg_match('/_forward/', $mailData['mail_type'])) {
$mailType = ($mailType == '1') ? '3' : '2';
}
} else {
$mailType = $_POST['account_type'];
}
$tpl->assign(array(
'MAIL_ID' => tohtml($mailId),
'USERNAME' => tohtml($username),
'NORMAL_CHECKED' => ($mailType == '1') ? ' checked' : '',
'FORWARD_CHECKED' => ($mailType == '2') ? ' checked' : '',
'NORMAL_FORWARD_CHECKED' => ($mailType == '3') ? ' checked' : '',
'PASSWORD' => isset($_POST['password']) ? tohtml($_POST['password']) : '',
'PASSWORD_REP' => isset($_POST['password_rep']) ? tohtml($_POST['password_rep']) : '',
'FORWARD_LIST' => isset($_POST['forward_list'])
? tohtml($_POST['forward_list'])
: (
$mailData['mail_forward'] != '_no_'
? tohtml($mailData['mail_forward'])
: ''
),
'DOMAIN_NAME' => tohtml($domainName),
'DOMAIN_NAME_UNICODE' => tohtml(decode_idna($domainName)),
'DOMAIN_NAME_SELECTED' => ' selected'
));
iMSCP_Events_Aggregator::getInstance()->registerListener(
'onGetJsTranslations',
function ($event) use ($mailTypeForwardOnly) {
/** @var $event iMSCP_Events_Description */
$event->getParam('translations')->core['mail_add_forward_only'] = $mailTypeForwardOnly;
}
);
}
/***********************************************************************************************************************
* Main
*/
require 'imscp-lib.php';
iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onClientScriptStart);
check_login('user');
if (!isset($_GET['id']) || !customerHasFeature('mail')) {
showBadRequestErrorPage();
}
if (!empty($_POST) && client_editMailAccount()) {
redirectTo('mail_accounts.php');
}
$tpl = new iMSCP_pTemplate();
$tpl->define_dynamic(array(
'layout' => 'shared/layouts/ui.tpl',
'page' => 'client/mail_edit.tpl',
'page_message' => 'layout'
));
$tpl->assign(array(
'TR_PAGE_TITLE' => tr('Client / Email / Edit Email Account'),
'TR_MAIl_ACCOUNT_DATA' => tr('Email account data'),
'TR_USERNAME' => tr('Username'),
'TR_DOMAIN_NAME' => tr('Domain name'),
'TR_MAIL_ACCOUNT_TYPE' => tr('Mail account type'),
'TR_NORMAL_MAIL' => tr('Normal'),
'TR_FORWARD_MAIL' => tr('Forward'),
'TR_FORWARD_NORMAL_MAIL' => tr('Normal + Forward'),
'TR_PASSWORD' => tr('Password'),
'TR_PASSWORD_REPEAT' => tr('Password confirmation'),
'TR_FORWARD_TO' => tr('Forward to'),
'TR_FWD_HELP' => tr('Separate multiple email addresses by comma or a line-break.'),
'TR_UPDATE' => tr('Update'),
'TR_CANCEL' => tr('Cancel')
));
client_generatePage($tpl);
generateNavigation($tpl);
generatePageMessage($tpl);
$tpl->parse('LAYOUT_CONTENT', 'page');
iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onClientScriptEnd, array('templateEngine' => $tpl));
$tpl->prnt();