Skip to content

Commit 2844e9b

Browse files
author
Till Brehm
committed
Fixes #5265 IDN Domain issues in email domain form
Fixes #4972 Domain in spamfilter_user settings not punicode encoded
1 parent 3f7f1e8 commit 2844e9b

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

interface/lib/plugins/mail_mail_domain_plugin.inc.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ function onLoad() {
2626
*/
2727
function mail_mail_domain_edit($event_name, $page_form) {
2828
global $app, $conf;
29+
30+
$domain = $app->functions->idn_encode($page_form->dataRecord['domain']);
2931

3032
// make sure that the record belongs to the client group and not the admin group when a dmin inserts it
3133
// also make sure that the user can not delete entry created by an admin
@@ -57,7 +59,7 @@ function mail_mail_domain_edit($event_name, $page_form) {
5759
}
5860

5961
//** If the domain name or owner has been changed, change the domain and owner in all mailbox records
60-
if($page_form->oldDataRecord && ($page_form->oldDataRecord['domain'] != $page_form->dataRecord['domain'] ||
62+
if($page_form->oldDataRecord && ($page_form->oldDataRecord['domain'] != $domain ||
6163
(isset($page_form->dataRecord['client_group_id']) && $page_form->oldDataRecord['sys_groupid'] != $page_form->dataRecord['client_group_id']))) {
6264
$app->uses('getconf');
6365
$mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail');
@@ -71,9 +73,9 @@ function mail_mail_domain_edit($event_name, $page_form) {
7173
foreach($mailusers as $rec) {
7274
// setting Maildir, Homedir, UID and GID
7375
$mail_parts = explode("@", $rec['email']);
74-
$maildir = str_replace("[domain]", $page_form->dataRecord['domain'], $mail_config["maildir_path"]);
76+
$maildir = str_replace("[domain]", $domain, $mail_config["maildir_path"]);
7577
$maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
76-
$email = $mail_parts[0].'@'.$page_form->dataRecord['domain'];
78+
$email = $mail_parts[0].'@'.$domain;
7779
$app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']);
7880
}
7981
}
@@ -82,8 +84,8 @@ function mail_mail_domain_edit($event_name, $page_form) {
8284
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $page_form->oldDataRecord['domain'], "%@" . $page_form->oldDataRecord['domain']);
8385
if(is_array($forwardings)) {
8486
foreach($forwardings as $rec) {
85-
$destination = str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination']);
86-
$source = str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['source']);
87+
$destination = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['destination']);
88+
$source = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['source']);
8789
$app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']);
8890
}
8991
}
@@ -100,18 +102,18 @@ function mail_mail_domain_edit($event_name, $page_form) {
100102
$mail_gets = $app->db->queryAllRecords("SELECT mailget_id, destination FROM mail_get WHERE destination LIKE ?", "%@" . $page_form->oldDataRecord['domain']);
101103
if(is_array($mail_gets)) {
102104
foreach($mail_gets as $rec) {
103-
$destination = str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination']);
105+
$destination = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['destination']);
104106
$app->db->datalogUpdate('mail_get', array("destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailget_id', $rec['mailget_id']);
105107
}
106108
}
107109

108-
if ($page_form->oldDataRecord["domain"] != $page_form->dataRecord['domain']) {
110+
if ($page_form->oldDataRecord["domain"] != $domain) {
109111
//* Delete the old spamfilter record
110112
$tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", "@" . $page_form->oldDataRecord["domain"]);
111113
$app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
112114
unset($tmp);
113115
}
114-
$app->db->query("UPDATE spamfilter_users SET email=REPLACE(email, ?, ?), sys_userid = ?, sys_groupid = ? WHERE email LIKE ?", $page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $client_user_id, $sys_groupid, "%@" . $page_form->oldDataRecord['domain']);
116+
$app->db->query("UPDATE spamfilter_users SET email=REPLACE(email, ?, ?), sys_userid = ?, sys_groupid = ? WHERE email LIKE ?", $page_form->oldDataRecord['domain'], $domain, $client_user_id, $sys_groupid, "%@" . $page_form->oldDataRecord['domain']);
115117

116118
} // end if domain name changed
117119
}

interface/web/mail/mail_domain_edit.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,13 @@ function onSubmit() {
281281

282282
function onAfterInsert() {
283283
global $app, $conf;
284+
285+
$domain = $app->functions->idn_encode($this->dataRecord["domain"]);
284286

285287
// Spamfilter policy
286288
$policy_id = $app->functions->intval($this->dataRecord["policy"]);
287289
if($policy_id > 0) {
288-
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $this->dataRecord["domain"]);
290+
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain);
289291
if($tmp_user["id"] > 0) {
290292
// There is already a record that we will update
291293
$app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
@@ -301,8 +303,8 @@ function onAfterInsert() {
301303
"server_id" => $this->dataRecord["server_id"],
302304
"priority" => 5,
303305
"policy_id" => $policy_id,
304-
"email" => '@' . $this->dataRecord["domain"],
305-
"fullname" => '@' . $this->dataRecord["domain"],
306+
"email" => '@' . $domain,
307+
"fullname" => '@' . $domain,
306308
"local" => 'Y'
307309
);
308310
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
@@ -324,6 +326,8 @@ function onAfterInsert() {
324326

325327
function onBeforeUpdate() {
326328
global $app, $conf;
329+
330+
$domain = $app->functions->idn_encode($this->dataRecord["domain"]);
327331

328332
//* Check if the server has been changed
329333
// We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
@@ -339,7 +343,7 @@ function onBeforeUpdate() {
339343
} else {
340344
//* We do not allow users to change a domain which has been created by the admin
341345
$rec = $app->db->queryOneRecord("SELECT domain from mail_domain WHERE domain_id = ?", $this->id);
342-
if($rec['domain'] != $this->dataRecord["domain"] && !$app->tform->checkPerm($this->id, 'u')) {
346+
if($rec['domain'] != $domain && !$app->tform->checkPerm($this->id, 'u')) {
343347
//* Add a error message and switch back to old server
344348
$app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
345349
$this->dataRecord["domain"] = $rec['domain'];
@@ -352,9 +356,11 @@ function onBeforeUpdate() {
352356
function onAfterUpdate() {
353357
global $app, $conf;
354358

359+
$domain = $app->functions->idn_encode($this->dataRecord["domain"]);
360+
355361
// Spamfilter policy
356362
$policy_id = $app->functions->intval($this->dataRecord["policy"]);
357-
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $this->dataRecord["domain"]);
363+
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain);
358364
if($policy_id > 0) {
359365
if($tmp_user["id"] > 0) {
360366
// There is already a record that we will update
@@ -371,8 +377,8 @@ function onAfterUpdate() {
371377
"server_id" => $this->dataRecord["server_id"],
372378
"priority" => 5,
373379
"policy_id" => $policy_id,
374-
"email" => '@' . $this->dataRecord["domain"],
375-
"fullname" => '@' . $this->dataRecord["domain"],
380+
"email" => '@' . $domain,
381+
"fullname" => '@' . $domain,
376382
"local" => 'Y'
377383
);
378384
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
@@ -385,7 +391,7 @@ function onAfterUpdate() {
385391
}
386392
} // endif spamfilter policy
387393
//** If the domain name or owner has been changed, change the domain and owner in all mailbox records
388-
if($this->oldDataRecord['domain'] != $this->dataRecord['domain'] || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) {
394+
if($this->oldDataRecord['domain'] != $domain || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) {
389395
$app->uses('getconf');
390396
$mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"], 'mail');
391397

@@ -398,7 +404,7 @@ function onAfterUpdate() {
398404
foreach($mailusers as $rec) {
399405
// setting Maildir, Homedir, UID and GID
400406
$mail_parts = explode("@", $rec['email']);
401-
$maildir = str_replace("[domain]", $this->dataRecord['domain'], $mail_config["maildir_path"]);
407+
$maildir = str_replace("[domain]", $domain, $mail_config["maildir_path"]);
402408
$maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
403409
$email = $mail_parts[0].'@'.$this->dataRecord['domain'];
404410
$app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']);
@@ -409,8 +415,8 @@ function onAfterUpdate() {
409415
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $this->oldDataRecord['domain'], '%@' . $this->oldDataRecord['domain']);
410416
if(is_array($forwardings)) {
411417
foreach($forwardings as $rec) {
412-
$destination = str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']);
413-
$source = str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['source']);
418+
$destination = str_replace($this->oldDataRecord['domain'], $domain, $rec['destination']);
419+
$source = str_replace($this->oldDataRecord['domain'], $domain, $rec['source']);
414420
$app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']);
415421
}
416422
}
@@ -422,7 +428,7 @@ function onAfterUpdate() {
422428
$fetchmail = $app->db->queryAllRecords("SELECT * FROM mail_get WHERE destination like ?", '%@' . $this->oldDataRecord['domain']);
423429
if(is_array($fetchmail)) {
424430
foreach($fetchmail as $rec) {
425-
$destination = str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']);
431+
$destination = str_replace($this->oldDataRecord['domain'], $domain, $rec['destination']);
426432
$app->db->datalogUpdate('mail_get', array("destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailget_id', $rec['mailget_id']);
427433
}
428434
}
@@ -436,15 +442,15 @@ function onAfterUpdate() {
436442

437443
//* update dns-record when the dkim record was changed
438444
// NOTE: only if the domain-name was not changed
439-
if ( $this->dataRecord['active'] == 'y' && $this->dataRecord['domain'] == $this->oldDataRecord['domain'] ) {
445+
if ( $this->dataRecord['active'] == 'y' && $domain == $this->oldDataRecord['domain'] ) {
440446
$dkim_active = @($this->dataRecord['dkim'] == 'y') ? true : false;
441447
$selector = @($this->dataRecord['dkim_selector'] != $this->oldDataRecord['dkim_selector']) ? true : false;
442448
$dkim_private = @($this->dataRecord['dkim_private'] != $this->oldDataRecord['dkim_private']) ? true : false;
443449

444-
$soaDomain = $this->dataRecord['domain'].'.';
450+
$soaDomain = $domain.'.';
445451
while ((!isset($soa) && (substr_count($soaDomain,'.') > 1))) {
446452
$soa = $app->db->queryOneRecord("SELECT id AS zone, sys_userid, sys_groupid, sys_perm_user, sys_perm_group, sys_perm_other, server_id, ttl, serial FROM dns_soa WHERE active = 'Y' AND origin = ?", $soaDomain);
447-
$soaDomain = preg_replace("/^\w+\./","",$soaDomain);
453+
$soaDomain = preg_replace("/^[\w\-]+\./","",$soaDomain);
448454
}
449455

450456
if ( ($selector || $dkim_private || $dkim_active) && $dkim_active )
@@ -455,7 +461,7 @@ function onAfterUpdate() {
455461
if (! $dkim_active) {
456462
// updated existing dmarc-record to policy 'none'
457463
$sql = "SELECT * from dns_rr WHERE name = ? AND data LIKE 'v=DMARC1%' AND " . $app->tform->getAuthSQL('r');
458-
$rec = $app->db->queryOneRecord($sql, '_dmarc.'.$this->dataRecord['domain'].'.');
464+
$rec = $app->db->queryOneRecord($sql, '_dmarc.'.$domain.'.');
459465
if (is_array($rec))
460466
if (strpos($rec['data'], 'p=none=') === false) {
461467
$rec['data'] = str_replace(array('quarantine', 'reject'), 'none', $rec['data']);

0 commit comments

Comments
 (0)