From 4cfd9d0edae233bceb75d81704f285dc43b4b73c Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Sat, 03 Dec 2005 18:07:53 -0500
Subject: [PATCH] 

---
 interface/web/dns/rr_edit.php |  127 ++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 54 deletions(-)

diff --git a/interface/web/dns/rr_edit.php b/interface/web/dns/rr_edit.php
index ff8590d..3331116 100644
--- a/interface/web/dns/rr_edit.php
+++ b/interface/web/dns/rr_edit.php
@@ -61,62 +61,81 @@
                 $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;
-                  }
-                } 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';
-                  }
-                  $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'];
+                $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;
                     }
-                    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;
-                        } else {
-                          $ptr_soa_new_serial = $ptr_soa_current_date.'01';
-                        }
-                        $app->db->query("UPDATE soa SET serial = '".$ptr_soa_new_serial."' WHERE id = ".$ptr_soa_exist['id']);
+                  } else { // new record
+                    $update = 1;
+                  }
+
+                  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"]);
+                  }
+
+                  // 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 {
+                          $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']);
+                            }
+                          }
+                        }
+                      }
+
                     }
                   }
                 }

--
Gitblit v1.9.1