forked from Coopeh/PHP-MultiDig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.php
182 lines (168 loc) · 6.97 KB
/
process.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
// Using Net_DNS2 library to complete the DNS requests
require_once('Net/DNS2.php');
// Create the request variable and choose the DNS server in use
if( isset( $_POST['dns-server'] ) ) {
$r = new Net_DNS2_Resolver( array( 'nameservers' => array( $_POST['dns-server'] ) ) );
} else {
$r = new Net_DNS2_Resolver( array( 'nameservers' => array( '8.8.8.8' ) ) );
}
// Make sure that the domains textbox has some domains in it
if( isset( $_POST['domains'] ) && !empty( $_POST['domains'] ) ) {
get_dns_records( $_POST['radio'], $_POST['domains'] );
}
function is_valid_domain_name( $domain_name ) {
return ( preg_match( "/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name ) //valid chars check
&& preg_match( "/^.{1,253}$/", $domain_name ) //overall length check
&& preg_match( "/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name ) ); //length of each label
}
// The main function
function get_dns_records( $type, $domains ){
// Allow the request variable from above
global $r;
// Make array from list of domains
$urls = preg_split( '/\s+/', $domains );
// Check which radio button was selected and assign record type
switch( $type ){
case "radio1":
$record_type = "A";
break;
default;
case "radio2":
$record_type = "AAAA";
break;
case "radio3":
$record_type = "CNAME";
break;
case "radio4":
$record_type = "MX";
break;
case "radio5":
$record_type = "NS";
break;
case "radio6":
$record_type = "PTR";
break;
case "radio7":
$record_type = "SPF";
break;
case "radio8":
$record_type = "TXT";
break;
case "radio9":
$record_type = "REVERSE";
break;
}
// Loop through the domains given
foreach( $urls as $data ) {
if ( is_valid_domain_name( $data ) ) {
// If the record type isn't a reverse lookup then use Net_DNS2 to run the DNS query
if( $record_type !== "REVERSE" ) {
try {
$record = $r->query( $data, $record_type );
} catch( Net_DNS2_Exception $e ) {
// If the query fails completely then let us know why
$record = "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">". $e->getMessage()."</span></td></tr>\n";
}
// If the record type is reverse lookup make sure the data given in the textbox matches standard IPv4/v6 types
} elseif( $record_type === "REVERSE" ) {
if( filter_var( $data, FILTER_VALIDATE_IP ) ){
$record = gethostbyaddr( $data );
} else {
$record = "Please enter a valid IPv4/v6 address";
}
}
// If record type isn't REVERSE and there's no record for the query let us know
if( $record_type !== "REVERSE" && empty( $record->answer ) ) {
// If the DNS entry doesn't exist then tell us
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\"> No record available</span></td></tr>\r\n";
// Otherwise, echo out the record results for each of the queries
} else {
switch( $record_type ) {
// Record type is set as A or AAAA
case "A":
case "AAAA":
foreach( $record->answer as $dnsr ) {
if( isset( $dnsr->address ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsr->address."</td></tr>\r\n";
} elseif( isset( $dnsr->cname ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsr->cname."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
default;
break;
// Record type is set as CNAME
case "CNAME":
foreach( $record->answer as $dnsr ) {
if( isset( $dnsr->cname ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsr->cname."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
break;
// Record type is set as MX
case "MX":
foreach( $record->answer as $dnsr ) {
if( isset( $dnsr->preference ) && isset( $dnsr->exchange ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsr->preference."</td><td class=\"record\">".$dnsr->exchange."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
break;
// Record type is set as NS
case "NS":
foreach( $record->answer as $dnsr ) {
if( isset( $dnsr->nsdname ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsr->nsdname."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
break;
// Record type is set as PTR
case "PTR":
foreach( $record->answer as $dnsr ){
if( isset( $dnsr->name ) && isset( $dnsr->ptrdname ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsr->name."</td><td class=\"record\">".$dnsr->ptrdname."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
break;
// Record type is set as SPF or TXT
case "SPF":
case "TXT":
foreach( $record->answer as $dnsr ){
if( isset( $dnsr->text ) ) {
foreach( $dnsr->text as $dnsrtext ) {
if( isset( $dnsrtext ) ) {
echo "<tr><td>".$data."</td><td class=\"record\">".$dnsrtext."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
}
break;
// Record type is set as ReverseDNS
case "REVERSE":
if( isset( $record ) ) {
echo "<tr><td>".$data."</td><td class=\"record\"> ".$record."</td></tr>\r\n";
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">No record available</span></td></tr>\r\n";
}
break;
}
}
} else {
echo "<tr><td>".$data."</td><td class=\"record\"><span class=\"norecord\">Invalid domain name entered</span></td></tr>\r\n";
}
}
}
?>