Patrick Anders
2014-12-09 f7ec00b2f8ba3efc5bdeacef9c813f8a826ae3be
interface/web/mail/mail_domain_edit.php
@@ -176,6 +176,9 @@
            $domain_select .= "<option value=''></option>\r\n";
         }
         $app->tpl->setVar("domain_option", $domain_select);
         $app->tpl->setVar("domain_module", 1);
      } else {
         $app->tpl->setVar("domain_module", 0);
      }
@@ -286,10 +289,10 @@
      //* create dns-record with dkim-values if the zone exists
      if ( (isset($this->dataRecord['dkim']) && $this->dataRecord['dkim'] == 'y') && (isset($this->dataRecord['active']) && $this->dataRecord['active'] == 'y') ) {
         $soa_rec = $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 = ?", $this->dataRecord['domain'].'.');
         if ( isset($soa_rec) ) {
         if ( isset($soa_rec) && !empty($soa_rec) ) {
            //* check for a dkim-record in the dns
            $dns_data = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE name = ? AND sys_groupid = ?", $this->dataRecord['dkim_selector'].'._domainkey.'.$this->dataRecord['domain'].'.', $_SESSION["s"]["user"]['sys_groupid']);
            if ( isset($dns_data) ) {
            if ( isset($dns_data) && !empty($dns_data) ) {
               $dns_data['data'] = 'v=DKIM1; t=s; p='.str_replace(array('-----BEGIN PUBLIC KEY-----','-----END PUBLIC KEY-----',"\r","\n"), '', $this->dataRecord['dkim_public']);
               $dns_data['active'] = 'Y';
               $dns_data['stamp'] = date('Y-m-d H:i:s');
@@ -346,7 +349,7 @@
      //* get domain-data from the db
      $mail_data = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = ?", $this->dataRecord['domain']);
      
      if ( isset($mail_data) ) {
      if ( isset($mail_data) && !empty($mail_data) ) {
         $post_data = $mail_data;
         $post_data['dkim_selector'] = $this->dataRecord['dkim_selector'];
         $post_data['dkim_public'] = $this->dataRecord['dkim_public'];
@@ -358,7 +361,7 @@
      //* dkim-value changed
      if ( $mail_data != $post_data ) {
         //* get the dns-record for the public from the db
         $dns_data = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE name = ? AND sys_groupid = ?'", $mail_data['dkim_selector'].'._domainkey.'.$mail_data['domain'].'.', $mail_data['sys_groupid']);
         $dns_data = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE name = ? AND sys_groupid = ?", $mail_data['dkim_selector'].'._domainkey.'.$mail_data['domain'].'.', $mail_data['sys_groupid']);
         //* we modify dkim dns-values for active mail-domains only
         if ( $post_data['active'] == 'y' ) {
@@ -376,7 +379,7 @@
            }
         } else $new_dns_data['active'] = 'N';
         if ( isset($dns_data) && isset($new_dns_data) ) {
         if ( isset($dns_data) && !empty($dns_data) && isset($new_dns_data) ) {
            //* update dns-record
            $new_dns_data['serial'] = $app->validate_dns->increase_serial($dns_data['serial']);
            $app->db->datalogUpdate('dns_rr', $new_dns_data, 'id', $dns_data['id']);
@@ -387,7 +390,7 @@
            //* create a new dns-record
            $new_dns_data = $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 = ?", $mail_data['domain'].'.');
            //* create a new record only if the dns-zone exists
            if ( isset($new_dns_data) && $post_data['dkim'] == 'y' ) {
            if ( isset($new_dns_data) && !empty($new_dns_data) && $post_data['dkim'] == 'y' ) {
               $new_dns_data['name'] = $post_data['dkim_selector'].'._domainkey.'.$post_data['domain'].'.';
               $new_dns_data['type'] = 'TXT';
               $new_dns_data['data'] = 'v=DKIM1; t=s; p='.str_replace(array('-----BEGIN PUBLIC KEY-----','-----END PUBLIC KEY-----',"\r","\n"), '', $post_data['dkim_public']);
@@ -428,6 +431,57 @@
            $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
         }
      } // endif spamfilter policy
      //** If the domain name or owner has been changed, change the domain and owner in all mailbox records
      if($this->oldDataRecord['domain'] != $this->dataRecord['domain'] || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) {
         $app->uses('getconf');
         $mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"], 'mail');
         //* Update the mailboxes
         $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
         $sys_groupid = $app->functions->intval((isset($this->dataRecord['client_group_id']))?$this->dataRecord['client_group_id']:$this->oldDataRecord['sys_groupid']);
         $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
         $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
         if(is_array($mailusers)) {
            foreach($mailusers as $rec) {
               // setting Maildir, Homedir, UID and GID
               $mail_parts = explode("@", $rec['email']);
               $maildir = str_replace("[domain]", $this->dataRecord['domain'], $mail_config["maildir_path"]);
               $maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
               $maildir = $app->db->quote($maildir);
               $email = $app->db->quote($mail_parts[0].'@'.$this->dataRecord['domain']);
               $app->db->datalogUpdate('mail_user', "maildir = '$maildir', email = '$email', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailuser_id', $rec['mailuser_id']);
            }
         }
         //* Update the aliases
         $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'])."'");
         if(is_array($forwardings)) {
            foreach($forwardings as $rec) {
               $destination = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']));
               $source = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['source']));
               $app->db->datalogUpdate('mail_forwarding', "source = '$source', destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'forwarding_id', $rec['forwarding_id']);
            }
         }
         //* Update the mailinglist
         $app->db->query("UPDATE mail_mailinglist SET sys_userid = $client_user_id, sys_groupid = $sys_groupid WHERE domain = '".$app->db->quote($this->oldDataRecord['domain'])."'");
         //* Update fetchmail accounts
         $fetchmail = $app->db->queryAllRecords("SELECT * FROM mail_get WHERE destination like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
         if(is_array($fetchmail)) {
            foreach($fetchmail as $rec) {
               $destination = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']));
               $app->db->datalogUpdate('mail_get', "destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailget_id', $rec['mailget_id']);
            }
         }
         //* Delete the old spamfilter record
         $tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->oldDataRecord["domain"])."'");
         $app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
         unset($tmp);
      } // end if domain name changed
   }
}