root/trunk/gui/client/mail_edit.php

Revision 1412, 11.5 kB (checked in by scitech, 9 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_edit.tpl');
27 $tpl->define_dynamic('page_message', 'page');
28 $tpl->define_dynamic('logged_from', 'page');
29 $tpl->define_dynamic('normal_mail', 'page');
30 $tpl->define_dynamic('forward_mail', 'page');
31
32 // page functions
33
34 function edit_mail_account(&$tpl, &$sql) {
35     if (!isset($_GET['id']) || $_GET['id'] === '' || !is_numeric($_GET['id'])) {
36         set_page_message(tr('Email account not found!'));
37         header('Location: mail_accounts.php');
38         die();
39     } else {
40         $mail_id = $_GET['id'];
41     }
42
43     $dmn_name = $_SESSION['user_logged'];
44
45     $query = "
46         SELECT
47             t1.*, t2.`domain_id`, t2.`domain_name`
48         FROM
49             `mail_users` as t1,
50             `domain` as t2
51         WHERE
52             t1.`mail_id` = ?
53         AND
54             t2.`domain_id` = t1.`domain_id`
55         AND
56             t2.`domain_name` = ?
57     ";
58
59     $rs = exec_query($sql, $query, array($mail_id, $dmn_name));
60
61     if ($rs->RecordCount() == 0) {
62         set_page_message(tr('User does not exist or you do not have permission to access this interface!'));
63         header('Location: mail_accounts.php');
64         die();
65     } else {
66         $mail_acc = $rs->fields['mail_acc'];
67         $domain_id = $rs->fields['domain_id'];
68         $mail_type_list = $rs->fields['mail_type'];
69         $mail_forward = $rs->fields['mail_forward'];
70         $sub_id = $rs->fields['sub_id'];
71
72         foreach (explode(',', $mail_type_list) as $mail_type) {
73             if ($mail_type == MT_NORMAL_MAIL) {
74                 $mtype[] = 1;
75                 $res1 = exec_query($sql, "SELECT `domain_name` FROM `domain` WHERE `domain_id`=?", array($domain_id));
76                 $tmp1 = $res1->FetchRow(0);
77                  $maildomain = $tmp1['domain_name'];
78             } else if ($mail_type == MT_NORMAL_FORWARD) {
79                 $mtype[] = 4;
80                 $res1 = exec_query($sql, "SELECT `domain_name` FROM `domain` WHERE `domain_id`=?", array($domain_id));
81                 $tmp1 = $res1->FetchRow(0);
82                 $maildomain = $tmp1['domain_name'];
83             } else if ($mail_type == MT_ALIAS_MAIL) {
84                 $mtype[] = 2;
85                 $res1 = exec_query($sql, "SELECT `alias_name` FROM `domain_aliasses` WHERE `alias_id`=?", array($sub_id));
86                 $tmp1 = $res1->FetchRow(0);
87                 $maildomain = $tmp1['alias_name'];
88             } else if ($mail_type == MT_ALIAS_FORWARD) {
89                 $mtype[] = 5;
90                 $res1 = exec_query($sql, "SELECT `alias_name` FROM `domain_aliasses` WHERE `alias_id`=?", array($sub_id));
91                 $tmp1 = $res1->FetchRow();
92                 $maildomain = $tmp1['alias_name'];
93             } else if ($mail_type == MT_SUBDOM_MAIL) {
94                 $mtype[] = 3;
95                 $res1 = exec_query($sql, "SELECT `subdomain_name` FROM `subdomain` WHERE `subdomain_id`=?", array($sub_id));
96                  $tmp1 = $res1->FetchRow();
97                 $maildomain = $tmp1['subdomain_name'];
98                 $res1 = exec_query($sql, "SELECT `domain_name` FROM `domain` WHERE `domain_id`=?", array($domain_id));
99                 $tmp1 = $res1->FetchRow(0);
100                 $maildomain = $maildomain . "." . $tmp1['domain_name'];
101             } else if ($mail_type == MT_SUBDOM_FORWARD) {
102                  $mtype[] = 6;
103                 $res1 = exec_query($sql, "SELECT `subdomain_name` FROM `subdomain` WHERE `subdomain_id`=?", array($sub_id));
104                 $tmp1 = $res1->FetchRow();
105                 $maildomain = $tmp1['subdomain_name'];
106                 $res1 = exec_query($sql, "SELECT `domain_name` FROM `domain` WHERE `domain_id`=?", array($domain_id));
107                 $tmp1 = $res1->FetchRow(0);
108                 $maildomain = $maildomain . "." . $tmp1['domain_name'];
109             } else if ($mail_type == MT_ALSSUB_MAIL) {
110                 $mtype[] = 7;
111                 $res1 = exec_query($sql, "SELECT `subdomain_alias_name`, `alias_id` FROM `subdomain_alias` WHERE `subdomain_alias_id`=?", array($sub_id));
112                  $tmp1 = $res1->FetchRow();
113                 $maildomain = $tmp1['subdomain_alias_name'];
114                 $alias_id = $tmp1['alias_id'];
115                 $res1 = exec_query($sql, "SELECT `alias_name` FROM `domain_aliasses` WHERE `alias_id`=?", array($alias_id));
116                 $tmp1 = $res1->FetchRow(0);
117                 $maildomain = $maildomain . "." . $tmp1['alias_name'];
118             } else if ($mail_type == MT_ALSSUB_FORWARD) {
119                  $mtype[] = 8;
120                 $res1 = exec_query($sql, "SELECT `subdomain_alias_name`, `alias_id` FROM `subdomain_alias` WHERE `subdomain_alias_id`=?", array($sub_id));
121                  $tmp1 = $res1->FetchRow();
122                 $maildomain = $tmp1['subdomain_alias_name'];
123                 $alias_id = $tmp1['alias_id'];
124                 $res1 = exec_query($sql, "SELECT `alias_name` FROM `domain_aliasses` WHERE `alias_id`=?", array($alias_id));
125                 $tmp1 = $res1->FetchRow(0);
126                 $maildomain = $maildomain . "." . $tmp1['alias_name'];
127             }
128         }
129
130         if (isset($_POST['forward_list'])) {
131             $mail_forward = clean_input($_POST['forward_list']);
132         }
133         $mail_acc = decode_idna($mail_acc);
134         $maildomain = decode_idna($maildomain);
135         $tpl->assign(
136             array(
137                 'EMAIL_ACCOUNT'    => $mail_acc . "@" . $maildomain,
138                 'FORWARD_LIST'    => str_replace(',', "\n", $mail_forward),
139                 'MTYPE'            => implode(',', $mtype),
140                 'MAIL_TYPE'        => $mail_type_list,
141                 'MAIL_ID'        => $mail_id
142             )
143         );
144
145         if (($mail_forward !== '_no_') && (count($mtype) > 1)) {
146             $tpl->assign(
147                 array(
148                     'ACTION'                => 'update_pass,update_forward',
149                     'FORWARD_MAIL'            => '',
150                     'FORWARD_MAIL_CHECKED'    => 'checked="checked"',
151                     'FORWARD_LIST_DISABLED'    => 'false'
152                 )
153             );
154             $tpl->parse('NORMAL_MAIL', '.normal_mail');
155         } else if ($mail_forward === '_no_') {
156             $tpl->assign(
157                 array(
158                     'ACTION'                => 'update_pass',
159                     'FORWARD_MAIL'            => '',
160                     'FORWARD_MAIL_CHECKED'    => '',
161                     'FORWARD_LIST'            => '',
162                     'FORWARD_LIST_DISABLED'    => 'true'
163                 )
164             );
165             $tpl->parse('NORMAL_MAIL', '.normal_mail');
166         } else {
167             $tpl->assign(
168                 array(
169                     'ACTION'                => 'update_forward',
170                     'NORMAL_MAIL'            => '',
171                     'FORWARD_LIST_DISABLED'    => 'false'
172                 )
173             );
174             $tpl->parse('FORWARD_MAIL', '.forward_mail');
175         }
176     }
177 }
178
179 function update_email_pass($sql) {
180     if (!isset($_POST['uaction'])) {
181         return false;
182     }
183     if (preg_match('/update_pass/', $_POST['uaction']) == 0) {
184         return true;
185     }
186     if (preg_match('/update_forward/', $_POST['uaction']) == 1 || isset($_POST['mail_forward'])) {
187         // The user only wants to update the forward list, not the password
188         if ($_POST['pass'] === '' && $_POST['pass_rep'] === '') {
189             return true;
190         }
191     }
192
193     $pass = clean_input($_POST['pass']);
194     $pass_rep = clean_input($_POST['pass_rep']);
195     $mail_id = $_GET['id'];
196     $mail_account = clean_input($_POST['mail_account']);
197
198     if (trim($pass) === '' || trim($pass_rep) === '' || $mail_id === '' || !is_numeric($mail_id)) {
199         set_page_message(tr('Password data is missing!'));
200         return false;
201     } else if ($pass !== $pass_rep) {
202         set_page_message(tr('Entered passwords differ!'));
203         return false;
204     } else if (!chk_password($pass, 50, "/[`\xb4'\"\\\\\x01-\x1f\015\012|<>^$]/i")) { // Not permitted chars
205     if(Config::get('PASSWD_STRONG')){
206       set_page_message(sprintf(tr('The password must be at least %s long and contain letters and numbers to be valid.'), Config::get('PASSWD_CHARS')));
207     } else {
208       set_page_message(sprintf(tr('Password data is shorter than %s signs or includes not permitted signs!'), Config::get('PASSWD_CHARS')));
209     }
210         return false;
211     } else {
212         $pass=encrypt_db_password($pass);
213         $status = Config::get('ITEM_CHANGE_STATUS');
214         check_for_lock_file();
215         $query = "UPDATE `mail_users` SET `mail_pass` = ?, `status` = ? WHERE `mail_id` = ?";
216         $rs = exec_query($sql, $query, array($pass, $status, $mail_id));
217         write_log($_SESSION['user_logged'] . ": change mail account password: $mail_account");
218         return true;
219     }
220 }
221
222 function update_email_forward(&$tpl, &$sql) {
223     if (!isset($_POST['uaction'])) {
224         return false;
225     }
226     if (preg_match('/update_forward/', $_POST['uaction']) == 0 && !isset($_POST['mail_forward'])) {
227         return true;
228     }
229
230     $mail_account = $_POST['mail_account'];
231     $mail_id = $_GET['id'];
232     $forward_list = clean_input($_POST['forward_list']);
233     $mail_accs = array();
234
235     if (isset($_POST['mail_forward']) || $_POST['uaction'] == 'update_forward') {
236         $faray = preg_split ('/[\n\s,]+/', $forward_list);
237
238         foreach ($faray as $value) {
239             $value = trim($value);
240             if (!chk_email($value) && $value !== '') {
241                 /* ERR .. strange :) not email in this line - warrning */
242                 set_page_message(tr("Mail forward list error!"));
243                 return false;
244             } else if ($value === '') {
245                 set_page_message(tr("Mail forward list error!"));
246                 return false;
247             }
248             $mail_accs[] = $value;
249         }
250
251         $forward_list = implode(',', $mail_accs);
252
253         // Check if the mail type doesn't contain xxx_forward and append it
254         if (preg_match('/_forward/', $_POST['mail_type']) == 0) {
255             // Get mail account type and append the corresponding xxx_forward
256             if ($_POST['mail_type'] == MT_NORMAL_MAIL) {
257                 $mail_type = $_POST['mail_type'] . ',' . MT_NORMAL_FORWARD;
258             } else if ($_POST['mail_type'] == MT_ALIAS_MAIL) {
259                 $mail_type = $_POST['mail_type'] . ',' . MT_ALIAS_FORWARD;
260             } else if ($_POST['mail_type'] == MT_SUBDOM_MAIL) {
261                 $mail_type = $_POST['mail_type'] . ',' . MT_SUBDOM_FORWARD;
262             } else if ($_POST['mail_type'] == MT_ALSSUB_MAIL) {
263                 $mail_type = $_POST['mail_type'] . ',' . MT_ALSSUB_FORWARD;
264             }
265         } else {
266             // The mail type already contains xxx_forward, so we can use $_POST['mail_type']
267             $mail_type = $_POST['mail_type'];
268         }
269     } else {
270         $forward_list = '_no_';
271         // Check if mail type was a forward type and remove it
272         if (preg_match('/_forward/', $_POST['mail_type']) == 1) {
273             $mail_type = preg_replace('/,[a-z]+_forward$/', '', $_POST['mail_type']);
274         }
275     }
276
277     $status = Config::get('ITEM_CHANGE_STATUS');
278
279     check_for_lock_file();
280
281     $query = "UPDATE `mail_users` SET `mail_forward` = ?, `mail_type` = ?, `status` = ? WHERE `mail_id` = ?";
282
283     $rs = exec_query($sql, $query, array($forward_list, $mail_type, $status, $mail_id));
284
285     write_log($_SESSION['user_logged'] . ": change mail forward: $mail_account");
286     return true;
287 }
288
289 // end page functions.
290
291 $theme_color = Config::get('USER_INITIAL_THEME');
292
293 $tpl->assign(
294     array(
295         'TR_CLIENT_EDIT_EMAIL_PAGE_TITLE'    => tr('ispCP - Manage Mail and FTP / Edit mail account'),
296         'THEME_COLOR_PATH'                    => "../themes/$theme_color",
297         'THEME_CHARSET'                        => tr('encoding'),
298         'ISP_LOGO'                            => get_logo($_SESSION['user_id'])
299     )
300 );
301
302 // dynamic page data.
303
304 edit_mail_account($tpl, $sql);
305
306 if (update_email_pass($sql) && update_email_forward($tpl, $sql)) {
307     set_page_message(tr("Mail were updated successfully!"));
308     send_request();
309     header("Location: mail_accounts.php");
310     die();
311 }
312
313 // static page messages.
314
315 gen_client_mainmenu($tpl, Config::get('CLIENT_TEMPLATE_PATH') . '/main_menu_email_accounts.tpl');
316 gen_client_menu($tpl, Config::get('CLIENT_TEMPLATE_PATH') . '/menu_email_accounts.tpl');
317
318 gen_logged_from($tpl);
319
320 check_permissions($tpl);
321
322 $tpl->assign(
323     array(
324         'TR_EDIT_EMAIL_ACCOUNT'    => tr('Edit email account'),
325         'TR_SAVE'                => tr('Save'),
326         'TR_PASSWORD'            => tr('Password'),
327         'TR_PASSWORD_REPEAT'    => tr('Repeat password'),
328         'TR_FORWARD_MAIL'        => tr('Forward mail'),
329         'TR_FORWARD_TO'            => tr('Forward to'),
330         'TR_FWD_HELP'            => tr("Separate multiple email addresses with a line-break."),
331         'TR_EDIT'                => tr('Edit')
332     )
333 );
334
335 gen_page_message($tpl);
336 $tpl->parse('PAGE', 'page');
337 $tpl->prnt();
338
339 if (Config::get('DUMP_GUI_DEBUG'))
340     dump_gui_debug();
341
342 unset_messages();
343
344 ?>
Note: See TracBrowser for help on using the browser.