tbrehm
2008-12-27 b96784d7c002ad125ceba27e2749c2b3713bec0d
commit | author | age
e22f1e 1 <?php
T 2 /*
3 Copyright (c) 2005, Till Brehm, projektfarm Gmbh
4 All rights reserved.
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
41 require_once('../../lib/config.inc.php');
42 require_once('../../lib/app.inc.php');
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 {
52     
22e7f9 53     
T 54     function onShowNew() {
55         global $app, $conf;
56         
57         // we will check only users, not admins
58         if($_SESSION["s"]["user"]["typ"] == 'user') {
59             
60             // Get the limits of the client
61             $client_group_id = $_SESSION["s"]["user"]["default_group"];
62             $client = $app->db->queryOneRecord("SELECT limit_mailbox FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
63             
64             // Check if the user may add another mailbox.
65             if($client["limit_mailbox"] >= 0) {
66                 $tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE sys_groupid = $client_group_id");
67                 if($tmp["number"] >= $client["limit_mailbox"]) {
68                     $app->error($app->tform->wordbook["limit_mailbox_txt"]);
69                 }
70             }
71         }
72         
73         parent::onShowNew();
74     }
75     
e22f1e 76     function onShowEnd() {
T 77         global $app, $conf;
78         
79         $email = $this->dataRecord["email"];
80         $email_parts = explode("@",$email);
81         $app->tpl->setVar("email_local_part",$email_parts[0]);
82         
83         // Getting Domains of the user
ac3b1f 84         $sql = "SELECT domain FROM mail_domain WHERE ".$app->tform->getAuthSQL('r');
e22f1e 85         $domains = $app->db->queryAllRecords($sql);
T 86         $domain_select = '';
ac3b1f 87         if(is_array($domains)) {
T 88             foreach( $domains as $domain) {
b5a23a 89                 $selected = ($domain["domain"] == @$email_parts[1])?'SELECTED':'';
ac3b1f 90                 $domain_select .= "<option value='$domain[domain]' $selected>$domain[domain]</option>\r\n";
T 91             }
e22f1e 92         }
T 93         $app->tpl->setVar("email_domain",$domain_select);
daff5c 94         unset($domains);
T 95         unset($domain_select);
96         
97         // Get the spamfilter policys for the user
98         $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = '".$this->dataRecord["email"]."'");
99         $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r');
100         $policys = $app->db->queryAllRecords($sql);
101         $policy_select = "<option value='0'>".$app->tform->wordbook["no_policy"]."</option>";
102         if(is_array($policys)) {
103             foreach( $policys as $p) {
104                 $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':'';
105                 $policy_select .= "<option value='$p[id]' $selected>$p[policy_name]</option>\r\n";
106             }
107         }
108         $app->tpl->setVar("policy",$policy_select);
109         unset($policys);
110         unset($policy_select);
111         unset($tmp_user);
ac3b1f 112         
T 113         // Convert quota from Bytes to MB
daff5c 114         $app->tpl->setVar("quota",$this->dataRecord["quota"] / 1024 / 1024);
ac3b1f 115         
e22f1e 116         parent::onShowEnd();
T 117     }
118     
119     function onSubmit() {
120         global $app, $conf;
121         
122         // Check if Domain belongs to user
b658fe 123         if(isset($_POST["email_domain"])) {
T 124             $domain = $app->db->queryOneRecord("SELECT server_id, domain FROM mail_domain WHERE domain = '".$app->db->quote($_POST["email_domain"])."' AND ".$app->tform->getAuthSQL('r'));
125             if($domain["domain"] != $_POST["email_domain"]) $app->tform->errorMessage .= $app->tform->wordbook["no_domain_perm"];
126         }
e22f1e 127         
965795 128         
T 129         // if its an insert, check that the password is not empty
130         if($this->id == 0 && $_POST["password"] == '') {
131             $app->tform->errorMessage .= $app->tform->wordbook["error_no_pwd"]."<br>";
132         }
133         
cf8190 134         // Check the client limits, if user is not the admin
965795 135         if($_SESSION["s"]["user"]["typ"] != 'admin') { // if user is not admin
T 136             // Get the limits of the client
137             $client_group_id = $_SESSION["s"]["user"]["default_group"];
138             $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");
22e7f9 139             
965795 140
22e7f9 141             // Check if the user may add another mailbox.
965795 142             if($this->id == 0 && $client["limit_mailbox"] >= 0) {
22e7f9 143                 $tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE sys_groupid = $client_group_id");
T 144                 if($tmp["number"] >= $client["limit_mailbox"]) {
145                     $app->tform->errorMessage .= $app->tform->wordbook["limit_mailbox_txt"]."<br>";
146                 }
147                 unset($tmp);
148             }
965795 149             
T 150             // Check the quota and adjust
151             if($client["limit_mailquota"] >= 0) {
152                 $tmp = $app->db->queryOneRecord("SELECT sum(quota) as mailquota FROM mail_user WHERE mailuser_id != ".intval($this->id)." AND sys_groupid = $client_group_id");
ba747c 153                 $mailquota = $tmp["mailquota"] / 1024 / 1024;
965795 154                 $new_mailbox_quota = intval($this->dataRecord["quota"]);
T 155                 if($mailquota + $new_mailbox_quota > $client["limit_mailquota"]) {
156                     $max_free_quota = $client["limit_mailquota"] - $mailquota;
157                     $app->tform->errorMessage .= $app->tform->wordbook["limit_mailquota_txt"].": ".$max_free_quota."<br>";
158                     // Set the quota field to the max free space
159                     $this->dataRecord["quota"] = $max_free_quota;
160                 }
161                 unset($tmp);
162                 unset($tmp_quota);
22e7f9 163             }
965795 164         } // end if user is not admin
e22f1e 165         
22e7f9 166
e22f1e 167         // compose the email field
b658fe 168         if(isset($_POST["email_local_part"]) && isset($_POST["email_domain"])) {
T 169             $this->dataRecord["email"] = $_POST["email_local_part"]."@".$_POST["email_domain"];
e22f1e 170         
b658fe 171             // Set the server id of the mailbox = server ID of mail domain.
T 172             $this->dataRecord["server_id"] = $domain["server_id"];
e22f1e 173         
b658fe 174             unset($this->dataRecord["email_local_part"]);
T 175             unset($this->dataRecord["email_domain"]);
ac3b1f 176         
b658fe 177             // Convert quota from MB to Bytes
T 178             $this->dataRecord["quota"] = $this->dataRecord["quota"] * 1024 * 1024;
179         
180             // setting Maildir, Homedir, UID and GID
181             $app->uses('getconf');
182             $mail_config = $app->getconf->get_server_config($domain["server_id"],'mail');
183             $maildir = str_replace("[domain]",$domain["domain"],$mail_config["maildir_path"]);
184             $maildir = str_replace("[localpart]",$_POST["email_local_part"],$maildir);
185             $this->dataRecord["maildir"] = $maildir;
186             $this->dataRecord["homedir"] = $mail_config["homedir_path"];
187             $this->dataRecord["uid"] = $mail_config["mailuser_uid"];
188             $this->dataRecord["gid"] = $mail_config["mailuser_gid"];
189         }
22e7f9 190
e22f1e 191         
T 192         parent::onSubmit();
193     }
194     
22e7f9 195     function onAfterInsert() {
T 196         global $app, $conf;
197         
198         // Set the domain owner as mailbox owner
daff5c 199         $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = '".$app->db->quote($_POST["email_domain"])."' AND ".$app->tform->getAuthSQL('r'));
22e7f9 200         $app->db->query("UPDATE mail_user SET sys_groupid = ".$domain["sys_groupid"]." WHERE mailuser_id = ".$this->id);
daff5c 201         
T 202         // send a welcome email to create the mailbox
203         mail($this->dataRecord["email"],$app->tform->wordbook["welcome_mail_subject"],$app->tform->wordbook["welcome_mail_message"]);
204         
205         // Spamfilter policy
206         $policy_id = intval($this->dataRecord["policy"]);
207         if($policy_id > 0) {
8500be 208             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '".mysql_real_escape_string($this->dataRecord["email"])."'");
daff5c 209             if($tmp_user["id"] > 0) {
T 210                 // There is already a record that we will update
866245 211                 $app->db->datalogUpdate('spamfilter_users', "policy_id = $ploicy_id", 'id', $tmp_user["id"]);
daff5c 212             } else {
T 213                 // We create a new record
f66929 214                 $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`) 
8500be 215                         VALUES (".$_SESSION["s"]["user"]["userid"].", ".$domain["sys_groupid"].", 'riud', 'riud', '', ".$domain["server_id"].", 1, ".$policy_id.", '".mysql_real_escape_string($this->dataRecord["email"])."', '".mysql_real_escape_string($this->dataRecord["email"])."', 'Y')";
f66929 216                 $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
daff5c 217             }
T 218         }  // endif spamfilter policy
219         
22e7f9 220     }
T 221     
222     function onAfterUpdate() {
223         global $app, $conf;
224         
225         // Set the domain owner as mailbox owner
b658fe 226         if(isset($_POST["email_domain"])) {
T 227             $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = '".$app->db->quote($_POST["email_domain"])."' AND ".$app->tform->getAuthSQL('r'));
228             $app->db->query("UPDATE mail_user SET sys_groupid = ".$domain["sys_groupid"]." WHERE mailuser_id = ".$this->id);
daff5c 229         
b658fe 230             // Spamfilter policy
T 231             $policy_id = intval($this->dataRecord["policy"]);
8500be 232             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '".mysql_real_escape_string($this->dataRecord["email"])."'");
b658fe 233             if($policy_id > 0) {
T 234                 if($tmp_user["id"] > 0) {
235                     // There is already a record that we will update
866245 236                     $app->db->datalogUpdate('spamfilter_users', "policy_id = $ploicy_id", 'id', $tmp_user["id"]);
b658fe 237                 } else {
T 238                     // We create a new record
f66929 239                     $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`) 
8500be 240                             VALUES (".$_SESSION["s"]["user"]["userid"].", ".$domain["sys_groupid"].", 'riud', 'riud', '', ".$domain["server_id"].", 1, ".$policy_id.", '".mysql_real_escape_string($this->dataRecord["email"])."', '".mysql_real_escape_string($this->dataRecord["email"])."', 'Y')";
f66929 241                     $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
b658fe 242                 }
T 243             }else {
244                 if($tmp_user["id"] > 0) {
245                     // There is already a record but the user shall have no policy, so we delete it
f66929 246                     $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
b658fe 247                 }
T 248             } // endif spamfilter policy
249         }
daff5c 250         
22e7f9 251     }
T 252     
e22f1e 253 }
T 254
255 $app->tform_actions = new page_action;
256 $app->tform_actions->onLoad();
257
348aef 258 ?>