root/trunk/gui/client/mail_accounts.php

Revision 1412, 14.3 kB (checked in by scitech, 2 hours ago)

Fixed #1518: Virtual mail problem. Add support for alias subdomain mail (part I)

Line 
1 <?php
2 /**
3  * ispCP ω (OMEGA) a Virtual Hosting Control System
4  *
5  * @copyright     2001-2006 by moleSoftware GmbH
6  * @copyright     2006-2008 by ispCP | http://isp-control.net
7  * @version     SVN: $Id$
8  * @link         http://isp-control.net
9  * @author         ispCP Team
10  *
11  * @license
12  *   This program is free software; you can redistribute it and/or modify it under
13  *   the terms of the MPL General Public License as published by the Free Software
14  *   Foundation; either version 1.1 of the License, or (at your option) any later
15  *   version.
16  *   You should have received a copy of the MPL Mozilla Public License along with
17  *   this program; if not, write to the Open Source Initiative (OSI)
18  *   http://opensource.org | osi@opensource.org
19  */
20
21 require '../include/ispcp-lib.php';
22
23 check_login(__FILE__);
24
25 $tpl = new pTemplate();
26 $tpl->define_dynamic('page', Config::get('CLIENT_TEMPLATE_PATH') . '/mail_accounts.tpl');
27 $tpl->define_dynamic('page_message', 'page');
28 $tpl->define_dynamic('logged_from', 'page');
29 $tpl->define_dynamic('mail_message', 'page');
30 $tpl->define_dynamic('mail_item', 'page');
31 $tpl->define_dynamic('mail_auto_respond', 'mail_item');
32 $tpl->define_dynamic('mails_total', 'page');
33 $tpl->define_dynamic('no_mails', 'page');
34
35 $theme_color = Config::get('USER_INITIAL_THEME');
36
37 $tpl->assign(
38     array('TR_CLIENT_MANAGE_USERS_PAGE_TITLE'    => tr('ispCP - Client/Manage Users'),
39         'THEME_COLOR_PATH'                         => "../themes/$theme_color",
40         'THEME_CHARSET'                            => tr('encoding'),
41         'ISP_LOGO'                                => get_logo($_SESSION['user_id'])
42     )
43 );
44
45 // page functions.
46
47 function gen_user_mail_action($mail_id, $mail_status) {
48     if ($mail_status === Config::get('ITEM_OK_STATUS')) {
49         return array(tr('Delete'), "mail_delete.php?id=$mail_id", tr('Edit'), "mail_edit.php?id=$mail_id");
50     } else {
51         return array(tr('N/A'), '#', tr('N/A'), '#');
52     }
53 }
54
55 function gen_user_mail_auto_respond(&$tpl, $mail_id, $mail_type, $mail_status, $mail_auto_respond) {
56 //     if (preg_match('/_mail/', $mail_type) == 1) {
57         if ($mail_status === Config::get('ITEM_OK_STATUS')) {
58             if ($mail_auto_respond == false) {
59                 $tpl->assign(
60                     array(
61                         'AUTO_RESPOND_DISABLE'            => tr('Enable'),
62                         'AUTO_RESPOND_DISABLE_SCRIPT'    => "mail_autoresponder_enable.php?id=$mail_id",
63                         'AUTO_RESPOND_EDIT'                => '',
64                         'AUTO_RESPOND_EDIT_SCRIPT'        => '',
65                         'AUTO_RESPOND_VIS'                => 'inline'
66                     )
67                 );
68             } else {
69                 $tpl->assign(
70                     array(
71                         'AUTO_RESPOND_DISABLE'            => tr('Disable'),
72                         'AUTO_RESPOND_DISABLE_SCRIPT'    => "mail_autoresponder_disable.php?id=$mail_id",
73                         'AUTO_RESPOND_EDIT'                => tr('Edit'),
74                         'AUTO_RESPOND_EDIT_SCRIPT'        => "mail_autoresponder_edit.php?id=$mail_id",
75                         'AUTO_RESPOND_VIS'                => 'inline'
76                     )
77                 );
78             }
79         } else {
80             $tpl->assign(
81                 array(
82                     'AUTO_RESPOND_DISABLE'            => tr('Please wait for update'),
83                     'AUTO_RESPOND_DISABLE_SCRIPT'    => '',
84                     'AUTO_RESPOND_EDIT'                => '',
85                     'AUTO_RESPOND_EDIT_SCRIPT'        => '',
86                     'AUTO_RESPOND_VIS'                => 'inline'
87                 )
88             );
89         }
90 //     } else {
91 //         $tpl->assign(
92 //             array(
93 //                 'AUTO_RESPOND_DISABLE'            => tr('Please wait for update'),
94 //                 'AUTO_RESPOND_DISABLE_SCRIPT'    => '',
95 //                 'AUTO_RESPOND_EDIT'                => '',
96 //                 'AUTO_RESPOND_EDIT_SCRIPT'        => '',
97 //                 'AUTO_RESPOND_VIS'                => 'none'
98 //             )
99 //         );
100 //     }
101 }
102
103 function gen_page_dmn_mail_list(&$tpl, &$sql, $dmn_id, $dmn_name) {
104     $dmn_query ="
105         SELECT
106             `mail_id`, `mail_acc`, `mail_type`, `status`, `mail_auto_respond`, CONCAT( LEFT(`mail_forward`, 50), IF( LENGTH(`mail_forward`) > 50, '...', '') ) as 'mail_forward'
107         FROM
108             `mail_users`
109         WHERE
110             `domain_id` = ?
111         AND
112             `sub_id` = 0
113         AND
114             (`mail_type` LIKE '%".MT_NORMAL_MAIL."%' OR `mail_type` LIKE '%".MT_NORMAL_FORWARD."%')
115         ORDER BY
116             `mail_acc` ASC,
117             `mail_type` DESC
118     ";
119
120     $rs = exec_query($sql, $dmn_query, array($dmn_id));
121     if ($rs->RecordCount() == 0) {
122         return 0;
123     } else {
124         global $counter;
125         while (!$rs->EOF) {
126             if ($counter % 2 == 0) {
127                 $tpl->assign('ITEM_CLASS', 'content');
128             } else {
129                 $tpl->assign('ITEM_CLASS', 'content2');
130             }
131
132             list($mail_delete, $mail_delete_script, $mail_edit, $mail_edit_script) = gen_user_mail_action($rs->fields['mail_id'], $rs->fields['status']);
133
134             $mail_acc = decode_idna($rs->fields['mail_acc']);
135             $show_dmn_name = decode_idna($dmn_name);
136
137             $mail_types = explode(',', $rs->fields['mail_type']);
138             $mail_type = '';
139
140             foreach ($mail_types as $type) {
141                 $mail_type .= user_trans_mail_type($type);
142                 if (strpos($type, '_forward') !== false) $mail_type .= ': ' . str_replace(array("\r\n", "\n", "\r"), ", ", $rs->fields['mail_forward']);
143                 $mail_type .= '<br />';
144             }
145
146             $tpl->assign(
147                 array(
148                     'MAIL_ACC'            => $mail_acc . "@" . $show_dmn_name,
149                     'MAIL_TYPE'            => $mail_type,
150                     'MAIL_STATUS'        => translate_dmn_status($rs->fields['status']),
151                     'MAIL_DELETE'        => $mail_delete,
152                     'MAIL_DELETE_SCRIPT'=> $mail_delete_script,
153                     'MAIL_EDIT'            => $mail_edit,
154                     'MAIL_EDIT_SCRIPT'    => $mail_edit_script
155                 )
156             );
157
158             gen_user_mail_auto_respond($tpl,
159                 $rs->fields['mail_id'],
160                 $rs->fields['mail_type'],
161                 $rs->fields['status'],
162                 $rs->fields['mail_auto_respond']);
163
164             $tpl->parse('MAIL_ITEM', '.mail_item');
165
166             $rs->MoveNext();
167             $counter ++;
168         }
169
170         return $rs->RecordCount();
171     }
172 }
173
174 function gen_page_sub_mail_list(&$tpl, &$sql, $dmn_id, $dmn_name) {
175     $sub_query = "
176         SELECT
177             t1.`subdomain_id` AS sub_id,
178             t1.`subdomain_name` AS sub_name,
179             t2.`mail_id`,
180             t2.`mail_acc`,
181             t2.`mail_type`,
182             t2.`status`,
183             t2.`mail_auto_respond`,
184             CONCAT( LEFT(t2.`mail_forward`, 50), IF( LENGTH(t2.`mail_forward`) > 50, '...', '') ) as 'mail_forward'
185         FROM
186             `subdomain` AS t1,
187             `mail_users` AS t2
188         WHERE
189             t1.`domain_id` = ?
190         AND
191             t2.`domain_id` = ?
192         AND
193             (t2.`mail_type` LIKE '%".MT_SUBDOM_MAIL."%' OR t2.`mail_type` LIKE '%".MT_SUBDOM_FORWARD."%')
194         AND
195             t1.`subdomain_id` = t2.`sub_id`
196         ORDER BY
197             t2.`mail_acc` ASC,
198             t2.`mail_type` DESC
199     ";
200
201     $rs = exec_query($sql, $sub_query, array($dmn_id, $dmn_id));
202
203     if ($rs->RecordCount() == 0) {
204         return 0;
205     } else {
206         global $counter;
207         while (!$rs->EOF) {
208             if ($counter % 2 == 0) {
209                 $tpl->assign('ITEM_CLASS', 'content');
210             } else {
211                 $tpl->assign('ITEM_CLASS', 'content2');
212             }
213
214             list($mail_delete, $mail_delete_script, $mail_edit, $mail_edit_script) = gen_user_mail_action($rs->fields['mail_id'], $rs->fields['status']);
215
216             $mail_acc = decode_idna($rs->fields['mail_acc']);
217
218             $show_sub_name = decode_idna($rs->fields['sub_name']);
219             $show_dmn_name = decode_idna($dmn_name);
220
221             $mail_types = explode(',', $rs->fields['mail_type']);
222             $mail_type = '';
223
224             foreach ($mail_types as $type) {
225                 $mail_type .= user_trans_mail_type($type);
226                 if (strpos($type, '_forward') !== false) $mail_type .= ': ' . str_replace(array("\r\n", "\n", "\r"), ", ", $rs->fields['mail_forward']);
227                 $mail_type .= '<br />';
228             }
229
230
231             $tpl->assign(
232                 array(
233                     'MAIL_ACC'            => $mail_acc . "@" . $show_sub_name . "." . $show_dmn_name,
234                     'MAIL_TYPE'            => $mail_type,
235                     'MAIL_STATUS'        => translate_dmn_status($rs->fields['status']),
236                     'MAIL_DELETE'        => $mail_delete,
237                     'MAIL_DELETE_SCRIPT'=> $mail_delete_script,
238                     'MAIL_EDIT'            => $mail_edit,
239                     'MAIL_EDIT_SCRIPT'    => $mail_edit_script
240                 )
241             );
242
243             gen_user_mail_auto_respond($tpl,
244                 $rs->fields['mail_id'],
245                 $rs->fields['mail_type'],
246                 $rs->fields['status'],
247                 $rs->fields['mail_auto_respond']);
248
249             $tpl->parse('MAIL_ITEM', '.mail_item');
250
251             $rs->MoveNext();
252             $counter ++;
253         }
254
255         return $rs->RecordCount();
256     }
257 }
258
259 function gen_page_als_sub_mail_list(&$tpl, &$sql, $dmn_id, $dmn_name) {
260     $sub_query = '
261         SELECT
262             t1.`mail_id`,
263             t1.`mail_acc`,
264             t1.`mail_type`,
265             t1.`status`,
266             t1.`mail_auto_respond`,
267             CONCAT( LEFT(t1.`mail_forward`, 50), IF( LENGTH(t1.`mail_forward`) > 50, \'...\', \'\') ) AS \'mail_forward\',
268             CONCAT(t2.`subdomain_alias_name`,\'.\',t3.`alias_name`) AS \'alssub_name\'
269         FROM
270             `mail_users` AS t1
271         LEFT JOIN (`subdomain_alias` as t2) ON (t1.`sub_id`=t2.`subdomain_alias_id`)
272         LEFT JOIN (`domain_aliasses` as t3) ON (t2.`alias_id`=t3.`alias_id`)
273         WHERE
274             t1.`domain_id` = ?
275         AND
276             (t1.`mail_type` LIKE \'%'.MT_ALSSUB_MAIL.'%\' OR t1.`mail_type` LIKE \'%'.MT_ALSSUB_FORWARD.'%\')
277         ORDER BY
278             t1.`mail_acc` ASC,
279             t1.`mail_type` DESC
280     ';
281
282     $rs = exec_query($sql, $sub_query, array($dmn_id));
283
284     if ($rs->RecordCount() == 0) {
285         return 0;
286     } else {
287         global $counter;
288         while (!$rs->EOF) {
289             if ($counter % 2 == 0) {
290                 $tpl->assign('ITEM_CLASS', 'content');
291             } else {
292                 $tpl->assign('ITEM_CLASS', 'content2');
293             }
294
295             list($mail_delete, $mail_delete_script, $mail_edit, $mail_edit_script) = gen_user_mail_action($rs->fields['mail_id'], $rs->fields['status']);
296
297             $mail_acc = decode_idna($rs->fields['mail_acc']);
298
299             $show_alssub_name = decode_idna($rs->fields['alssub_name']);
300
301             $mail_types = explode(',', $rs->fields['mail_type']);
302             $mail_type = '';
303
304             foreach ($mail_types as $type) {
305                 $mail_type .= user_trans_mail_type($type);
306                 if (strpos($type, '_forward') !== false) $mail_type .= ': ' . str_replace(array("\r\n", "\n", "\r"), ", ", $rs->fields['mail_forward']);
307                 $mail_type .= '<br />';
308             }
309
310
311             $tpl->assign(
312                 array(
313                     'MAIL_ACC'            => $mail_acc . "@" . $show_alssub_name,
314                     'MAIL_TYPE'            => $mail_type,
315                     'MAIL_STATUS'        => translate_dmn_status($rs->fields['status']),
316                     'MAIL_DELETE'        => $mail_delete,
317                     'MAIL_DELETE_SCRIPT'=> $mail_delete_script,
318                     'MAIL_EDIT'            => $mail_edit,
319                     'MAIL_EDIT_SCRIPT'    => $mail_edit_script
320                 )
321             );
322
323             gen_user_mail_auto_respond($tpl,
324                 $rs->fields['mail_id'],
325                 $rs->fields['mail_type'],
326                 $rs->fields['status'],
327                 $rs->fields['mail_auto_respond']);
328
329             $tpl->parse('MAIL_ITEM', '.mail_item');
330
331             $rs->MoveNext();
332             $counter ++;
333         }
334
335         return $rs->RecordCount();
336     }
337 }
338
339 function gen_page_als_mail_list(&$tpl, &$sql, $dmn_id, $dmn_name) {
340     $als_query = "
341         SELECT
342             t1.`alias_id` AS als_id,
343             t1.`alias_name` AS als_name,
344             t2.`mail_id`,
345             t2.`mail_acc`,
346             t2.`mail_type`,
347             t2.`status`,
348             t2.`mail_auto_respond`,
349             CONCAT( LEFT(t2.`mail_forward`, 50), IF( LENGTH(t2.`mail_forward`) > 50, '...', '') ) as 'mail_forward'
350         FROM
351             `domain_aliasses` AS t1,
352             `mail_users` AS t2
353         WHERE
354             t1.`domain_id` = ?
355         AND
356             t2.`domain_id` = ?
357         AND
358             t1.`alias_id` = t2.`sub_id`
359         AND
360             (t2.`mail_type` LIKE '%".MT_ALIAS_MAIL."%' OR t2.`mail_type` LIKE '%".MT_ALIAS_FORWARD."%')
361         ORDER BY
362             t2.`mail_acc` ASC,
363             t2.`mail_type` DESC
364     ";
365
366     $rs = exec_query($sql, $als_query, array($dmn_id, $dmn_id));
367
368     if ($rs->RecordCount() == 0) {
369         return 0;
370     } else {
371         global $counter;
372         while (!$rs->EOF) {
373             if ($counter % 2 == 0) {
374                 $tpl->assign('ITEM_CLASS', 'content');
375             } else {
376                 $tpl->assign('ITEM_CLASS', 'content2');
377             }
378             list($mail_delete, $mail_delete_script, $mail_edit, $mail_edit_script) = gen_user_mail_action($rs->fields['mail_id'], $rs->fields['status']);
379
380             $mail_acc = decode_idna($rs->fields['mail_acc']);
381             $show_dmn_name = decode_idna($dmn_name);
382
383             $show_als_name = decode_idna($rs->fields['als_name']);
384
385              $mail_types = explode(',', $rs->fields['mail_type']);
386             $mail_type = '';
387
388              foreach ($mail_types as $type) {
389                 $mail_type .= user_trans_mail_type($type);
390                 if (strpos($type, '_forward') !== false) $mail_type .= ': ' . str_replace(array("\r\n", "\n", "\r"), ", ", $rs->fields['mail_forward']);
391                 $mail_type .= '<br />';
392             }
393
394             $tpl->assign(
395                 array(
396                     'MAIL_ACC'            => $mail_acc . "@" . $show_als_name,
397                     'MAIL_TYPE'            => $mail_type,
398                     'MAIL_STATUS'        => translate_dmn_status($rs->fields['status']),
399                     'MAIL_DELETE'        => $mail_delete,
400                     'MAIL_DELETE_SCRIPT'=> $mail_delete_script,
401                     'MAIL_EDIT'            => $mail_edit,
402                     'MAIL_EDIT_SCRIPT'    => $mail_edit_script
403                     )
404                 );
405
406             gen_user_mail_auto_respond($tpl,
407                 $rs->fields['mail_id'],
408                 $rs->fields['mail_type'],
409                 $rs->fields['status'],
410                 $rs->fields['mail_auto_respond']);
411
412             $tpl->parse('MAIL_ITEM', '.mail_item');
413
414             $rs->MoveNext();
415             $counter ++;
416         }
417
418         return $rs->RecordCount();
419     }
420 }
421
422 function gen_page_lists(&$tpl, &$sql, $user_id) {
423     list($dmn_id,
424         $dmn_name,
425         $dmn_gid,
426         $dmn_uid,
427         $dmn_created_id,
428         $dmn_created,
429         $dmn_last_modified,
430         $dmn_mailacc_limit,
431         $dmn_ftpacc_limit,
432         $dmn_traff_limit,
433         $dmn_sqld_limit,
434         $dmn_sqlu_limit,
435         $dmn_status,
436         $dmn_als_limit,
437         $dmn_subd_limit,
438         $dmn_ip_id,
439         $dmn_disk_limit,
440         $dmn_disk_usage,
441         $dmn_php,
442         $dmn_cgi) = get_domain_default_props($sql, $user_id);
443
444     $dmn_mails = gen_page_dmn_mail_list($tpl, $sql, $dmn_id, $dmn_name);
445     $sub_mails = gen_page_sub_mail_list($tpl, $sql, $dmn_id, $dmn_name);
446     $alssub_mails = gen_page_als_sub_mail_list($tpl, $sql, $dmn_id, $dmn_name);
447     $als_mails = gen_page_als_mail_list($tpl, $sql, $dmn_id, $dmn_name);
448     $total_mails = $dmn_mails + $sub_mails + $als_mails+$alssub_mails;
449
450     if ($total_mails > 0) {
451         $tpl->assign(
452             array(
453                 'MAIL_MESSAGE'            => '',
454                 'DMN_TOTAL'                => $dmn_mails,
455                 'SUB_TOTAL'                => $sub_mails,
456                 'ALSSUB_TOTAL'                => $sub_mails,
457                 'ALS_TOTAL'                => $als_mails,
458                 'TOTAL_MAIL_ACCOUNTS'    => $total_mails,
459                 'ALLOWED_MAIL_ACCOUNTS'    => (($dmn_mailacc_limit != 0) ? $dmn_mailacc_limit : tr('unlimited'))
460             )
461         );
462     } else {
463         $tpl->assign(
464             array(
465                 'MAIL_MSG'        => tr('Mail accounts list is empty!'),
466                 'MAIL_ITEM'        => '',