-
Notifications
You must be signed in to change notification settings - Fork 119
/
mail_accounts.php
328 lines (285 loc) · 11.4 KB
/
mail_accounts.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
<?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.
*/
// TODO (nuxwin): DataTables server-side processing
/***********************************************************************************************************************
* Functions
*/
/**
* Count the number of email addresses created by default
*
* @param int $mainDmnId Main domain id
* @return int Number of default mails adresses
*/
function _client_countDefaultMails($mainDmnId)
{
$stmt = exec_query(
'
SELECT
COUNT(mail_id)
FROM
mail_users
WHERE
domain_id = ?
AND
(status = ? OR status = ?)
AND
(mail_acc = ? OR mail_acc = ? OR mail_acc = ?)
',
array($mainDmnId, 'ok', 'toadd', 'abuse', 'postmaster', 'webmaster')
);
return $stmt->fetchRow(PDO::FETCH_COLUMN);
}
/**
* Generate user mail action links
*
* @param int $mailId Mail account unique identifier
* @param string $mailStatus Mail account status
* @return array
*/
function _client_generateUserMailAction($mailId, $mailStatus)
{
if ($mailStatus == 'ok') {
return array(tr('Delete'), "mail_delete.php?id=$mailId", tr('Edit'), "mail_edit.php?id=$mailId");
}
return array(tr('N/A'), '#', tr('N/A'), '#');
}
/**
* Generate auto-resonder action links
*
* @param iMSCP_pTemplate $tpl pTemplate instance
* @param int $mailId Mail uique identifier
* @param string $mailStatus Mail status
* @param bool $mailAutoRespond
* @return void
*/
function _client_generateUserMailAutoRespond($tpl, $mailId, $mailStatus, $mailAutoRespond)
{
if ($mailStatus != 'ok') {
$tpl->assign('AUTO_RESPOND_ITEM', '');
return;
}
if (!$mailAutoRespond) {
$tpl->assign(array(
'AUTO_RESPOND' => tr('Enable'),
'AUTO_RESPOND_SCRIPT' => "mail_autoresponder_enable.php?mail_account_id=$mailId",
'AUTO_RESPOND_EDIT_LINK' => ''
));
} else {
$tpl->assign(array(
'AUTO_RESPOND' => tr('Disable'),
'AUTO_RESPOND_SCRIPT' => "mail_autoresponder_disable.php?mail_account_id=$mailId",
'AUTO_RESPOND_EDIT' => tr('Edit'),
'AUTO_RESPOND_EDIT_SCRIPT' => "mail_autoresponder_edit.php?mail_account_id=$mailId",
));
$tpl->parse('AUTO_RESPOND_EDIT_LINK', 'auto_respond_edit_link');
}
$tpl->parse('AUTO_RESPOND_ITEM', 'auto_respond_item');
}
/**
* Generate Mail accounts list
*
* @param iMSCP_pTemplate $tpl Template engine
* @param int $mainDmnId Customer main domain unique identifier
* @return int number of mail accounts
*/
function _client_generateMailAccountsList($tpl, $mainDmnId)
{
$stmt = exec_query(
"
SELECT mail_id, CONCAT(LEFT(mail_forward, 30), IF(LENGTH(mail_forward) > 30, '...', '')) AS mail_forward,
mail_type, status, mail_auto_respond, quota, mail_addr
FROM mail_users
WHERE domain_id = ?
AND mail_type NOT LIKE '%catchall%'
ORDER BY mail_addr ASC, mail_type DESC
",
$mainDmnId
);
$rowCount = $stmt->rowCount();
if (!$rowCount) {
return 0;
}
$postfixConfig = new iMSCP_Config_Handler_File(
utils_normalizePath(iMSCP_Registry::get('config')->CONF_DIR . '/postfix/postfix.data')
);
$syncQuotaInfo = isset($_GET['sync_quota_info']);
$hasMailboxes = false;
$overQuota = false;
while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
list($mailDelete, $mailDeleteScript, $mailEdit, $mailEditScript) = _client_generateUserMailAction(
$row['mail_id'], $row['status']
);
$mailAddr = $row['mail_addr'];
$mailTypes = explode(',', $row['mail_type']);
$mailType = '';
foreach ($mailTypes as $type) {
$mailType .= user_trans_mail_type($type);
if (strpos($type, '_forward') !== false) {
$mailType .= ': ' . str_replace(',', ', ', $row['mail_forward']);
}
$mailType .= '<br>';
}
if ($row['status'] == 'ok' && strpos($row['mail_type'], '_mail') !== false) {
$hasMailboxes = true;
list($user, $domain) = explode('@', $row['mail_addr']);
$maildirsize = parseMaildirsize(
utils_normalizePath($postfixConfig['MTA_VIRTUAL_MAIL_DIR'] . "/$domain/$user/maildirsize"),
$syncQuotaInfo
);
if ($maildirsize !== FALSE) {
$quotaPercent = ($maildirsize['QUOTA_BYTES'] > 0)
? ceil(100 * $maildirsize['BYTE_COUNT'] / $maildirsize['QUOTA_BYTES']) : 0;
if ($quotaPercent) {
if ($quotaPercent >= 100) {
$overQuota = true;
}
$mailQuotaInfo = sprintf(
($quotaPercent >= 95)
? '<span style="color:red">%s / %s (%s%%)</span>'
: '%s / %s (%s%%)',
bytesHuman($maildirsize['BYTE_COUNT'], NULL, 1),
bytesHuman($maildirsize['QUOTA_BYTES'], NULL, 1),
$quotaPercent
);
} else {
$mailQuotaInfo = sprintf(
'%s / %s (%s%%)',
bytesHuman($maildirsize['BYTE_COUNT'], NULL, 1),
($maildirsize['QUOTA_BYTES'] > 1)
? bytesHuman($maildirsize['QUOTA_BYTES'], NULL, 1) : tr('Unlimited'),
0
);
}
} else {
$mailQuotaInfo = tr('Unavailable');
}
} else {
$mailQuotaInfo = tr('Unavailable');
}
$tpl->assign(array(
'MAIL_ADDR' => tohtml(decode_idna($mailAddr)),
'MAIL_TYPE' => $mailType,
'MAIL_QUOTA_INFO' => $mailQuotaInfo,
'MAIL_STATUS' => translate_dmn_status($row['status']),
'MAIL_DELETE' => $mailDelete,
'MAIL_DELETE_SCRIPT' => $mailDeleteScript,
'MAIL_EDIT' => $mailEdit,
'MAIL_EDIT_SCRIPT' => $mailEditScript,
'DEL_ITEM' => $row['mail_id'],
'DISABLED_DEL_ITEM' => ($row['status'] != 'ok') ? ' disabled' : ''
));
_client_generateUserMailAutoRespond($tpl, $row['mail_id'], $row['status'], $row['mail_auto_respond']);
$tpl->parse('MAIL_ITEM', '.mail_item');
}
if ($syncQuotaInfo) {
set_page_message(tr('Mailboxes quota info were synced.'), 'success');
redirectTo('mail_accounts.php');
}
if (!$hasMailboxes) {
$tpl->assign('SYNC_QUOTA_INFO_LINK', '');
}
if ($overQuota) {
set_page_message(tr('At least one of your mailbox is over quota.'), 'static_warning');
}
return $rowCount;
}
/**
* Generate page
*
* @param iMSCP_pTemplate $tpl Reference to the pTemplate object
* @return void
*/
function client_generatePage($tpl)
{
if (!customerHasFeature('mail')) {
$tpl->assign('MAIL_FEATURE', '');
set_page_message(tr('Mail feature is disabled.'), 'static_info');
return;
}
$cfg = iMSCP_Registry::get('config');
$dmnProps = get_domain_default_props($_SESSION['user_id']);
$mainDmnId = $dmnProps['domain_id'];
$dmnMailAccLimit = $dmnProps['domain_mailacc_limit'];
$countedMails = _client_generateMailAccountsList($tpl, $mainDmnId);
$defaultMails = _client_countDefaultMails($mainDmnId);
if (!$cfg['COUNT_DEFAULT_EMAIL_ADDRESSES']) {
$countedMails -= $defaultMails;
}
$totalMails = tr(
'Total mails: %s / %s %s', $countedMails, translate_limit_value($dmnMailAccLimit), ($defaultMails)
? (($cfg['COUNT_DEFAULT_EMAIL_ADDRESSES'])
? '(' . tr('Incl. default mails') . ')'
: '(' . tr('Excl. default mails') . ')')
: ''
);
if ($countedMails || $defaultMails) {
$tpl->assign('TOTAL_MAIL_ACCOUNTS', $totalMails);
return;
}
$tpl->assign('MAIL_ITEMS', '');
set_page_message(tr('Mail accounts list is empty.'), 'static_info');
}
/***********************************************************************************************************************
* Main
*/
require_once 'imscp-lib.php';
iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onClientScriptStart);
check_login('user');
if (!customerHasMailOrExtMailFeatures()) {
showBadRequestErrorPage();
}
$tpl = new iMSCP_pTemplate();
$tpl->define_dynamic(array(
'layout' => 'shared/layouts/ui.tpl',
'page' => 'client/mail_accounts.tpl',
'page_message' => 'layout',
'mail_feature' => 'page',
'mail_items' => 'mail_feature',
'mail_item' => 'mail_items',
'auto_respond_item' => 'mail_item',
'auto_respond_edit_link' => 'auto_respond_item',
'sync_quota_info_link' => 'mail_items'
));
$tpl->assign(array(
'TR_PAGE_TITLE' => tr('Client / Email / Overview'),
'TR_MAIL' => tr('Mail'),
'TR_TYPE' => tr('Type'),
'TR_QUOTA_INFO' => tr('Quota info'),
'TR_STATUS' => tr('Status'),
'TR_ACTIONS' => tr('Actions'),
'TR_AUTORESPOND' => tr('Auto responder'),
'TR_DELETE' => tr('Delete'),
'TR_MESSAGE_DELETE' => tojs(tr('Are you sure you want to delete %s?', '%s')),
'TR_MESSAGE_DELETE_SELECTED_ITEMS' => tojs(tr('Are you sure you want to delete all selected mail accounts?')),
'TR_DELETE_SELECTED_ITEMS' => tr('Delete selected mail accounts'),
'TR_MESSAGE_DELETE_SELECTED_ITEMS_ERR' => tojs(tr('You must select a least one mail account to delete')),
'TR_SYNC_QUOTA_INFO' => tr('Sync quota info'),
'TR_SYNC_QUOTA_TOOLTIP' => tohtml(tr('Force synching of mailboxes quota info. Quota info are automatically synced every 5 minutes.'), 'htmlAttr')
));
iMSCP_Events_Aggregator::getInstance()->registerListener('onGetJsTranslations', function ($e) {
/** @var $e \iMSCP_Events_Event */
$e->getParam('translations')->core['dataTable'] = getDataTablesPluginTranslations(false);
});
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();