-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.php
147 lines (113 loc) · 3.72 KB
/
parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* Parser by [email protected], Dec 2017
* Written for output to be used by dnsmasq
*
*/
$cfg = array();
$cfg['mysql_hostname'] = 'yourserverIP';
$cfg['mysql_username'] = 'dnsadmin';
$cfg['mysql_password'] = '<sup3rsecrEt!>';
$cfg['mysql_database'] = 'dnsadmin';
$cfg['mail_to'] = '[email protected]';
$cfg['mail_from'] = '[email protected]';
$cfg['servernr'] = 'server1';
$cfg['outputfile'] = '/etc/dnsmasq.d/domain/forwardrecords.conf';
$cfg['process_restart'] = '/etc/init.d/dnsmasq restart';
$cfg['debug'] = 0;
if($cfg['debug'] > 0) {
ini_set('display_errors', '1');
error_reporting(E_ALL);
}
/*************************************************************************************/
$dbh = connect_db();
$change = get_changed_param();
if ($change == "1") {
$data = "#Generated by DNS admin parser\n#Do not modify manually, changes will be lost\n";
foreach (get_available_zones() as $zone) {
$_zone = $zone['zonename'];
foreach (get_available_records($_zone) as $record) {
$record['recordtype'] = strtoupper($record['recordtype']);
$_hostname = $record['hostname'];
$_value = $record['recordvalue'];
if ($record['recordtype'] == "PTR" ) {
$data .= sprintf("ptr-record=%s.%s,%s\n",$_hostname,$_zone,$_value);
} elseif ($record['recordtype'] == "A" || $record['recordtype'] == "AAAA") {
$data .= sprintf("address=/%s.%s/%s\n",$_hostname,$_zone,$_value);
}
}
}
file_put_contents($cfg['outputfile'], $data);
set_changed_param();
exec("{$cfg['process_restart']}");
} elseif ($change == "0") {
//no changes
}
disconnect_db($dbh);
/*************************************************************************************/
function connect_db() {
global $cfg;
$dbh = new mysqli($cfg['mysql_hostname'], $cfg['mysql_username'], $cfg['mysql_password'], $cfg['mysql_database']);
if($dbh->connect_errno > 0){
printf("<font class=\"error\"><b>ERROR:</b> Unable to connect to database: %s\n<br>\n", $dbh->connect_error);
exit;
}
$dbh->set_charset("utf8");
return($dbh);
}
function disconnect_db($dbh) {
$dbh->close();
}
function get_available_zones() {
global $dbh;
$q_select = $dbh->prepare("SELECT id,zonename,active FROM `_zones` WHERE active = 1 AND `datedeleted` IS NULL");
$q_select->execute();
$r_select = $q_select->get_result();
$zones = array();
if($r_select->num_rows > 0) {
while($row_select = $r_select->fetch_assoc()) {
$zones[$row_select['id']] = $row_select;
}
}
$r_select->free_result();
$q_select->close();
return($zones);
}
function get_available_records($zone) {
global $dbh;
$_zone = "`{$zone}`";
$q_select = $dbh->prepare("SELECT id,zoneid,hostname,recordtype,recordvalue,pref,active FROM {$_zone} WHERE `datedeleted` IS NULL ORDER BY `hostname`");
$q_select->execute();
$r_select = $q_select->get_result();
$records = array();
if($r_select->num_rows > 0) {
while($row_select = $r_select->fetch_assoc()) {
$records[$row_select['id']] = $row_select;
}
}
$r_select->free_result();
$q_select->close();
return($records);
}
function get_changed_param() {
global $cfg,$dbh;
$q_select = $dbh->prepare("SELECT {$cfg['servernr']} FROM `_update` WHERE id = 1");
$q_select->execute();
$r_select = $q_select->get_result();
$changed = array();
if($r_select->num_rows > 0) {
$changed = $r_select->fetch_assoc();
}
$r_select->free_result();
$q_select->close();
return($changed["{$cfg['servernr']}"]);
}
function set_changed_param() {
global $cfg,$dbh;
$_value = "0";
$q_select = $dbh->prepare("UPDATE `_update` SET {$cfg['servernr']} = ? WHERE id = 1");
$q_select->bind_param('i',$_value);
$q_select->execute();
$q_select->close();
}
?>