Marius Cramer
2014-08-13 42539643c396f9d8865dcf9a51b13dc869709d16
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
604c0c 95         $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = '".$app->db->quote($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"])) {
d6363b 142             $domain = $app->db->queryOneRecord("SELECT server_id, domain FROM mail_domain WHERE domain = '".$app->db->quote($app->functions->idn_encode($_POST["email_domain"]))."' AND ".$app->tform->getAuthSQL('r'));
M 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"]);
965795 156             $client = $app->db->queryOneRecord("SELECT limit_mailbox, limit_mailquota 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) {
22e7f9 161                 $tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE sys_groupid = $client_group_id");
T 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))) {
65ea2e 170                 $tmp = $app->db->queryOneRecord("SELECT sum(quota) as mailquota FROM mail_user WHERE mailuser_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
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             }
ede489 182             
965795 183         } // end if user is not admin
22e7f9 184
7fe908 185
MC 186         $app->uses('getconf');
187         $mail_config = $app->getconf->get_server_config(!empty($domain["server_id"]) ? $domain["server_id"] : '', 'mail');
188
89623d 189         //* compose the email field
b658fe 190         if(isset($_POST["email_local_part"]) && isset($_POST["email_domain"])) {
d6363b 191             $this->dataRecord["email"] = strtolower($_POST["email_local_part"]."@".$app->functions->idn_encode($_POST["email_domain"]));
7fe908 192
b658fe 193             // Set the server id of the mailbox = server ID of mail domain.
T 194             $this->dataRecord["server_id"] = $domain["server_id"];
7fe908 195
b658fe 196             unset($this->dataRecord["email_local_part"]);
T 197             unset($this->dataRecord["email_domain"]);
7fe908 198
b658fe 199             // Convert quota from MB to Bytes
4bf118 200             if($this->dataRecord["quota"] != -1) $this->dataRecord["quota"] = $this->dataRecord["quota"] * 1024 * 1024;
7fe908 201
b658fe 202             // setting Maildir, Homedir, UID and GID
7fe908 203             $maildir = str_replace("[domain]", $domain["domain"], $mail_config["maildir_path"]);
MC 204             $maildir = str_replace("[localpart]", strtolower($_POST["email_local_part"]), $maildir);
b658fe 205             $this->dataRecord["maildir"] = $maildir;
T 206             $this->dataRecord["homedir"] = $mail_config["homedir_path"];
207             $this->dataRecord["uid"] = $mail_config["mailuser_uid"];
208             $this->dataRecord["gid"] = $mail_config["mailuser_gid"];
7fe908 209
89623d 210             //* Check if there is no alias or forward with this address
7acef0 211             $tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE active = 'y' AND source = '".$app->db->quote($this->dataRecord["email"])."'");
89623d 212             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("duplicate_alias_or_forward_txt")."<br>";
T 213             unset($tmp);
7fe908 214
b658fe 215         }
7fe908 216
MC 217         $sys_config = $app->getconf->get_global_config('mail');
218         if($sys_config["enable_custom_login"] == "y") {
219             if(!isset($_POST["login"]) || $_POST["login"] == '') $this->dataRecord["login"] = $this->dataRecord["email"];
220             elseif(strpos($_POST["login"], '@') !== false && $_POST["login"] != $this->dataRecord["email"]) $app->tform->errorMessage .= $app->tform->lng("error_login_email_txt")."<br>";
d4c760 221         } else {
7fe908 222             $this->dataRecord["login"] = isset($this->dataRecord["email"]) ? $this->dataRecord["email"] : '';
d4c760 223         }
75ae20 224         //* if autoresponder checkbox not selected, do not save dates
W 225         if (!isset($_POST['autoresponder']) && array_key_exists('autoresponder_start_date', $_POST)) {
7fe908 226             $this->dataRecord['autoresponder_start_date'] = array_map(create_function('$item', 'return 0;'), $this->dataRecord['autoresponder_start_date']);
MC 227             $this->dataRecord['autoresponder_end_date'] = array_map(create_function('$item', 'return 0;'), $this->dataRecord['autoresponder_end_date']);
75ae20 228         }
7fe908 229
e22f1e 230         parent::onSubmit();
T 231     }
7fe908 232
22e7f9 233     function onAfterInsert() {
T 234         global $app, $conf;
7fe908 235
22e7f9 236         // Set the domain owner as mailbox owner
d6363b 237         $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = '".$app->db->quote($app->functions->idn_encode($_POST["email_domain"]))."' AND ".$app->tform->getAuthSQL('r'));
604c0c 238         $app->db->query("UPDATE mail_user SET sys_groupid = ".$app->functions->intval($domain["sys_groupid"])." WHERE mailuser_id = ".$this->id);
7fe908 239
daff5c 240         // Spamfilter policy
65ea2e 241         $policy_id = $app->functions->intval($this->dataRecord["policy"]);
daff5c 242         if($policy_id > 0) {
5a43e7 243             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '".$app->db->quote($this->dataRecord["email"])."'");
daff5c 244             if($tmp_user["id"] > 0) {
T 245                 // There is already a record that we will update
04620b 246                 $app->db->datalogUpdate('spamfilter_users', "policy_id = $policy_id", 'id', $tmp_user["id"]);
daff5c 247             } else {
T 248                 // We create a new record
7fe908 249                 $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
604c0c 250                         VALUES (".$app->functions->intval($_SESSION["s"]["user"]["userid"]).", ".$app->functions->intval($domain["sys_groupid"]).", 'riud', 'riud', '', ".$app->functions->intval($domain["server_id"]).", 10, ".$app->functions->intval($policy_id).", '".$app->db->quote($this->dataRecord["email"])."', '".$app->db->quote($this->dataRecord["email"])."', 'Y')";
f66929 251                 $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
daff5c 252             }
T 253         }  // endif spamfilter policy
7fe908 254
MC 255
6cc49f 256         // Set the fields for dovecot
T 257         if(isset($this->dataRecord["email"])) {
258             $disableimap = ($this->dataRecord["disableimap"])?'y':'n';
259             $disablepop3 = ($this->dataRecord["disablepop3"])?'y':'n';
260             $disabledeliver = ($this->dataRecord["postfix"] == 'y')?'n':'y';
44c2dd 261             $disablesmtp = ($this->dataRecord["disablesmtp"])?'y':'n';
7fe908 262
cea1e5 263             $sql = "UPDATE mail_user SET disableimap = '$disableimap', disablesieve = '$disableimap', disablepop3 = '$disablepop3', disablesmtp = '$disablesmtp', disabledeliver = '$disabledeliver', disablelda = '$disabledeliver', disabledoveadm = '$disableimap' WHERE mailuser_id = ".$this->id;
6cc49f 264             $app->db->query($sql);
T 265         }
22e7f9 266     }
7fe908 267
22e7f9 268     function onAfterUpdate() {
T 269         global $app, $conf;
7fe908 270
22e7f9 271         // Set the domain owner as mailbox owner
b658fe 272         if(isset($_POST["email_domain"])) {
d6363b 273             $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = '".$app->db->quote($app->functions->idn_encode($_POST["email_domain"]))."' AND ".$app->tform->getAuthSQL('r'));
604c0c 274             $app->db->query("UPDATE mail_user SET sys_groupid = ".$app->functions->intval($domain["sys_groupid"])." WHERE mailuser_id = ".$this->id);
7fe908 275
b658fe 276             // Spamfilter policy
65ea2e 277             $policy_id = $app->functions->intval($this->dataRecord["policy"]);
5a43e7 278             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '".$app->db->quote($this->dataRecord["email"])."'");
b658fe 279             if($policy_id > 0) {
T 280                 if($tmp_user["id"] > 0) {
281                     // There is already a record that we will update
e1bf06 282                     $app->db->datalogUpdate('spamfilter_users', "policy_id = $policy_id", 'id', $tmp_user["id"]);
b658fe 283                 } else {
T 284                     // We create a new record
7fe908 285                     $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
604c0c 286                             VALUES (".$app->functions->intval($_SESSION["s"]["user"]["userid"]).", ".$app->functions->intval($domain["sys_groupid"]).", 'riud', 'riud', '', ".$app->functions->intval($domain["server_id"]).", 10, ".$app->functions->intval($policy_id).", '".$app->db->quote($this->dataRecord["email"])."', '".$app->db->quote($this->dataRecord["email"])."', 'Y')";
f66929 287                     $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
b658fe 288                 }
T 289             }else {
290                 if($tmp_user["id"] > 0) {
291                     // There is already a record but the user shall have no policy, so we delete it
f66929 292                     $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
b658fe 293                 }
T 294             } // endif spamfilter policy
295         }
7fe908 296
6cc49f 297         // Set the fields for dovecot
T 298         if(isset($this->dataRecord["email"])) {
a8b07f 299             $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n';
X 300             $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n';
6cc49f 301             $disabledeliver = ($this->dataRecord["postfix"] == 'y')?'n':'y';
44c2dd 302             $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n';
7fe908 303
5fbd57 304             $sql = "UPDATE mail_user SET disableimap = '$disableimap', disablesieve = '$disableimap', `disablesieve-filter` = '$disableimap', disablepop3 = '$disablepop3', disablesmtp = '$disablesmtp', disabledeliver = '$disabledeliver', disablelda = '$disabledeliver', disabledoveadm = '$disableimap' WHERE mailuser_id = ".$this->id;
6cc49f 305             $app->db->query($sql);
T 306         }
7fe908 307
95ed0d 308         //** If the email address has been changed, change it in all aliases too
98302d 309         if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) {
7fe908 310             //if($this->oldDataRecord['email'] != $this->dataRecord['email']) {
MC 311
95ed0d 312             //* Update the aliases
T 313             $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = '".$app->db->quote($this->oldDataRecord['email'])."'");
314             if(is_array($forwardings)) {
315                 foreach($forwardings as $rec) {
316                     $destination = $app->db->quote($this->dataRecord['email']);
317                     $app->db->datalogUpdate('mail_forwarding', "destination = '$destination'", 'forwarding_id', $rec['forwarding_id']);
318                 }
319             }
7fe908 320
95ed0d 321         } // end if email addess changed
7fe908 322
22e7f9 323     }
7fe908 324
e22f1e 325 }
T 326
327 $app->tform_actions = new page_action;
328 $app->tform_actions->onLoad();
329
690cfe 330 ?>