updated database.defaults.sql and added some example scripts
[NeonServV5.git] / scripts / examples / dnslookup.php
1 <?php
2 /* dnslookup.c - NeonServ v5.6
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 function time2str($time, $anz = 9) {
20     $str="";
21     if(!$anz) $anz=9;
22     if($time>=(60*60*24*365) && $anz > 0) {
23         $anz--;
24         $years=floor($time/(60*60*24*365));
25         $str.=$years."Y";
26         $time=$time-((60*60*24*365)*$years);
27     }
28     if($time>=(60*60*24*30) && $anz > 0) {
29         $anz--;
30         $months=floor($time/(60*60*24*30));
31         if($str != "") $str.=" ";
32         $str.=$months."M";
33         $time=$time-((60*60*24*30)*$months);
34     }
35     if($time>=(60*60*24) && $anz > 0) {
36         $anz--;
37         $days=floor($time/(60*60*24));
38         if($str != "") $str.=" ";
39         $str.=$days."d";
40         $time=$time-((60*60*24)*$days);
41     }
42     if($time>=(60*60) && $anz > 0) {
43         $anz--;
44         $stunden=floor($time/(60*60));
45         if($str != "") $str.=" ";
46         $str.=$stunden."h";
47         $time=$time-((60*60)*$stunden);
48     }
49     if($time>=(60) && $anz > 0) {
50         $anz--;
51         $min=floor($time/(60));
52         if($str != "") $str.=" ";
53         $str.=$min."m";
54         $time=$time-((60)*$min);
55     }
56     if(($time>1 || $str == "") && $anz > 0){
57         $anz--;
58         if($str != "") $str.=" ";
59         $str.=$time."s";
60     }
61     return $str;
62 }
63
64
65 $host = $argv[1];
66 $show_record = strtoupper($argv[2]);
67
68 $pattern_ipv6 = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/';
69 $pattern_ipv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
70
71 $record_order = array("SOA", "NS", "A", "AAAA", "MX");
72
73 function show_subrecords($host) {
74     $dns = dns_get_record($host, DNS_ALL);
75     if(count($dns)) {
76         usort($dns, "sort_records");
77         foreach($dns as $record) {
78             switch($record['type']) {
79                 case "A":
80                     echo"   \002A     \002 ".$record['ip'].($record['ttl'] < 86400 ? "  (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
81                     break;
82                 case "AAAA":
83                     echo"   \002AAAA  \002 ".$record['ipv6'].($record['ttl'] < 86400 ? "  (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
84                     break;
85                 case "CNAME":
86                     echo"   \002CNAME \002 ".$record['target']."\n";
87                     break;
88             }
89         }
90     }
91 }
92
93 function sort_records($a, $b) {
94     global $record_order;
95     $index_a = array_search($a['type'], $record_order);
96     if($index_a === FALSE) $index_a = count($record_order);
97     $index_b = array_search($b['type'], $record_order);
98     if($index_b === FALSE) $index_b = count($record_order);
99     $order = $index_a - $index_b;
100     if($order == 0) {
101         switch($record['type']) {
102         case "A":
103             $suborder = "ip";
104             break;
105         case "AAAA":
106             $suborder = "ipv6";
107             break;
108         case "TXT":
109             $suborder = "txt";
110             break;
111         default:
112             $suborder = "target";
113             break;
114         }
115         $order = strcmp($a[$suborder], $b[$suborder]);
116     }
117     return $order;
118 }
119
120 if(strlen($host) && preg_match("#^((([a-z0-9-.]*)\.|)([a-z]{2,5})|).?$#i", $host)) {
121     $dns = dns_get_record($host, DNS_ALL);
122     echo"DNS Records for \002".$host."\002:";
123     if($show_record != "" && $show_record != "*" && $show_record != "ANY")
124         echo" (".$show_record.")";
125     echo"\n";
126     if(count($dns)) {
127         usort($dns, "sort_records");
128         foreach($dns as $record) {
129             if($show_record != "" && $show_record != "*" && $show_record != "ANY" && $show_record != $record['type'])
130                 continue;
131             switch($record['type']) {
132                 case "A":
133                     echo" \002A     \002 ".$record['ip'].($record['ttl'] < 86400 ? "  (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
134                     break;
135                 case "AAAA":
136                     echo" \002AAAA  \002 ".$record['ipv6'].($record['ttl'] < 86400 ? "  (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
137                     break;
138                 case "MX":
139                     echo" \002MX    \002 ".$record['target']." (priority: ".$record['pri'].")\n";
140                     if($show_record == "MX") {
141                         show_subrecords($record['target']);
142                     }
143                     break;
144                 case "NS":
145                     echo" \002NS    \002 ".$record['target']."\n";
146                     if($show_record == "NS") {
147                         show_subrecords($record['target']);
148                     }
149                     break;
150                 case "CNAME":
151                     echo" \002CNAME \002 ".$record['target']."\n";
152                     break;
153                 case "TXT":
154                     echo" \002TXT   \002 ".$record['txt']."\n";
155                     break;
156                 case "SOA":
157                     if($show_record != "SOA") {
158                         echo" \002SOA   \002 (see: dns ".$host." SOA)\n";
159                         break;
160                     }
161                     echo" \002SOA   \002 (Start of Authority):\n";
162                     echo"    name:     ".$record['mname']."\n";
163                     echo"    serial:   ".$record['serial']."\n";
164                     echo"    refresh:  ".$record['refresh']." (".time2str($record['refresh'], 2).")\n";
165                     echo"    retry:    ".$record['retry']." (".time2str($record['retry'], 2).")\n";
166                     echo"    expire:   ".$record['expire']." (".time2str($record['expire'], 2).")\n";
167                     echo"    TTL:      ".$record['minimum-ttl']." (".time2str($record['minimum-ttl'], 2).")\n";
168                     break;
169             }
170         }
171     } else
172         echo"No records found.\n";
173 } else if(preg_match($pattern_ipv4, $host) || preg_match($pattern_ipv6, $host)) {
174     $hostname = gethostbyaddr($host);
175     echo"Reverse Lookup for \002".$host."\002:\n";
176     echo " \002PTR  \002 ".$hostname."\n";
177 } else {
178     echo"Invalid Hostname or IP-Address.\n";
179 }
180 ?>