From 4cfd9d0edae233bceb75d81704f285dc43b4b73c Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Sat, 3 Dec 2005 23:07:53 +0000
Subject: [PATCH]

---
 interface/lib/classes/validate_dns.inc.php |  21 +++-
 interface/web/dns/lib/lang/en_soa.lng      |   2 +
 interface/web/dns/list/soa.list.php        |   6 +-
 interface/web/dns/rr_edit.php              | 119 ++++++++++++---------
 interface/web/dns/soa_edit.php             |  91 ++++++----------
 interface/web/dns/templates/soa_list.htm   |   2 +-
 6 files changed, 128 insertions(+), 113 deletions(-)

diff --git a/interface/lib/classes/validate_dns.inc.php b/interface/lib/classes/validate_dns.inc.php
index bca195fe3b..2c6255f477 100644
--- a/interface/lib/classes/validate_dns.inc.php
+++ b/interface/lib/classes/validate_dns.inc.php
@@ -197,6 +197,7 @@ function validate_rr(&$rr){
     break;
   case "MX":
     $error .= $this->validate_field($rr['data'], 'Data', $rr['zone'], 0);
+    $error .= $this->is_integer($rr['aux'], $app->tform->wordbook['aux_txt'], 1);
     break;
   case "NS":
     $error .= $this->validate_field($rr['data'], 'Data', $rr['zone'], 0);
@@ -210,13 +211,12 @@ function validate_rr(&$rr){
     break;
   case "SRV":
     $error .= $this->validate_srv_data($rr['data'], $rr['zone']);
+    $error .= $this->is_integer($rr['aux'], $app->tform->wordbook['aux_txt'], 1);
     break;
   case "TXT":
     break;
   }
 
-  $error .= $this->is_integer($rr['aux'], $app->tform->wordbook['aux_txt'], 1);
-
   $error .= $this->is_integer($rr['ttl'], $app->tform->wordbook['ttl_txt']);
 
 
@@ -257,4 +257,21 @@ function validate_soa(&$soa){
   return $error;
 }
 
+function increase_serial($serial){
+  global $app, $conf;
+
+  // increase serial
+  $serial_date = substr($serial, 0, 8);
+  $count = intval(substr($serial, 8, 2));
+  $current_date = date("Ymd");
+  if($serial_date == $current_date){
+    $count += 1;
+    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
+    $new_serial = $current_date.$count;
+  } else {
+    $new_serial = $current_date.'01';
+  }
+  return $new_serial;
+}
+
 }
\ No newline at end of file
diff --git a/interface/web/dns/lib/lang/en_soa.lng b/interface/web/dns/lib/lang/en_soa.lng
index 7e8b1ebc76..9345cc81a8 100644
--- a/interface/web/dns/lib/lang/en_soa.lng
+++ b/interface/web/dns/lib/lang/en_soa.lng
@@ -16,6 +16,8 @@ $wb["rp_mbox_txt"] = 'RP Mailbox';
 $wb["rp_txtref_txt"] = 'RP TXTREF';
 $wb["srv_target_txt"] = 'SRV TARGET';
 $wb["zone_origin_txt"] = 'Zone Origin';
+$wb["yes_txt"] = 'Yes';
+$wb["no_txt"] = 'No';
 $wb["btn_save_txt"] = 'Save';
 $wb["btn_cancel_txt"] = 'Cancel';
 $wb["soa_error_empty"] = 'The origin must not be empty!';
diff --git a/interface/web/dns/list/soa.list.php b/interface/web/dns/list/soa.list.php
index 9755c1ed5e..1a8c4aa4e7 100644
--- a/interface/web/dns/list/soa.list.php
+++ b/interface/web/dns/list/soa.list.php
@@ -42,7 +42,6 @@ $liste["paging_tpl"]                = "templates/paging.tpl.htm";
 // Enable auth
 $liste["auth"]                                = "yes";
 
-
 /*****************************************************
 * Suchfelder
 *****************************************************/
@@ -132,12 +131,12 @@ $liste["item"][] = array(        'field'                => "ttl",
 
 $liste["item"][] = array(        'field'                => "active",
                                                         'datatype'        => "VARCHAR",
-                                                        'formtype'        => "TEXT",
+                                                        'formtype'        => "SELECT",
                                                         'op'                => "=",
                                                         'prefix'        => "",
                                                         'suffix'        => "",
                                                         'width'                => "",
-                                                        'value'                => "");
+                                                        'value'                => array('Y' => 'Yes', 'N' => 'No'));
 
 /*
 $liste["item"][] = array(        'field'                => "xfer",
@@ -149,5 +148,4 @@ $liste["item"][] = array(        'field'                => "xfer",
                                                         'width'                => "",
                                                         'value'                => "");
 */
-
 ?>
\ No newline at end of file
diff --git a/interface/web/dns/rr_edit.php b/interface/web/dns/rr_edit.php
index ff8590d93a..333111667c 100644
--- a/interface/web/dns/rr_edit.php
+++ b/interface/web/dns/rr_edit.php
@@ -61,62 +61,81 @@ class page_action extends tform_actions {
                 $app->uses('validate_dns');
                 $app->tform->errorMessage .= $app->validate_dns->validate_rr($this->dataRecord);
 
-                // update serial
-                $soa = $app->db->queryOneRecord("SELECT * FROM soa WHERE id = ".$this->dataRecord["zone"]);
-                $serial = $soa['serial'];
-                $update = 0;
-                if($old_record = $app->db->queryOneRecord("SELECT * FROM rr WHERE id = ".$this->dataRecord["id"])){
-                  foreach($old_record as $key => $val){
-                    if($this->dataRecord[$key] != $val) $update += 1;
+                $increased_serials[] = -1;
+                if($app->tform->errorMessage == ''){
+                  // update serial
+                  $soa = $app->db->queryOneRecord("SELECT * FROM soa WHERE id = ".$this->dataRecord["zone"]);
+                  $serial = $soa['serial'];
+                  $update = 0;
+                  if($old_record = $app->db->queryOneRecord("SELECT * FROM rr WHERE id = ".$this->dataRecord["id"])){
+                    foreach($old_record as $key => $val){
+                      if($this->dataRecord[$key] != $val) $update += 1;
+                    }
+                  } else { // new record
+                    $update = 1;
                   }
-                } else { // new record
-                  $update = 1;
-                }
-                if($update > 0){
-                  $serial_date = substr($serial, 0, 8);
-                  $count = intval(substr($serial, 8, 2));
-                  $current_date = date("Ymd");
-                  if($serial_date == $current_date){
-                    $count += 1;
-                    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
-                    $new_serial = $current_date.$count;
-                  } else {
-                    $new_serial = $current_date.'01';
+
+                  if($update > 0){
+                    $new_serial = $app->validate_dns->increase_serial($serial);
+                    $increased_serials[] = $soa['id'];
+                    $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$this->dataRecord["zone"]);
                   }
-                  $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$this->dataRecord["zone"]);
-                }
 
-                // PTR
-                if($conf['auto_create_ptr'] == 1 && trim($conf['default_ns']) != '' && trim($conf['default_mbox']) != ''){
-                  if($this->dataRecord['type'] == 'A' || $this->dataRecord['type'] == 'AAAA'){
-                    list($a, $b, $c, $d) = explode('.', $this->dataRecord['data']);
-                    $ptr_soa = $c.'.'.$b.'.'.$a.'.in-addr.arpa.';
-                    if(substr($this->dataRecord['name'], -1) == '.'){
-                      $ptr_soa_rr_data = $this->dataRecord['name'];
-                    } else {
-                      $ptr_soa_rr_data = $this->dataRecord['name'].(trim($this->dataRecord['name']) == '' ? '' : '.').$soa['origin'];
-                    }
-                    if(!$ptr_soa_exist = $app->db->queryOneRecord("SELECT * FROM soa WHERE origin = '".$ptr_soa."'")){
-                      $app->db->query("INSERT INTO soa (origin, ns, mbox, serial, refresh, retry, expire, minimum, ttl, active) VALUES ('".$ptr_soa."', '".trim($conf['default_ns'])."', '".trim($conf['default_mbox'])."', '".date("Ymd").'01'."', '".$conf['default_refresh']."', '".$conf['default_retry']."', '".$conf['default_expire']."', '".$conf['default_minimum_ttl']."', '".$conf['default_ttl']."', 'Y')");
-                      $ptr_soa_id = $app->db->insertID();
-                      $app->db->query("INSERT INTO rr (zone, name, type, data, aux, ttl) VALUES ('".$ptr_soa_id."', '".$d."', 'PTR', '".$ptr_soa_rr_data."', '0', '".$conf['default_ttl']."')");
-                    } else {
-                      if($ptr_soa_exist['active'] != 'Y') $app->db->query("UPDATE soa SET active = 'Y' WHERE id = ".$ptr_soa_exist['id']);
-                      if(!$ptr_soa_rr_exist = $app->db->queryOneRecord("SELECT * FROM rr WHERE zone = '".$ptr_soa_exist['id']."' AND name = '".$d."' AND type = 'PTR'")){
-                        $app->db->query("INSERT INTO rr (zone, name, type, data, aux, ttl) VALUES ('".$ptr_soa_exist['id']."', '".$d."', 'PTR', '".$ptr_soa_rr_data."', '0', '".$conf['default_ttl']."')");
-                        // increase serial of PTR SOA
-                        $ptr_soa_serial_date = substr($ptr_soa_exist['serial'], 0, 8);
-                        $ptr_soa_count = intval(substr($ptr_soa_exist['serial'], 8, 2));
-                        $ptr_soa_current_date = date("Ymd");
-                        if($ptr_soa_serial_date == $ptr_soa_current_date){
-                          $ptr_soa_count += 1;
-                          $ptr_soa_count = str_pad($ptr_soa_count, 2, "0", STR_PAD_LEFT);
-                          $ptr_soa_new_serial = $ptr_soa_current_date.$ptr_soa_count;
+                  // PTR
+                  if($conf['auto_create_ptr'] == 1 && trim($conf['default_ns']) != '' && trim($conf['default_mbox']) != ''){
+                    if($this->dataRecord['type'] == 'A' || $this->dataRecord['type'] == 'AAAA'){
+                      list($a, $b, $c, $d) = explode('.', $this->dataRecord['data']);
+                      $ptr_soa = $c.'.'.$b.'.'.$a.'.in-addr.arpa.';
+                      if(substr($this->dataRecord['name'], -1) == '.'){
+                        $ptr_soa_rr_data = $this->dataRecord['name'];
+                      } else {
+                        $ptr_soa_rr_data = $this->dataRecord['name'].(trim($this->dataRecord['name']) == '' ? '' : '.').$soa['origin'];
+                      }
+
+                      if(!$ptr_soa_exist = $app->db->queryOneRecord("SELECT * FROM soa WHERE origin = '".$ptr_soa."'")){
+                        $app->db->query("INSERT INTO soa (origin, ns, mbox, serial, refresh, retry, expire, minimum, ttl, active) VALUES ('".$ptr_soa."', '".trim($conf['default_ns'])."', '".trim($conf['default_mbox'])."', '".date("Ymd").'01'."', '".$conf['default_refresh']."', '".$conf['default_retry']."', '".$conf['default_expire']."', '".$conf['default_minimum_ttl']."', '".$conf['default_ttl']."', 'Y')");
+                        $ptr_soa_id = $app->db->insertID();
+                        $app->db->query("INSERT INTO rr (zone, name, type, data, aux, ttl) VALUES ('".$ptr_soa_id."', '".$d."', 'PTR', '".$ptr_soa_rr_data."', '0', '".$conf['default_ttl']."')");
+                      } else {
+                        if($ptr_soa_exist['active'] != 'Y') $app->db->query("UPDATE soa SET active = 'Y' WHERE id = ".$ptr_soa_exist['id']);
+                        if(!$ptr_soa_rr_exist = $app->db->queryOneRecord("SELECT * FROM rr WHERE zone = '".$ptr_soa_exist['id']."' AND name = '".$d."' AND type = 'PTR'")){
+                          $app->db->query("INSERT INTO rr (zone, name, type, data, aux, ttl) VALUES ('".$ptr_soa_exist['id']."', '".$d."', 'PTR', '".$ptr_soa_rr_data."', '0', '".$conf['default_ttl']."')");
+                          // increase serial of PTR SOA
+                          if(!in_array($ptr_soa_exist['id'], $increased_serials)){
+                            $ptr_soa_new_serial = $app->validate_dns->increase_serial($ptr_soa_exist['serial']);
+                            $increased_serials[] = $ptr_soa_exist['id'];
+                            $app->db->query("UPDATE soa SET serial = '".$ptr_soa_new_serial."' WHERE id = ".$ptr_soa_exist['id']);
+                          }
+                        }
+                      }
+
+                      // if IP address changes, delete/change old PTR record
+                      if(!empty($old_record)){
+                        list($oa, $ob, $oc, $od) = explode('.', $old_record['data']);
+                        if($a_rr_with_same_ip = $app->db->queryOneRecord("SELECT rr.*, soa.origin FROM rr, soa WHERE rr.type = 'A' AND rr.data = '".$old_record['data']."' AND rr.zone = soa.id AND soa.active = 'Y' AND rr.id != ".$this->dataRecord["id"])){
+                          if(substr($a_rr_with_same_ip['name'], -1) == '.'){
+                            $new_ptr_soa_rr_data = $a_rr_with_same_ip['name'];
+                          } else {
+                            $new_ptr_soa_rr_data = $a_rr_with_same_ip['name'].(trim($a_rr_with_same_ip['name']) == '' ? '' : '.').$a_rr_with_same_ip['origin'];
+                          }
+                          $app->db->query("UPDATE rr SET data = '".$new_ptr_soa_rr_data."' WHERE zone = '".$ptr_soa_exist['id']."' AND name = '".$od."' AND type = 'PTR'");
                         } else {
-                          $ptr_soa_new_serial = $ptr_soa_current_date.'01';
+                          $old_ptr_soa = $oc.'.'.$ob.'.'.$oa.'.in-addr.arpa.';
+                          $old_ptr_soa_exist = $app->db->queryOneRecord("SELECT * FROM soa WHERE origin = '".$old_ptr_soa."'");
+                          $app->db->query("DELETE FROM rr WHERE zone = '".$old_ptr_soa_exist['id']."' AND name = '".$od."' AND type = 'PTR'");
+                          //die("DELETE FROM rr WHERE zone = '".$old_record['zone']."' AND name = '".$od."' AND type = 'PTR'");
+                          if(!$app->db->queryOneRecord("SELECT * FROM rr WHERE zone = '".$old_ptr_soa_exist['id']."'")){
+                            $app->db->query("DELETE FROM soa WHERE id = ".$old_ptr_soa_exist['id']);
+                          } else {
+                            // increase serial
+                            if(!in_array($old_ptr_soa_exist['id'], $increased_serials)){
+                              $new_serial = $app->validate_dns->increase_serial($old_ptr_soa_exist['serial']);
+                              $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$old_ptr_soa_exist['id']);
+                            }
+                          }
                         }
-                        $app->db->query("UPDATE soa SET serial = '".$ptr_soa_new_serial."' WHERE id = ".$ptr_soa_exist['id']);
                       }
+
                     }
                   }
                 }
diff --git a/interface/web/dns/soa_edit.php b/interface/web/dns/soa_edit.php
index 77fc417856..026576d407 100644
--- a/interface/web/dns/soa_edit.php
+++ b/interface/web/dns/soa_edit.php
@@ -59,77 +59,56 @@ class page_action extends tform_actions {
                 $app->uses('validate_dns');
                 $app->tform->errorMessage .= $app->validate_dns->validate_soa($this->dataRecord);
 
+                $increased_serials[] = -1;
                 // update serial
                 $soa = $app->db->queryOneRecord("SELECT * FROM soa WHERE id = ".$this->dataRecord["id"]);
                 $serial = $soa['serial'];
                 $update = 0;
-                if($old_record = $app->db->queryOneRecord("SELECT * FROM soa WHERE id = ".$this->dataRecord["id"])){
-                  foreach($old_record as $key => $val){
+                if($soa){
+                  foreach($soa as $key => $val){
                     if($this->dataRecord[$key] != $val && $key != 'active') $update += 1;
                   }
                 } else { // new record
                   $update = 1;
                 }
                 if($update > 0){
-                  $serial_date = substr($serial, 0, 8);
-                  $count = intval(substr($serial, 8, 2));
-                  $current_date = date("Ymd");
-                  if($serial_date == $current_date){
-                    $count += 1;
-                    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
-                    $new_serial = $current_date.$count;
-                  } else {
-                    $new_serial = $current_date.'01';
-                  }
+                  $new_serial = $app->validate_dns->increase_serial($serial);
+                  $increased_serials[] = $soa['id'];
                   $this->dataRecord["serial"] = $new_serial;
                 }
 
-                // update rr if origin has changed
-                if($soa['origin'] != $this->dataRecord['origin']){
-                /*
-                  if($rrs = $app->db->queryAllRecords("SELECT * FROM rr WHERE zone = ".$soa['id'])){
-                    foreach($rrs as $rr){
-                      if(substr($rr['name'], -(strlen($this->dataRecord['origin']))) != $this->dataRecord['origin']) $app->db->query("UPDATE rr SET name = '".substr($rr['name'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE id = ".$rr['id']);
-                      if(substr($rr['data'], -(strlen($this->dataRecord['origin']))) != $this->dataRecord['origin']) $app->db->query("UPDATE rr SET data = '".substr($rr['data'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE id = ".$rr['id']);
-                    }
-                  }
-                */
-                  if($rrs = $app->db->queryAllRecords("SELECT * FROM rr")){
-                    $update_soas = array();
-                    foreach($rrs as $rr){
-                      if($soa['origin'] == substr($rr['name'], -(strlen($soa['origin']))) || $soa['origin'] == substr($rr['data'], -(strlen($soa['origin'])))) $update_soas[] = $rr['zone'];
-                      //$update_soas[] = $app->db->queryAllRecords("SELECT DISTINCT zone FROM rr WHERE name LIKE '%".$soa['origin']."' OR data LIKE '%".$soa['origin']."'");
-
-                      $app->db->query("UPDATE rr SET name = '".substr($rr['name'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE name LIKE '%".$soa['origin']."' AND type != 'PTR'");
-                      $app->db->query("UPDATE rr SET data = '".substr($rr['data'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE data LIKE '%".$soa['origin']."' AND type != 'PTR'");
-
-                      if($conf['auto_create_ptr'] == 1 && trim($conf['default_ns']) != '' && trim($conf['default_mbox']) != ''){
-                        $app->db->query("UPDATE rr SET name = '".substr($rr['name'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE name LIKE '%".$soa['origin']."' AND type = 'PTR'");
-                        $app->db->query("UPDATE rr SET data = '".substr($rr['data'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE data LIKE '%".$soa['origin']."' AND type = 'PTR'");
-                      }
-                    }
+                if($soa){
+                  // update rr if origin has changed
+                  if($soa['origin'] != $this->dataRecord['origin']){
+
+                    if($rrs = $app->db->queryAllRecords("SELECT * FROM rr")){
+                      $update_soas = array();
+                      foreach($rrs as $rr){
+                        if($soa['origin'] == substr($rr['name'], -(strlen($soa['origin']))) || $soa['origin'] == substr($rr['data'], -(strlen($soa['origin'])))) $update_soas[] = $rr['zone'];
+                        //$update_soas[] = $app->db->queryAllRecords("SELECT DISTINCT zone FROM rr WHERE name LIKE '%".$soa['origin']."' OR data LIKE '%".$soa['origin']."'");
+
+                        $app->db->query("UPDATE rr SET name = '".substr($rr['name'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE name LIKE '%".$soa['origin']."' AND type != 'PTR'");
+                        $app->db->query("UPDATE rr SET data = '".substr($rr['data'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE data LIKE '%".$soa['origin']."' AND type != 'PTR'");
 
-                    // increase serial
-                    if(!empty($update_soas)){
-                    //print_r($update_soas);
-                    //die();
-                      $update_soas = array_unique($update_soas);
-                      foreach($update_soas as $update_soa){
-                        $u_soa = $app->db->queryOneRecord("SELECT * FROM soa WHERE id = ".$update_soa);
-                        $serial_date = substr($u_soa['serial'], 0, 8);
-                        $count = intval(substr($u_soa['serial'], 8, 2));
-                        $current_date = date("Ymd");
-                        if($serial_date == $current_date){
-                          $count += 1;
-                          $count = str_pad($count, 2, "0", STR_PAD_LEFT);
-                          $new_serial = $current_date.$count;
-                        } else {
-                          $new_serial = $current_date.'01';
-                        }
                         if($conf['auto_create_ptr'] == 1 && trim($conf['default_ns']) != '' && trim($conf['default_mbox']) != ''){
-                          $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$update_soa);
-                        } else {
-                          $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$update_soa." AND origin NOT LIKE '%.in-addr.arpa.'");
+                          $app->db->query("UPDATE rr SET name = '".substr($rr['name'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE name LIKE '%".$soa['origin']."' AND type = 'PTR'");
+                          $app->db->query("UPDATE rr SET data = '".substr($rr['data'], 0, -(strlen($this->dataRecord['origin']))).$this->dataRecord['origin']."' WHERE data LIKE '%".$soa['origin']."' AND type = 'PTR'");
+                        }
+                      }
+
+                      // increase serial
+                      if(!empty($update_soas)){
+                        $update_soas = array_unique($update_soas);
+                        foreach($update_soas as $update_soa){
+                          $u_soa = $app->db->queryOneRecord("SELECT * FROM soa WHERE id = ".$update_soa);
+                          if(!in_array($u_soa['id'], $increased_serials)){
+                            $new_serial = $app->validate_dns->increase_serial($u_soa['serial']);
+                            if($conf['auto_create_ptr'] == 1 && trim($conf['default_ns']) != '' && trim($conf['default_mbox']) != ''){
+                              $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$update_soa);
+                            } else {
+                              $app->db->query("UPDATE soa SET serial = '".$new_serial."' WHERE id = ".$update_soa." AND origin NOT LIKE '%.in-addr.arpa.'");
+                            }
+                          }
                         }
                       }
                     }
diff --git a/interface/web/dns/templates/soa_list.htm b/interface/web/dns/templates/soa_list.htm
index 30404b1256..97e85af618 100644
--- a/interface/web/dns/templates/soa_list.htm
+++ b/interface/web/dns/templates/soa_list.htm
@@ -32,7 +32,7 @@
     <td class="frmText11"><input type="text" name="search_minimum" value="{tmpl_var name='search_minimum'}" class="text" /></td>
     //-->
     <td class="frmText11"><input type="text" name="search_ttl" value="{tmpl_var name='search_ttl'}" class="text" /></td>
-    <td class="frmText11"><input type="text" name="search_active" value="{tmpl_var name='search_active'}" class="text" /></td>
+    <td class="frmText11"><select name="search_active" onChange="document.myform.submit();">{tmpl_var name='search_active'}</select></td>
     <!--
     <td class="frmText11"><input type="text" name="search_xfer" value="{tmpl_var name='search_xfer'}" class="text" /></td>
     //-->
-- 
GitLab