Marius Cramer
2014-08-13 42539643c396f9d8865dcf9a51b13dc869709d16
commit | author | age
532ae5 1 <?php
L 2 /*
3 Copyright (c) 2007, 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
35 $tform_def_file = "form/mail_domain.tform.php";
36
37 /******************************************
38 * End Form configuration
39 ******************************************/
40
7fe908 41 require_once '../../lib/config.inc.php';
MC 42 require_once '../../lib/app.inc.php';
532ae5 43
L 44 //* Check permissions for module
45 $app->auth->check_module_permissions('mail');
46
47 // Loading classes
10b4c8 48 $app->uses('tpl,tform,tform_actions,tools_sites');
532ae5 49 $app->load('tform_actions');
L 50
51 class page_action extends tform_actions {
52
53     function onShowNew() {
54         global $app, $conf;
55
56         // we will check only users, not admins
57         if($_SESSION["s"]["user"]["typ"] == 'user') {
58             if(!$app->tform->checkClientLimit('limit_maildomain')) {
59                 $app->error($app->tform->wordbook["limit_maildomain_txt"]);
60             }
61             if(!$app->tform->checkResellerLimit('limit_maildomain')) {
62                 $app->error('Reseller: '.$app->tform->wordbook["limit_maildomain_txt"]);
63             }
64         }
65
66         parent::onShowNew();
67     }
68
69     function onShowEnd() {
70         global $app, $conf;
71
72         if($_SESSION["s"]["user"]["typ"] == 'admin') {
73             // Getting Clients of the user
ca4b71 74             $sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND sys_group.client_id > 0 ORDER BY client.company_name, client.contact_name, sys_group.name";
7fe908 75
532ae5 76             $clients = $app->db->queryAllRecords($sql);
L 77             $client_select = '';
78             if($_SESSION["s"]["user"]["typ"] == 'admin') $client_select .= "<option value='0'></option>";
8ab3cd 79             //$tmp_data_record = $app->tform->getDataRecord($this->id);
532ae5 80             if(is_array($clients)) {
L 81                 foreach( $clients as $client) {
8ab3cd 82                     $selected = @(is_array($this->dataRecord) && ($client["groupid"] == $this->dataRecord['client_group_id'] || $client["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
615a0a 83                     $client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
532ae5 84                 }
L 85             }
7fe908 86             $app->tpl->setVar("client_group_id", $client_select);
532ae5 87
L 88         } elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
89
90             // Get the limits of the client
91             $client_group_id = $_SESSION["s"]["user"]["default_group"];
615a0a 92             $client = $app->db->queryOneRecord("SELECT client.client_id, client.contact_name, client.default_mailserver, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname, sys_group.name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id order by client.contact_name");
532ae5 93
L 94             // Set the mailserver to the default server of the client
95             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = $client[default_mailserver]");
7fe908 96             $app->tpl->setVar("server_id", "<option value='$client[default_mailserver]'>$tmp[server_name]</option>");
532ae5 97             unset($tmp);
L 98
99             // Fill the client select field
ca4b71 100             $sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND client.parent_client_id = ".$app->functions->intval($client['client_id'])." ORDER BY client.company_name, client.contact_name, sys_group.name";
532ae5 101             $clients = $app->db->queryAllRecords($sql);
604c0c 102             $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".$app->functions->intval($client['client_id']));
615a0a 103             $client_select = '<option value="'.$tmp['groupid'].'">'.$client['contactname'].'</option>';
8ab3cd 104             //$tmp_data_record = $app->tform->getDataRecord($this->id);
532ae5 105             if(is_array($clients)) {
L 106                 foreach( $clients as $client) {
8ab3cd 107                     $selected = @(is_array($this->dataRecord) && ($client["groupid"] == $this->dataRecord['client_group_id'] || $client["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
615a0a 108                     $client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
532ae5 109                 }
L 110             }
7fe908 111             $app->tpl->setVar("client_group_id", $client_select);
532ae5 112
L 113         }
114
115         /*
116          * Now we have to check, if we should use the domain-module to select the domain
117          * or not
118          */
119         $app->uses('ini_parser,getconf');
120         $settings = $app->getconf->get_global_config('domains');
121         if ($settings['use_domain_module'] == 'y') {
122             /*
123              * The domain-module is in use.
124             */
10b4c8 125             $domains = $app->tools_sites->getDomainModuleDomains();
532ae5 126             $domain_select = '';
L 127             if(is_array($domains) && sizeof($domains) > 0) {
128                 /* We have domains in the list, so create the drop-down-list */
129                 foreach( $domains as $domain) {
4e18bd 130                     $domain_select .= "<option value=" . $domain['domain_id'] ;
532ae5 131                     if ($domain['domain'] == $this->dataRecord["domain"]) {
L 132                         $domain_select .= " selected";
133                     }
8c1761 134                     $domain_select .= ">" . $app->functions->idn_decode($domain['domain']) . "</option>\r\n";
532ae5 135                 }
L 136             }
137             else {
138                 /*
139                  * We have no domains in the domain-list. This means, we can not add ANY new domain.
140                  * To avoid, that the variable "domain_option" is empty and so the user can
141                  * free enter a domain, we have to create a empty option!
142                 */
143                 $domain_select .= "<option value=''></option>\r\n";
144             }
7fe908 145             $app->tpl->setVar("domain_option", $domain_select);
532ae5 146         }
L 147
148
149         // Get the spamfilter policys for the user
604c0c 150         $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->dataRecord["domain"])."'");
ed1d29 151         $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r')." ORDER BY policy_name";
532ae5 152         $policys = $app->db->queryAllRecords($sql);
L 153         $policy_select = "<option value='0'>".$app->tform->wordbook["no_policy"]."</option>";
154         if(is_array($policys)) {
155             foreach( $policys as $p) {
156                 $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':'';
157                 $policy_select .= "<option value='$p[id]' $selected>$p[policy_name]</option>\r\n";
158             }
159         }
7fe908 160         $app->tpl->setVar("policy", $policy_select);
532ae5 161         unset($policys);
L 162         unset($policy_select);
163         unset($tmp_user);
164
165         if($this->id > 0) {
166             //* we are editing a existing record
167             $app->tpl->setVar("edit_disabled", 1);
168             $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
169         } else {
170             $app->tpl->setVar("edit_disabled", 0);
171         }
172
173         parent::onShowEnd();
174     }
175
176     function onSubmit() {
177         global $app, $conf;
7fe908 178
MC 179         /* check if the domain module is used - and check if the selected domain can be used! */
4e18bd 180         $app->uses('ini_parser,getconf');
M 181         $settings = $app->getconf->get_global_config('domains');
182         if ($settings['use_domain_module'] == 'y') {
7fe908 183             $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
MC 184             if(!$domain_check) {
185                 // invalid domain selected
186                 $app->tform->errorMessage .= $app->tform->lng("domain_error_empty")."<br />";
187             } else {
188                 $this->dataRecord['domain'] = $domain_check;
189             }
190         }
191
192         if($_SESSION["s"]["user"]["typ"] != 'admin') {
532ae5 193
L 194             // Get the limits of the client
604c0c 195             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
532ae5 196             $client = $app->db->queryOneRecord("SELECT limit_maildomain, default_mailserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
L 197
198             // When the record is updated
199             if($this->id > 0) {
200                 // restore the server ID if the user is not admin and record is edited
65ea2e 201                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM mail_domain WHERE domain_id = ".$app->functions->intval($this->id));
532ae5 202                 $this->dataRecord["server_id"] = $tmp["server_id"];
L 203                 unset($tmp);
204                 // When the record is inserted
205             } else {
206                 // set the server ID to the default mailserver of the client
207                 $this->dataRecord["server_id"] = $client["default_mailserver"];
208
209                 // Check if the user may add another mail_domain
210                 if($client["limit_maildomain"] >= 0) {
211                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE sys_groupid = $client_group_id");
212                     if($tmp["number"] >= $client["limit_maildomain"]) {
213                         $app->error($app->tform->wordbook["limit_maildomain_txt"]);
214                     }
215                 }
216             }
217
218             // Clients may not set the client_group_id, so we unset them if user is not a admin
219             if(!$app->auth->has_clients($_SESSION['s']['user']['userid'])) unset($this->dataRecord["client_group_id"]);
220         }
221
222         //* make sure that the email domain is lowercase
223         if(isset($this->dataRecord["domain"])) $this->dataRecord["domain"] = strtolower($this->dataRecord["domain"]);
224
225
226         parent::onSubmit();
227     }
228
229     function onAfterInsert() {
230         global $app, $conf;
231
232         // make sure that the record belongs to the client group and not the admin group when a dmin inserts it
233         // also make sure that the user can not delete domain created by a admin
234         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 235             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
532ae5 236             $app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
L 237         }
238         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 239             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
532ae5 240             $app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
L 241         }
242
243         // Spamfilter policy
65ea2e 244         $policy_id = $app->functions->intval($this->dataRecord["policy"]);
532ae5 245         if($policy_id > 0) {
cb904e 246             $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->dataRecord["domain"])."'");
532ae5 247             if($tmp_user["id"] > 0) {
L 248                 // There is already a record that we will update
249                 $app->db->datalogUpdate('spamfilter_users', "policy_id = $ploicy_id", 'id', $tmp_user["id"]);
250             } else {
251                 $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$this->id);
252                 // We create a new record
253                 $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
604c0c 254                         VALUES (".$_SESSION["s"]["user"]["userid"].", ".$app->functions->intval($tmp_domain["sys_groupid"]).", 'riud', 'riud', '', ".$app->functions->intval($this->dataRecord["server_id"]).", 5, ".$app->functions->intval($policy_id).", '@".$app->db->quote($this->dataRecord["domain"])."', '@".$app->db->quote($this->dataRecord["domain"])."', 'Y')";
532ae5 255                 $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
L 256                 unset($tmp_domain);
257             }
258         }  // endif spamfilter policy
259     }
260
261     function onBeforeUpdate() {
262         global $app, $conf;
263
264         //* Check if the server has been changed
265         // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
266         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
267             $rec = $app->db->queryOneRecord("SELECT server_id, domain from mail_domain WHERE domain_id = ".$this->id);
268             if($rec['server_id'] != $this->dataRecord["server_id"]) {
269                 //* Add a error message and switch back to old server
270                 $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
271                 $this->dataRecord["server_id"] = $rec['server_id'];
272             }
273             unset($rec);
274             //* If the user is neither admin nor reseller
275         } else {
276             //* We do not allow users to change a domain which has been created by the admin
277             $rec = $app->db->queryOneRecord("SELECT domain from mail_domain WHERE domain_id = ".$this->id);
7fe908 278             if($rec['domain'] != $this->dataRecord["domain"] && $app->tform->checkPerm($this->id, 'u')) {
532ae5 279                 //* Add a error message and switch back to old server
L 280                 $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
281                 $this->dataRecord["domain"] = $rec['domain'];
282             }
283             unset($rec);
284         }
285     }
286
287
288
289     function onAfterUpdate() {
290         global $app, $conf;
291
c9435b 292         // make sure that the record belongs to the client group and not the admin group when admin inserts it
532ae5 293         // also make sure that the user can not delete domain created by a admin
L 294         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 295             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
c9435b 296             $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
T 297             $client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
298             $app->db->query("UPDATE mail_domain SET sys_userid = $client_user_id, sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
532ae5 299         }
L 300         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 301             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
c9435b 302             $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
T 303             $client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
304             $app->db->query("UPDATE mail_domain SET sys_userid = $client_user_id, sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
532ae5 305         }
L 306
307         // Spamfilter policy
65ea2e 308         $policy_id = $app->functions->intval($this->dataRecord["policy"]);
cb904e 309         $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->dataRecord["domain"])."'");
532ae5 310         if($policy_id > 0) {
L 311             if($tmp_user["id"] > 0) {
312                 // There is already a record that we will update
313                 $app->db->datalogUpdate('spamfilter_users', "policy_id = $policy_id", 'id', $tmp_user["id"]);
314             } else {
315                 $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$this->id);
316                 // We create a new record
317                 $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
604c0c 318                         VALUES (".$_SESSION["s"]["user"]["userid"].", ".$tmp_domain["sys_groupid"].", 'riud', 'riud', '', ".$app->functions->intval($this->dataRecord["server_id"]).", 5, ".$app->functions->intval($policy_id).", '@".$app->db->quote($this->dataRecord["domain"])."', '@".$app->db->quote($this->dataRecord["domain"])."', 'Y')";
532ae5 319                 $app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
L 320                 unset($tmp_domain);
321             }
322         } else {
323             if($tmp_user["id"] > 0) {
324                 // There is already a record but the user shall have no policy, so we delete it
325                 $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
326             }
327         } // endif spamfilter policy
328         //** If the domain name or owner has been changed, change the domain and owner in all mailbox records
329         if($this->oldDataRecord['domain'] != $this->dataRecord['domain'] || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) {
330             $app->uses('getconf');
7fe908 331             $mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"], 'mail');
532ae5 332
L 333             //* Update the mailboxes
cb904e 334             $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
604c0c 335             $sys_groupid = $app->functions->intval((isset($this->dataRecord['client_group_id']))?$this->dataRecord['client_group_id']:$this->oldDataRecord['sys_groupid']);
c9435b 336             $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
604c0c 337             $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
532ae5 338             if(is_array($mailusers)) {
L 339                 foreach($mailusers as $rec) {
340                     // setting Maildir, Homedir, UID and GID
7fe908 341                     $mail_parts = explode("@", $rec['email']);
MC 342                     $maildir = str_replace("[domain]", $this->dataRecord['domain'], $mail_config["maildir_path"]);
343                     $maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
cb904e 344                     $maildir = $app->db->quote($maildir);
T 345                     $email = $app->db->quote($mail_parts[0].'@'.$this->dataRecord['domain']);
c9435b 346                     $app->db->datalogUpdate('mail_user', "maildir = '$maildir', email = '$email', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailuser_id', $rec['mailuser_id']);
532ae5 347                 }
L 348             }
349
350             //* Update the aliases
4bd960 351             $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like '%@".$app->db->quote($this->oldDataRecord['domain'])."' OR destination like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
532ae5 352             if(is_array($forwardings)) {
L 353                 foreach($forwardings as $rec) {
7fe908 354                     $destination = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']));
MC 355                     $source = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['source']));
c9435b 356                     $app->db->datalogUpdate('mail_forwarding', "source = '$source', destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'forwarding_id', $rec['forwarding_id']);
532ae5 357                 }
L 358             }
7fe908 359
532ae5 360             //* Update the mailinglist
c9435b 361             $app->db->query("UPDATE mail_mailinglist SET sys_userid = $client_user_id, sys_groupid = $sys_groupid WHERE domain = '".$app->db->quote($this->oldDataRecord['domain'])."'");
0df526 362             
TB 363             //* Update fetchmail accounts
364             $fetchmail = $app->db->queryAllRecords("SELECT * FROM mail_get WHERE destination like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
365             if(is_array($fetchmail)) {
366                 foreach($fetchmail as $rec) {
367                     $destination = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']));
368                     $app->db->datalogUpdate('mail_get', "destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailget_id', $rec['mailget_id']);
369                 }
370             }
371             
532ae5 372             //* Delete the old spamfilter record
cb904e 373             $tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->oldDataRecord["domain"])."'");
532ae5 374             $app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
L 375             unset($tmp);
376
377         } // end if domain name changed
378
379     }
380
381 }
382
383 $page = new page_action;
384 $page->onLoad();
385
7fe908 386 ?>