Marius Burkard
2016-05-04 c3189ce6c7301c3ec17878fd3918f31d0d3cb18a
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
f339eb 210         // Set Maildir format
D 211         if ($this->id == 0) {
ef6e5d 212             $this->dataRecord['maildir_format'] = $mail_config['maildir_format'];
f339eb 213         }
D 214         else {
215             // restore Maildir format
216             $tmp = $app->db->queryOneRecord("SELECT maildir_format FROM mail_user WHERE mailuser_id = ".$app->functions->intval($this->id));
217             $this->dataRecord['maildir_format'] = $tmp['maildir_format'];
218         }
219         
89623d 220         //* compose the email field
b658fe 221         if(isset($_POST["email_local_part"]) && isset($_POST["email_domain"])) {
d6363b 222             $this->dataRecord["email"] = strtolower($_POST["email_local_part"]."@".$app->functions->idn_encode($_POST["email_domain"]));
7fe908 223
b658fe 224             // Set the server id of the mailbox = server ID of mail domain.
T 225             $this->dataRecord["server_id"] = $domain["server_id"];
7fe908 226
b658fe 227             unset($this->dataRecord["email_local_part"]);
T 228             unset($this->dataRecord["email_domain"]);
7fe908 229
b658fe 230             // Convert quota from MB to Bytes
4bf118 231             if($this->dataRecord["quota"] != -1) $this->dataRecord["quota"] = $this->dataRecord["quota"] * 1024 * 1024;
7fe908 232
b658fe 233             // setting Maildir, Homedir, UID and GID
7fe908 234             $maildir = str_replace("[domain]", $domain["domain"], $mail_config["maildir_path"]);
MC 235             $maildir = str_replace("[localpart]", strtolower($_POST["email_local_part"]), $maildir);
b658fe 236             $this->dataRecord["maildir"] = $maildir;
T 237             $this->dataRecord["homedir"] = $mail_config["homedir_path"];
0e2978 238             
DM 239             // Will be overwritten by mail_plugin
9ea57c 240             $this->dataRecord['uid'] = -1;
DM 241             $this->dataRecord['gid'] = -1;
0e2978 242                 
89623d 243             //* Check if there is no alias or forward with this address
2af58c 244             $tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE active = 'y' AND source = ?", $this->dataRecord["email"]);
89623d 245             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("duplicate_alias_or_forward_txt")."<br>";
T 246             unset($tmp);
7fe908 247
b658fe 248         }
7fe908 249
MC 250         $sys_config = $app->getconf->get_global_config('mail');
251         if($sys_config["enable_custom_login"] == "y") {
252             if(!isset($_POST["login"]) || $_POST["login"] == '') $this->dataRecord["login"] = $this->dataRecord["email"];
253             elseif(strpos($_POST["login"], '@') !== false && $_POST["login"] != $this->dataRecord["email"]) $app->tform->errorMessage .= $app->tform->lng("error_login_email_txt")."<br>";
d4c760 254         } else {
7fe908 255             $this->dataRecord["login"] = isset($this->dataRecord["email"]) ? $this->dataRecord["email"] : '';
d4c760 256         }
75ae20 257         //* if autoresponder checkbox not selected, do not save dates
96ae77 258         if (!isset($_POST['autoresponder'])) {
TB 259             $this->dataRecord['autoresponder_start_date'] = '';
260             $this->dataRecord['autoresponder_end_date'] = '';
75ae20 261         }
7fe908 262
e22f1e 263         parent::onSubmit();
T 264     }
7fe908 265
22e7f9 266     function onAfterInsert() {
T 267         global $app, $conf;
7fe908 268
22e7f9 269         // Set the domain owner as mailbox owner
2af58c 270         $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 271         $app->db->query("UPDATE mail_user SET sys_groupid = ? WHERE mailuser_id = ?", $domain["sys_groupid"], $this->id);
7fe908 272
daff5c 273         // Spamfilter policy
65ea2e 274         $policy_id = $app->functions->intval($this->dataRecord["policy"]);
daff5c 275         if($policy_id > 0) {
2af58c 276             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]);
daff5c 277             if($tmp_user["id"] > 0) {
T 278                 // There is already a record that we will update
3a11d2 279                 $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
daff5c 280             } else {
T 281                 // We create a new record
3a11d2 282                 $insert_data = array(
MC 283                     "sys_userid" => $_SESSION["s"]["user"]["userid"],
284                     "sys_groupid" => $domain["sys_groupid"],
285                     "sys_perm_user" => 'riud',
286                     "sys_perm_group" => 'riud',
287                     "sys_perm_other" => '',
288                     "server_id" => $domain["server_id"],
289                     "priority" => 10,
290                     "policy_id" => $policy_id,
291                     "email" => $this->dataRecord["email"],
292                     "fullname" => $this->dataRecord["email"],
293                     "local" => 'Y'
294                 );
f66929 295                 $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
daff5c 296             }
T 297         }  // endif spamfilter policy
7fe908 298
MC 299
6cc49f 300         // Set the fields for dovecot
T 301         if(isset($this->dataRecord["email"])) {
302             $disableimap = ($this->dataRecord["disableimap"])?'y':'n';
303             $disablepop3 = ($this->dataRecord["disablepop3"])?'y':'n';
304             $disabledeliver = ($this->dataRecord["postfix"] == 'y')?'n':'y';
44c2dd 305             $disablesmtp = ($this->dataRecord["disablesmtp"])?'y':'n';
7fe908 306
2af58c 307             $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disabledoveadm = ? WHERE mailuser_id = ?";
MC 308             $app->db->query($sql, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disableimap, $this->id);
6cc49f 309         }
22e7f9 310     }
7fe908 311
22e7f9 312     function onAfterUpdate() {
T 313         global $app, $conf;
7fe908 314
22e7f9 315         // Set the domain owner as mailbox owner
b658fe 316         if(isset($_POST["email_domain"])) {
2af58c 317             $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 318             $app->db->query("UPDATE mail_user SET sys_groupid = ? WHERE mailuser_id = ?", $domain["sys_groupid"], $this->id);
7fe908 319
b658fe 320             // Spamfilter policy
65ea2e 321             $policy_id = $app->functions->intval($this->dataRecord["policy"]);
2af58c 322             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]);
b658fe 323             if($policy_id > 0) {
T 324                 if($tmp_user["id"] > 0) {
325                     // There is already a record that we will update
3a11d2 326                     $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
b658fe 327                 } else {
T 328                     // We create a new record
3a11d2 329                     $insert_data = array(
MC 330                         "sys_userid" => $_SESSION["s"]["user"]["userid"],
331                         "sys_groupid" => $domain["sys_groupid"],
332                         "sys_perm_user" => 'riud',
333                         "sys_perm_group" => 'riud',
334                         "sys_perm_other" => '',
335                         "server_id" => $domain["server_id"],
336                         "priority" => 10,
337                         "policy_id" => $policy_id,
338                         "email" => $this->dataRecord["email"],
339                         "fullname" => $this->dataRecord["email"],
340                         "local" => 'Y'
341                     );
f66929 342                     $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
b658fe 343                 }
T 344             }else {
345                 if($tmp_user["id"] > 0) {
346                     // There is already a record but the user shall have no policy, so we delete it
f66929 347                     $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
b658fe 348                 }
T 349             } // endif spamfilter policy
350         }
7fe908 351
6cc49f 352         // Set the fields for dovecot
T 353         if(isset($this->dataRecord["email"])) {
a8b07f 354             $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n';
X 355             $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n';
6cc49f 356             $disabledeliver = ($this->dataRecord["postfix"] == 'y')?'n':'y';
44c2dd 357             $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n';
7fe908 358
2af58c 359             $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, `disablesieve-filter` = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disabledoveadm = ? WHERE mailuser_id = ?";
MC 360             $app->db->query($sql, $disableimap, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disableimap, $this->id);
6cc49f 361         }
7fe908 362
95ed0d 363         //** If the email address has been changed, change it in all aliases too
98302d 364         if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) {
7fe908 365             //if($this->oldDataRecord['email'] != $this->dataRecord['email']) {
MC 366
95ed0d 367             //* Update the aliases
2af58c 368             $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = ?", $this->oldDataRecord['email']);
95ed0d 369             if(is_array($forwardings)) {
T 370                 foreach($forwardings as $rec) {
3a11d2 371                     $destination = $this->dataRecord['email'];
MC 372                     $app->db->datalogUpdate('mail_forwarding', array("destination" => $destination), 'forwarding_id', $rec['forwarding_id']);
95ed0d 373                 }
T 374             }
7fe908 375
95ed0d 376         } // end if email addess changed
7fe908 377
f17718 378         //* Change backup options when user mail backup options have been changed
FS 379         if(isset($this->dataRecord['backup_interval']) && ($this->dataRecord['backup_interval'] != $this->oldDataRecord['backup_interval'] || $this->dataRecord['backup_copies'] != $this->oldDataRecord['backup_copies'])) {
3a11d2 380             $backup_interval = $this->dataRecord['backup_interval'];
d9443d 381             $backup_copies = $app->functions->intval($this->dataRecord['backup_copies']);
3a11d2 382             $app->db->datalogUpdate('mail_user', array("backup_interval" => $backup_interval, "backup_copies" => $backup_copies), 'mailuser_id', $rec['mailuser_id']);
f17718 383             unset($backup_copies);
FS 384             unset($backup_interval);
385         } // end if backup options changed
386
22e7f9 387     }
7fe908 388
e22f1e 389 }
T 390
391 $app->tform_actions = new page_action;
392 $app->tform_actions->onLoad();
393
690cfe 394 ?>