@@ -61,62 +61,81 @@ function onSubmit() {
6161 $ app ->uses ('validate_dns ' );
6262 $ app ->tform ->errorMessage .= $ app ->validate_dns ->validate_rr ($ this ->dataRecord );
6363
64- // update serial
65- $ soa = $ app ->db ->queryOneRecord ("SELECT * FROM soa WHERE id = " .$ this ->dataRecord ["zone " ]);
66- $ serial = $ soa ['serial ' ];
67- $ update = 0 ;
68- if ($ old_record = $ app ->db ->queryOneRecord ("SELECT * FROM rr WHERE id = " .$ this ->dataRecord ["id " ])){
69- foreach ($ old_record as $ key => $ val ){
70- if ($ this ->dataRecord [$ key ] != $ val ) $ update += 1 ;
64+ $ increased_serials [] = -1 ;
65+ if ($ app ->tform ->errorMessage == '' ){
66+ // update serial
67+ $ soa = $ app ->db ->queryOneRecord ("SELECT * FROM soa WHERE id = " .$ this ->dataRecord ["zone " ]);
68+ $ serial = $ soa ['serial ' ];
69+ $ update = 0 ;
70+ if ($ old_record = $ app ->db ->queryOneRecord ("SELECT * FROM rr WHERE id = " .$ this ->dataRecord ["id " ])){
71+ foreach ($ old_record as $ key => $ val ){
72+ if ($ this ->dataRecord [$ key ] != $ val ) $ update += 1 ;
73+ }
74+ } else { // new record
75+ $ update = 1 ;
7176 }
72- } else { // new record
73- $ update = 1 ;
74- }
75- if ($ update > 0 ){
76- $ serial_date = substr ($ serial , 0 , 8 );
77- $ count = intval (substr ($ serial , 8 , 2 ));
78- $ current_date = date ("Ymd " );
79- if ($ serial_date == $ current_date ){
80- $ count += 1 ;
81- $ count = str_pad ($ count , 2 , "0 " , STR_PAD_LEFT );
82- $ new_serial = $ current_date .$ count ;
83- } else {
84- $ new_serial = $ current_date .'01 ' ;
77+
78+ if ($ update > 0 ){
79+ $ new_serial = $ app ->validate_dns ->increase_serial ($ serial );
80+ $ increased_serials [] = $ soa ['id ' ];
81+ $ app ->db ->query ("UPDATE soa SET serial = ' " .$ new_serial ."' WHERE id = " .$ this ->dataRecord ["zone " ]);
8582 }
86- $ app ->db ->query ("UPDATE soa SET serial = ' " .$ new_serial ."' WHERE id = " .$ this ->dataRecord ["zone " ]);
87- }
8883
89- // PTR
90- if ($ conf ['auto_create_ptr ' ] == 1 && trim ($ conf ['default_ns ' ]) != '' && trim ($ conf ['default_mbox ' ]) != '' ){
91- if ($ this ->dataRecord ['type ' ] == 'A ' || $ this ->dataRecord ['type ' ] == 'AAAA ' ){
92- list ($ a , $ b , $ c , $ d ) = explode ('. ' , $ this ->dataRecord ['data ' ]);
93- $ ptr_soa = $ c .'. ' .$ b .'. ' .$ a .'.in-addr.arpa. ' ;
94- if (substr ($ this ->dataRecord ['name ' ], -1 ) == '. ' ){
95- $ ptr_soa_rr_data = $ this ->dataRecord ['name ' ];
96- } else {
97- $ ptr_soa_rr_data = $ this ->dataRecord ['name ' ].(trim ($ this ->dataRecord ['name ' ]) == '' ? '' : '. ' ).$ soa ['origin ' ];
98- }
99- if (!$ ptr_soa_exist = $ app ->db ->queryOneRecord ("SELECT * FROM soa WHERE origin = ' " .$ ptr_soa ."' " )){
100- $ 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') " );
101- $ ptr_soa_id = $ app ->db ->insertID ();
102- $ 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 ' ]."') " );
103- } else {
104- if ($ ptr_soa_exist ['active ' ] != 'Y ' ) $ app ->db ->query ("UPDATE soa SET active = 'Y' WHERE id = " .$ ptr_soa_exist ['id ' ]);
105- if (!$ ptr_soa_rr_exist = $ app ->db ->queryOneRecord ("SELECT * FROM rr WHERE zone = ' " .$ ptr_soa_exist ['id ' ]."' AND name = ' " .$ d ."' AND type = 'PTR' " )){
106- $ 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 ' ]."') " );
107- // increase serial of PTR SOA
108- $ ptr_soa_serial_date = substr ($ ptr_soa_exist ['serial ' ], 0 , 8 );
109- $ ptr_soa_count = intval (substr ($ ptr_soa_exist ['serial ' ], 8 , 2 ));
110- $ ptr_soa_current_date = date ("Ymd " );
111- if ($ ptr_soa_serial_date == $ ptr_soa_current_date ){
112- $ ptr_soa_count += 1 ;
113- $ ptr_soa_count = str_pad ($ ptr_soa_count , 2 , "0 " , STR_PAD_LEFT );
114- $ ptr_soa_new_serial = $ ptr_soa_current_date .$ ptr_soa_count ;
84+ // PTR
85+ if ($ conf ['auto_create_ptr ' ] == 1 && trim ($ conf ['default_ns ' ]) != '' && trim ($ conf ['default_mbox ' ]) != '' ){
86+ if ($ this ->dataRecord ['type ' ] == 'A ' || $ this ->dataRecord ['type ' ] == 'AAAA ' ){
87+ list ($ a , $ b , $ c , $ d ) = explode ('. ' , $ this ->dataRecord ['data ' ]);
88+ $ ptr_soa = $ c .'. ' .$ b .'. ' .$ a .'.in-addr.arpa. ' ;
89+ if (substr ($ this ->dataRecord ['name ' ], -1 ) == '. ' ){
90+ $ ptr_soa_rr_data = $ this ->dataRecord ['name ' ];
91+ } else {
92+ $ ptr_soa_rr_data = $ this ->dataRecord ['name ' ].(trim ($ this ->dataRecord ['name ' ]) == '' ? '' : '. ' ).$ soa ['origin ' ];
93+ }
94+
95+ if (!$ ptr_soa_exist = $ app ->db ->queryOneRecord ("SELECT * FROM soa WHERE origin = ' " .$ ptr_soa ."' " )){
96+ $ 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') " );
97+ $ ptr_soa_id = $ app ->db ->insertID ();
98+ $ 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 ' ]."') " );
99+ } else {
100+ if ($ ptr_soa_exist ['active ' ] != 'Y ' ) $ app ->db ->query ("UPDATE soa SET active = 'Y' WHERE id = " .$ ptr_soa_exist ['id ' ]);
101+ if (!$ ptr_soa_rr_exist = $ app ->db ->queryOneRecord ("SELECT * FROM rr WHERE zone = ' " .$ ptr_soa_exist ['id ' ]."' AND name = ' " .$ d ."' AND type = 'PTR' " )){
102+ $ 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 ' ]."') " );
103+ // increase serial of PTR SOA
104+ if (!in_array ($ ptr_soa_exist ['id ' ], $ increased_serials )){
105+ $ ptr_soa_new_serial = $ app ->validate_dns ->increase_serial ($ ptr_soa_exist ['serial ' ]);
106+ $ increased_serials [] = $ ptr_soa_exist ['id ' ];
107+ $ app ->db ->query ("UPDATE soa SET serial = ' " .$ ptr_soa_new_serial ."' WHERE id = " .$ ptr_soa_exist ['id ' ]);
108+ }
109+ }
110+ }
111+
112+ // if IP address changes, delete/change old PTR record
113+ if (!empty ($ old_record )){
114+ list ($ oa , $ ob , $ oc , $ od ) = explode ('. ' , $ old_record ['data ' ]);
115+ 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 " ])){
116+ if (substr ($ a_rr_with_same_ip ['name ' ], -1 ) == '. ' ){
117+ $ new_ptr_soa_rr_data = $ a_rr_with_same_ip ['name ' ];
118+ } else {
119+ $ new_ptr_soa_rr_data = $ a_rr_with_same_ip ['name ' ].(trim ($ a_rr_with_same_ip ['name ' ]) == '' ? '' : '. ' ).$ a_rr_with_same_ip ['origin ' ];
120+ }
121+ $ app ->db ->query ("UPDATE rr SET data = ' " .$ new_ptr_soa_rr_data ."' WHERE zone = ' " .$ ptr_soa_exist ['id ' ]."' AND name = ' " .$ od ."' AND type = 'PTR' " );
115122 } else {
116- $ ptr_soa_new_serial = $ ptr_soa_current_date .'01 ' ;
123+ $ old_ptr_soa = $ oc .'. ' .$ ob .'. ' .$ oa .'.in-addr.arpa. ' ;
124+ $ old_ptr_soa_exist = $ app ->db ->queryOneRecord ("SELECT * FROM soa WHERE origin = ' " .$ old_ptr_soa ."' " );
125+ $ app ->db ->query ("DELETE FROM rr WHERE zone = ' " .$ old_ptr_soa_exist ['id ' ]."' AND name = ' " .$ od ."' AND type = 'PTR' " );
126+ //die("DELETE FROM rr WHERE zone = '".$old_record['zone']."' AND name = '".$od."' AND type = 'PTR'");
127+ if (!$ app ->db ->queryOneRecord ("SELECT * FROM rr WHERE zone = ' " .$ old_ptr_soa_exist ['id ' ]."' " )){
128+ $ app ->db ->query ("DELETE FROM soa WHERE id = " .$ old_ptr_soa_exist ['id ' ]);
129+ } else {
130+ // increase serial
131+ if (!in_array ($ old_ptr_soa_exist ['id ' ], $ increased_serials )){
132+ $ new_serial = $ app ->validate_dns ->increase_serial ($ old_ptr_soa_exist ['serial ' ]);
133+ $ app ->db ->query ("UPDATE soa SET serial = ' " .$ new_serial ."' WHERE id = " .$ old_ptr_soa_exist ['id ' ]);
134+ }
135+ }
117136 }
118- $ app ->db ->query ("UPDATE soa SET serial = ' " .$ ptr_soa_new_serial ."' WHERE id = " .$ ptr_soa_exist ['id ' ]);
119137 }
138+
120139 }
121140 }
122141 }
0 commit comments