]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/ipinfo.cgi
ipinfo.cgi: Allow to display multiple flags.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ipinfo.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
8ba55ea6 5# Copyright (C) 2010 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
ac1cfefa 21
f367d5b3 22use CGI;
ac1cfefa
MT
23use IO::Socket;
24use strict;
25
26# enable only the following on debugging purpose
27#use warnings;
28#use CGI::Carp 'fatalsToBrowser';
29
986e08d9 30require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
31require "${General::swroot}/lang.pl";
32require "${General::swroot}/header.pl";
dca3f207 33require "${General::swroot}/location-functions.pl";
ac1cfefa
MT
34
35my %cgiparams=();
36
37&Header::showhttpheaders();
38
ac1cfefa 39&Header::openpage($Lang::tr{'ip info'}, 1, '');
ac1cfefa
MT
40&Header::openbigbox('100%', 'left');
41my @lines=();
42my $extraquery='';
f367d5b3 43
4e033257
SS
44# Hash which contains the whois servers from
45# the responisible RIR of the continent.
46my %whois_servers_by_continent = (
47 "AF" => "whois.afrinic.net",
48 "AS" => "whois.apnic.net",
49 "EU" => "whois.ripe.net",
50 "NA" => "whois.arin.net",
51 "SA" => "whois.lacnic.net"
52);
53
54# Default whois server if no continent could be determined.
55my $whois_server = "whois.arin.net";
56
f367d5b3
MT
57my $addr = CGI::param("ip") || "";
58
59if (&General::validip($addr)) {
ac1cfefa
MT
60 my $iaddr = inet_aton($addr);
61 my $hostname = gethostbyaddr($iaddr, AF_INET);
62 if (!$hostname) { $hostname = $Lang::tr{'lookup failed'}; }
63
dca3f207
SS
64 # enumerate location information for IP address...
65 my $db_handle = &Location::Functions::init();
66 my $ccode = &Location::Functions::lookup_country_code($db_handle, $addr);
33975f57 67 my @network_flags = &Location::Functions::address_has_flags($addr);
4e033257
SS
68
69 # Try to get the continent of the country code.
4979ba0a 70 my $continent = &Location::get_continent_code($db_handle, $ccode);
4e033257
SS
71
72 # Check if a whois server for the continent is known.
73 if($whois_servers_by_continent{$continent}) {
74 # Use it.
75 $whois_server = $whois_servers_by_continent{$continent};
76 }
77
dca3f207 78 my $flag_icon = &Location::Functions::get_flag_icon($ccode);
9b5a17bf 79
4e033257 80 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
ac1cfefa
MT
81 if ($sock)
82 {
4e033257 83 print $sock "$addr\n";
ac1cfefa 84 while (<$sock>) {
4e033257 85 $extraquery = $1 if (/ReferralServer: whois:\/\/(\S+)\s+/);
ac1cfefa
MT
86 push(@lines,$_);
87 }
88 close($sock);
89 if ($extraquery) {
90 undef (@lines);
4e033257
SS
91 $whois_server = $extraquery;
92 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
ac1cfefa
MT
93 if ($sock)
94 {
95 print $sock "$addr\n";
96 while (<$sock>) {
97 push(@lines,$_);
98 }
99 }
100 else
101 {
4e033257 102 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
ac1cfefa
MT
103 }
104 }
105 }
106 else
107 {
4e033257 108 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
ac1cfefa
MT
109 }
110
4e033257 111 &Header::openbox('100%', 'left', $addr . " <a href='country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode' title='$ccode' /></a> (" . $hostname . ') : '.$whois_server);
e5b1e648
SS
112
113 # Check if the address has a flag.
33975f57
SS
114 if (@network_flags) {
115 # Get amount of flags for this network.
116 my $flags_amount = @network_flags;
117 my $processed_flags;
118
119 # The message string which will be displayed.
120 my $message_string = "This address is marked as";
121
122 # Loop through the array of network_flags.
123 foreach my $network_flag (@network_flags) {
124 # Increment value of processed flags.
125 $processed_flags++;
126
127 # Get the network flag name.
128 my $network_flag_name = &Location::Functions::get_full_country_name($network_flag);
129
130 # Add the flag name to the message string.
131 $message_string = "$message_string" . " $network_flag_name";
132
133 # Check if multiple flags are set for this network.
134 if ($flags_amount gt "1") {
135 # Check if the the current flag is the next-to-last one.
136 if ($processed_flags eq $flags_amount - 1) {
137 $message_string = "$message_string" . " and ";
138
139 # Check if the current flag it the last one.
140 } elsif ($processed_flags eq $flags_amount) {
141 # The message is finished add a dot for ending the sentence.
142 $message_string = "$message_string" . ".";
143
144 # Otherwise add a simple comma to the message string.
145 } else {
146 $message_string = "$message_string" . ", ";
147 }
148 } else {
149 # Nothing special to do, simple add a dot to finish the sentence.
150 $message_string = "$message_string" . ".";
151 }
152 }
e5b1e648 153
33975f57
SS
154 # Display the generated notice.
155 print "<h3>$message_string</h3>\n";
e5b1e648
SS
156 print "<br>\n";
157 }
158
ac1cfefa
MT
159 print "<pre>\n";
160 foreach my $line (@lines) {
161 print &Header::cleanhtml($line,"y");
162 }
163 print "</pre>\n";
164 &Header::closebox();
f367d5b3
MT
165} else {
166 &Header::openbox('100%', 'left', $Lang::tr{'invalid ip'});
167 print <<EOF;
168 <p style="text-align: center;">
169 $Lang::tr{'invalid ip'}
170 </p>
171EOF
172 &Header::closebox();
ac1cfefa
MT
173}
174
175print <<END
176<div align='center'>
177<table width='80%'>
178<tr>
415cbcec 179 <td align='center'><a href='$ENV{'HTTP_REFERER'}'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td>
ac1cfefa
MT
180</tr>
181</table>
182</div>
183END
184;
185
186&Header::closebigbox();
187
188&Header::closepage();