Marius Cramer
2015-04-15 3a11d23a2f32a1b9b2ec43429917c000017c5eff
commit | author | age
e22f1e 1 <?php
T 2 /*
89623d 3 Copyright (c) 2005 - 2009, Till Brehm, projektfarm Gmbh
e22f1e 4 All rights reserved.
T 5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 /******************************************
32 * Begin Form configuration
33 ******************************************/
34
ac3b1f 35 $tform_def_file = "form/mail_user.tform.php";
e22f1e 36
T 37 /******************************************
38 * End Form configuration
39 ******************************************/
40
7fe908 41 require_once '../../lib/config.inc.php';
MC 42 require_once '../../lib/app.inc.php';
e22f1e 43
910093 44 //* Check permissions for module
T 45 $app->auth->check_module_permissions('mail');
e22f1e 46
T 47 // Loading classes
48 $app->uses('tpl,tform,tform_actions');
49 $app->load('tform_actions');
50
51 class page_action extends tform_actions {
7fe908 52
MC 53
22e7f9 54     function onShowNew() {
T 55         global $app, $conf;
7fe908 56
22e7f9 57         // we will check only users, not admins
T 58         if($_SESSION["s"]["user"]["typ"] == 'user') {
3cebc3 59             if(!$app->tform->checkClientLimit('limit_mailbox')) {
T 60                 $app->error($app->tform->wordbook["limit_mailbox_txt"]);
61             }
62             if(!$app->tform->checkResellerLimit('limit_mailbox')) {
63                 $app->error('Reseller: '.$app->tform->wordbook["limit_mailbox_txt"]);
22e7f9 64             }
T 65         }
7fe908 66
22e7f9 67         parent::onShowNew();
T 68     }
7fe908 69
e22f1e 70     function onShowEnd() {
T 71         global $app, $conf;
7fe908 72
e22f1e 73         $email = $this->dataRecord["email"];
7fe908 74         $email_parts = explode("@", $email);
MC 75         $app->tpl->setVar("email_local_part", $email_parts[0]);
d6363b 76         $email_parts[1] = $app->functions->idn_decode($email_parts[1]);
7fe908 77
e22f1e 78         // Getting Domains of the user
8cf78b 79         // $sql = "SELECT domain, server_id FROM mail_domain WHERE ".$app->tform->getAuthSQL('r').' ORDER BY domain';
T 80         $sql = "SELECT domain, server_id FROM mail_domain WHERE domain NOT IN (SELECT SUBSTR(source,2) FROM mail_forwarding WHERE type = 'aliasdomain') AND ".$app->tform->getAuthSQL('r')." ORDER BY domain";
e22f1e 81         $domains = $app->db->queryAllRecords($sql);
T 82         $domain_select = '';
ac3b1f 83         if(is_array($domains)) {
T 84             foreach( $domains as $domain) {
7fe908 85                 $domain['domain'] = $app->functions->idn_decode($domain['domain']);
b5a23a 86                 $selected = ($domain["domain"] == @$email_parts[1])?'SELECTED':'';
ac3b1f 87                 $domain_select .= "<option value='$domain[domain]' $selected>$domain[domain]</option>\r\n";
T 88             }
e22f1e 89         }
7fe908 90         $app->tpl->setVar("email_domain", $domain_select);
daff5c 91         unset($domains);
T 92         unset($domain_select);
7fe908 93
daff5c 94         // Get the spamfilter policys for the user
2af58c 95         $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]);
ed1d29 96         $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r') . " ORDER BY policy_name";
daff5c 97         $policys = $app->db->queryAllRecords($sql);
89623d 98         $policy_select = "<option value='0'>".$app->tform->lng("no_policy")."</option>";
daff5c 99         if(is_array($policys)) {
T 100             foreach( $policys as $p) {
101                 $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':'';
102                 $policy_select .= "<option value='$p[id]' $selected>$p[policy_name]</option>\r\n";
103             }
104         }
7fe908 105         $app->tpl->setVar("policy", $policy_select);
daff5c 106         unset($policys);
T 107         unset($policy_select);
108         unset($tmp_user);
7fe908 109
ac3b1f 110         // Convert quota from Bytes to MB
7fe908 111         if($this->dataRecord["quota"] != -1) $app->tpl->setVar("quota", $this->dataRecord["quota"] / 1024 / 1024);
MC 112
75ae20 113         // Is autoresponder set?
a8b07f 114         if (!empty($this->dataRecord['autoresponder']) && $this->dataRecord['autoresponder'] == 'y') {
75ae20 115             $app->tpl->setVar("ar_active", 'checked="checked"');
W 116         } else {
117             $app->tpl->setVar("ar_active", '');
d4c760 118         }
7fe908 119
10b4c8 120         if($this->dataRecord['autoresponder_subject'] == '') {
T 121             $app->tpl->setVar('autoresponder_subject', $app->tform->lng('autoresponder_subject'));
122         } else {
123             $app->tpl->setVar('autoresponder_subject', $this->dataRecord['autoresponder_subject']);
124         }
7fe908 125
MC 126         $app->uses('getconf');
127         $mail_config = $app->getconf->get_global_config('mail');
d4c760 128         if($mail_config["enable_custom_login"] == "y") {
7fe908 129             $app->tpl->setVar("enable_custom_login", 1);
d4c760 130         } else {
7fe908 131             $app->tpl->setVar("enable_custom_login", 0);
75ae20 132         }
7fe908 133
e22f1e 134         parent::onShowEnd();
T 135     }
7fe908 136
e22f1e 137     function onSubmit() {
T 138         global $app, $conf;
7fe908 139
89623d 140         //* Check if Domain belongs to user
b658fe 141         if(isset($_POST["email_domain"])) {
2af58c 142             $domain = $app->db->queryOneRecord("SELECT server_id, domain FROM mail_domain WHERE domain = ? AND ".$app->tform->getAuthSQL('r'), $app->functions->idn_encode($_POST["email_domain"]));
d6363b 143             if($domain["domain"] != $app->functions->idn_encode($_POST["email_domain"])) $app->tform->errorMessage .= $app->tform->lng("no_domain_perm");
b658fe 144         }
7fe908 145
MC 146
89623d 147         //* if its an insert, check that the password is not empty
965795 148         if($this->id == 0 && $_POST["password"] == '') {
89623d 149             $app->tform->errorMessage .= $app->tform->lng("error_no_pwd")."<br>";
965795 150         }
7fe908 151
89623d 152         //* Check the client limits, if user is not the admin
965795 153         if($_SESSION["s"]["user"]["typ"] != 'admin') { // if user is not admin
T 154             // Get the limits of the client
604c0c 155             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cc7a82 156             $client = $app->db->queryOneRecord("SELECT limit_mailbox, limit_mailquota, parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
7fe908 157
965795 158
22e7f9 159             // Check if the user may add another mailbox.
965795 160             if($this->id == 0 && $client["limit_mailbox"] >= 0) {
cc7a82 161                 $tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE sys_groupid = ?", $client_group_id);
22e7f9 162                 if($tmp["number"] >= $client["limit_mailbox"]) {
89623d 163                     $app->tform->errorMessage .= $app->tform->lng("limit_mailbox_txt")."<br>";
22e7f9 164                 }
T 165                 unset($tmp);
166             }
7fe908 167
965795 168             // Check the quota and adjust
ede489 169             if(isset($_POST["quota"]) && $client["limit_mailquota"] >= 0 && (($app->functions->intval($this->dataRecord["quota"]) * 1024 * 1024 != $this->oldDataRecord['quota']) || ($_POST["quota"] <= 0))) {
2af58c 170                 $tmp = $app->db->queryOneRecord("SELECT sum(quota) as mailquota FROM mail_user WHERE mailuser_id != ? AND ".$app->tform->getAuthSQL('u'), $this->id);
ba747c 171                 $mailquota = $tmp["mailquota"] / 1024 / 1024;
65ea2e 172                 $new_mailbox_quota = $app->functions->intval($this->dataRecord["quota"]);
27b8b8 173                 if(($mailquota + $new_mailbox_quota > $client["limit_mailquota"]) || ($new_mailbox_quota == 0 && $client["limit_mailquota"] != -1)) {
965795 174                     $max_free_quota = $client["limit_mailquota"] - $mailquota;
89623d 175                     $app->tform->errorMessage .= $app->tform->lng("limit_mailquota_txt").": ".$max_free_quota."<br>";
965795 176                     // Set the quota field to the max free space
T 177                     $this->dataRecord["quota"] = $max_free_quota;
178                 }
179                 unset($tmp);
180                 unset($tmp_quota);
22e7f9 181             }
009b2d 182
SC 183             if($client['parent_client_id'] > 0) {
184                 // Get the limits of the reseller
2af58c 185                 $reseller = $app->db->queryOneRecord("SELECT limit_mailquota, limit_maildomain FROM client WHERE client_id = ?", $client['parent_client_id']);
009b2d 186
SC 187                 //* Check the website quota of the client
188                 if(isset($_POST["quota"]) && $reseller["limit_mailquota"] >= 0 && $app->functions->intval($this->dataRecord["quota"]) * 1024 * 1024 != $this->oldDataRecord['quota']) {
2af58c 189                     $tmp = $app->db->queryOneRecord("SELECT sum(quota) as mailquota FROM mail_user, sys_group, client WHERE mail_user.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ? IN (client.parent_client_id, client.client_id) AND mailuser_id != ?", $client['parent_client_id'], $this->id);
009b2d 190
SC 191                     $mailquota = $tmp["mailquota"] / 1024 / 1024;
192                     $new_mailbox_quota = $app->functions->intval($this->dataRecord["quota"]);
193                     if(($mailquota + $new_mailbox_quota > $reseller["limit_mailquota"]) || ($new_mailbox_quota == 0 && $reseller["limit_mailquota"] != -1)) {
194                         $max_free_quota = $reseller["limit_mailquota"] - $mailquota;
195                         if($max_free_quota < 0) $max_free_quota = 0;
196                         $app->tform->errorMessage .= $app->tform->lng("limit_mailquota_txt").": ".$max_free_quota."<br>";
197                         // Set the quota field to the max free space
198                         $this->dataRecord["quota"] = $max_free_quota;
199                     }
200                     unset($tmp);
201                     unset($tmp_quota);
202                 }
203             }
965795 204         } // end if user is not admin
22e7f9 205
7fe908 206
MC 207         $app->uses('getconf');
208         $mail_config = $app->getconf->get_server_config(!empty($domain["server_id"]) ? $domain["server_id"] : '', 'mail');
209
89623d 210         //* compose the email field
b658fe 211         if(isset($_POST["email_local_part"]) && isset($_POST["email_domain"])) {
d6363b 212             $this->dataRecord["email"] = strtolower($_POST["email_local_part"]."@".$app->functions->idn_encode($_POST["email_domain"]));
7fe908 213
b658fe 214             // Set the server id of the mailbox = server ID of mail domain.
T 215             $this->dataRecord["server_id"] = $domain["server_id"];
7fe908 216
b658fe 217             unset($this->dataRecord["email_local_part"]);
T 218             unset($this->dataRecord["email_domain"]);
7fe908 219
b658fe 220             // Convert quota from MB to Bytes
4bf118 221             if($this->dataRecord["quota"] != -1) $this->dataRecord["quota"] = $this->dataRecord["quota"] * 1024 * 1024;
7fe908 222
b658fe 223             // setting Maildir, Homedir, UID and GID
7fe908 224             $maildir = str_replace("[domain]", $domain["domain"], $mail_config["maildir_path"]);
MC 225             $maildir = str_replace("[localpart]", strtolower($_POST["email_local_part"]), $maildir);
b658fe 226             $this->dataRecord["maildir"] = $maildir;
T 227             $this->dataRecord["homedir"] = $mail_config["homedir_path"];
0e2978 228             
DM 229             // Will be overwritten by mail_plugin
9ea57c 230             $this->dataRecord['uid'] = -1;
DM 231             $this->dataRecord['gid'] = -1;
0e2978 232                 
89623d 233             //* Check if there is no alias or forward with this address
2af58c 234             $tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE active = 'y' AND source = ?", $this->dataRecord["email"]);
89623d 235             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("duplicate_alias_or_forward_txt")."<br>";
T 236             unset($tmp);
7fe908 237
b658fe 238         }
7fe908 239
MC 240         $sys_config = $app->getconf->get_global_config('mail');
241         if($sys_config["enable_custom_login"] == "y") {
242             if(!isset($_POST["login"]) || $_POST["login"] == '') $this->dataRecord["login"] = $this->dataRecord["email"];
243             elseif(strpos($_POST["login"], '@') !== false && $_POST["login"] != $this->dataRecord["email"]) $app->tform->errorMessage .= $app->tform->lng("error_login_email_txt")."<br>";
d4c760 244         } else {
7fe908 245             $this->dataRecord["login"] = isset($this->dataRecord["email"]) ? $this->dataRecord["email"] : '';
d4c760 246         }
75ae20 247         //* if autoresponder checkbox not selected, do not save dates
W 248         if (!isset($_POST['autoresponder']) && array_key_exists('autoresponder_start_date', $_POST)) {
7fe908 249             $this->dataRecord['autoresponder_start_date'] = array_map(create_function('$item', 'return 0;'), $this->dataRecord['autoresponder_start_date']);
MC 250             $this->dataRecord['autoresponder_end_date'] = array_map(create_function('$item', 'return 0;'), $this->dataRecord['autoresponder_end_date']);
75ae20 251         }
7fe908 252
e22f1e 253         parent::onSubmit();
T 254     }
7fe908 255
22e7f9 256     function onAfterInsert() {
T 257         global $app, $conf;
7fe908 258
22e7f9 259         // Set the domain owner as mailbox owner
2af58c 260         $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = ? AND ".$app->tform->getAuthSQL('r'), $app->functions->idn_encode($_POST["email_domain"]));
MC 261         $app->db->query("UPDATE mail_user SET sys_groupid = ? WHERE mailuser_id = ?", $domain["sys_groupid"], $this->id);
7fe908 262
daff5c 263         // Spamfilter policy
65ea2e 264         $policy_id = $app->functions->intval($this->dataRecord["policy"]);
daff5c 265         if($policy_id > 0) {
2af58c 266             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]);
daff5c 267             if($tmp_user["id"] > 0) {
T 268                 // There is already a record that we will update
3a11d2 269                 $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
daff5c 270             } else {
T 271                 // We create a new record
3a11d2 272                 $insert_data = array(
MC 273                     "sys_userid" => $_SESSION["s"]["user"]["userid"],
274                     "sys_groupid" => $domain["sys_groupid"],
275                     "sys_perm_user" => 'riud',
276                     "sys_perm_group" => 'riud',
277                     "sys_perm_other" => '',
278                     "server_id" => $domain["server_id"],
279                     "priority" => 10,
280                     "policy_id" => $policy_id,
281                     "email" => $this->dataRecord["email"],
282                     "fullname" => $this->dataRecord["email"],
283                     "local" => 'Y'
284                 );
f66929 285                 $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
daff5c 286             }
T 287         }  // endif spamfilter policy
7fe908 288
MC 289
6cc49f 290         // Set the fields for dovecot
T 291         if(isset($this->dataRecord["email"])) {
292             $disableimap = ($this->dataRecord["disableimap"])?'y':'n';
293             $disablepop3 = ($this->dataRecord["disablepop3"])?'y':'n';
294             $disabledeliver = ($this->dataRecord["postfix"] == 'y')?'n':'y';
44c2dd 295             $disablesmtp = ($this->dataRecord["disablesmtp"])?'y':'n';
7fe908 296
2af58c 297             $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disabledoveadm = ? WHERE mailuser_id = ?";
MC 298             $app->db->query($sql, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disableimap, $this->id);
6cc49f 299         }
22e7f9 300     }
7fe908 301
22e7f9 302     function onAfterUpdate() {
T 303         global $app, $conf;
7fe908 304
22e7f9 305         // Set the domain owner as mailbox owner
b658fe 306         if(isset($_POST["email_domain"])) {
2af58c 307             $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = ? AND ".$app->tform->getAuthSQL('r'), $app->functions->idn_encode($_POST["email_domain"]));
MC 308             $app->db->query("UPDATE mail_user SET sys_groupid = ? WHERE mailuser_id = ?", $domain["sys_groupid"], $this->id);
7fe908 309
b658fe 310             // Spamfilter policy
65ea2e 311             $policy_id = $app->functions->intval($this->dataRecord["policy"]);
2af58c 312             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]);
b658fe 313             if($policy_id > 0) {
T 314                 if($tmp_user["id"] > 0) {
315                     // There is already a record that we will update
3a11d2 316                     $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
b658fe 317                 } else {
T 318                     // We create a new record
3a11d2 319                     $insert_data = array(
MC 320                         "sys_userid" => $_SESSION["s"]["user"]["userid"],
321                         "sys_groupid" => $domain["sys_groupid"],
322                         "sys_perm_user" => 'riud',
323                         "sys_perm_group" => 'riud',
324                         "sys_perm_other" => '',
325                         "server_id" => $domain["server_id"],
326                         "priority" => 10,
327                         "policy_id" => $policy_id,
328                         "email" => $this->dataRecord["email"],
329                         "fullname" => $this->dataRecord["email"],
330                         "local" => 'Y'
331                     );
f66929 332                     $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
b658fe 333                 }
T 334             }else {
335                 if($tmp_user["id"] > 0) {
336                     // There is already a record but the user shall have no policy, so we delete it
f66929 337                     $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
b658fe 338                 }
T 339             } // endif spamfilter policy
340         }
7fe908 341
6cc49f 342         // Set the fields for dovecot
T 343         if(isset($this->dataRecord["email"])) {
a8b07f 344             $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n';
X 345             $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n';
6cc49f 346             $disabledeliver = ($this->dataRecord["postfix"] == 'y')?'n':'y';
44c2dd 347             $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n';
7fe908 348
2af58c 349             $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, `disablesieve-filter` = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disabledoveadm = ? WHERE mailuser_id = ?";
MC 350             $app->db->query($sql, $disableimap, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disableimap, $this->id);
6cc49f 351         }
7fe908 352
95ed0d 353         //** If the email address has been changed, change it in all aliases too
98302d 354         if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) {
7fe908 355             //if($this->oldDataRecord['email'] != $this->dataRecord['email']) {
MC 356
95ed0d 357             //* Update the aliases
2af58c 358             $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = ?", $this->oldDataRecord['email']);
95ed0d 359             if(is_array($forwardings)) {
T 360                 foreach($forwardings as $rec) {
3a11d2 361                     $destination = $this->dataRecord['email'];
MC 362                     $app->db->datalogUpdate('mail_forwarding', array("destination" => $destination), 'forwarding_id', $rec['forwarding_id']);
95ed0d 363                 }
T 364             }
7fe908 365
95ed0d 366         } // end if email addess changed
7fe908 367
f17718 368         //* Change backup options when user mail backup options have been changed
FS 369         if(isset($this->dataRecord['backup_interval']) && ($this->dataRecord['backup_interval'] != $this->oldDataRecord['backup_interval'] || $this->dataRecord['backup_copies'] != $this->oldDataRecord['backup_copies'])) {
3a11d2 370             $backup_interval = $this->dataRecord['backup_interval'];
d9443d 371             $backup_copies = $app->functions->intval($this->dataRecord['backup_copies']);
3a11d2 372             $app->db->datalogUpdate('mail_user', array("backup_interval" => $backup_interval, "backup_copies" => $backup_copies), 'mailuser_id', $rec['mailuser_id']);
f17718 373             unset($backup_copies);
FS 374             unset($backup_interval);
375         } // end if backup options changed
376
22e7f9 377     }
7fe908 378
e22f1e 379 }
T 380
381 $app->tform_actions = new page_action;
382 $app->tform_actions->onLoad();
383
690cfe 384 ?>