6
6
#-----------------------------------------------------------------------------
7
7
# Perl Required Modules: Geo::IP or Geo::IP::PurePerl
8
8
#-----------------------------------------------------------------------------
9
# $Revision: 1.14 $ - $Author: eldy $ - $Date: 2008/11/15 16:03:42 $
9
# $Revision: 1.18 $ - $Author: eldy $ - $Date: 2010/08/04 12:38:02 $
66
70
# ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
67
debug(" Plugin geoip_isp_maxmind: InitParams=$InitParams",1);
68
my ($mode,$datafile)=split(/\s+/,$InitParams,2);
71
debug(" Plugin $PluginName: InitParams=$InitParams",1);
72
my ($mode,$tmpdatafile)=split(/\s+/,$InitParams,2);
73
my ($datafile,$override)=split(/\+/,$tmpdatafile,2);
69
74
if (! $datafile) { $datafile="GeoIPIsp.dat"; }
75
else { $datafile =~ s/%20/ /g; }
70
76
if ($type eq 'geoippureperl') {
71
77
# With pureperl with always use GEOIP_STANDARD.
72
78
# GEOIP_MEMORY_CACHE seems to fail with ActiveState
76
82
if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
77
83
else { $mode=Geo::IP::GEOIP_STANDARD(); }
79
debug(" Plugin geoip_isp_maxmind: GeoIP initialized type=$type mode=$mode",1);
85
if ($override){ $override =~ s/%20/ /g; $OverrideFile=$override; }
87
debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode",1);
80
88
if ($type eq 'geoippureperl') {
81
89
$geoip_isp_maxmind = Geo::IP::PurePerl->open($datafile, $mode);
83
91
$geoip_isp_maxmind = Geo::IP->open($datafile, $mode);
85
94
# Fails on some GeoIP version
86
95
# debug(" Plugin geoip_isp_maxmind: GeoIP initialized database_info=".$geoip_isp_maxmind->database_info());
100
109
my $menulink=$_[2];
101
110
my $menutext=$_[3];
103
if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLMenuLink"); }
112
if ($Debug) { debug(" Plugin $PluginName: AddHTMLMenuLink"); }
104
113
if ($categ eq 'who') {
105
$menu->{'plugin_geoip_isp_maxmind'}=0.6; # Pos
106
$menulink->{'plugin_geoip_isp_maxmind'}=2; # Type of link
107
$menutext->{'plugin_geoip_isp_maxmind'}="ISP"; # Text
114
$menu->{"plugin_$PluginName"}=0.6; # Pos
115
$menulink->{"plugin_$PluginName"}=2; # Type of link
116
$menutext->{"plugin_$PluginName"}="ISP"; # Text
126
135
my $total_p; my $total_h; my $total_k;
127
136
my $rest_p; my $rest_h; my $rest_k;
129
if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLGraph $categ $menu $menulink $menutext"); }
138
if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph $categ $menu $menulink $menutext"); }
131
140
&tab_head("$title",19,0,'isp');
132
141
print "<tr bgcolor=\"#$color_TableBGRowTitle\"><th>ISP : ".((scalar keys %_isp_h)-($_isp_h{'unknown'}?1:0))."</th>";
149
158
my $isp=$key; $isp =~ s/_/ /g;
150
159
print "<td class=\"aws\">".ucfirst($isp)."</td>";
151
if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?$_isp_p{$key}:" ")."</td>"; }
160
if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?Format_Number($_isp_p{$key}):" ")."</td>"; }
152
161
if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?"$p_p %":' ')."</td>"; }
153
if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?$_isp_h{$key}:" ")."</td>"; }
162
if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?Format_Number($_isp_h{$key}):" ")."</td>"; }
154
163
if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?"$p_h %":' ')."</td>"; }
155
164
if ($ShowISP =~ /B/i) { print "<td>".Format_Bytes($_isp_k{$key})."</td>"; }
156
165
if ($ShowISP =~ /L/i) { print "<td>".($_isp_p{$key}?Format_Date($_isp_l{$key},1):'-')."</td>"; }
180
189
if ($TotalHits) { $p_h=int($rest_h/$TotalHits*1000)/10; }
182
191
print "<td class=\"aws\"><span style=\"color: #$color_other\">$Message[2]/$Message[0]</span></td>";
183
if ($ShowISP =~ /P/i) { print "<td>".($rest_p?$rest_p:" ")."</td>"; }
192
if ($ShowISP =~ /P/i) { print "<td>".($rest_p?Format_Number($rest_p):" ")."</td>"; }
184
193
if ($ShowISP =~ /P/i) { print "<td>".($rest_p?"$p_p %":' ')."</td>"; }
185
if ($ShowISP =~ /H/i) { print "<td>".($rest_h?$rest_h:" ")."</td>"; }
194
if ($ShowISP =~ /H/i) { print "<td>".($rest_h?Format_Number($rest_h):" ")."</td>"; }
186
195
if ($ShowISP =~ /H/i) { print "<td>".($rest_h?"$p_h %":' ')."</td>"; }
187
196
if ($ShowISP =~ /B/i) { print "<td>".Format_Bytes($rest_k)."</td>"; }
188
197
if ($ShowISP =~ /L/i) { print "<td> </td>"; }
224
233
if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
226
235
print "<th width=\"80\">";
227
print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_geoip_isp_maxmind"):"$PROG$StaticLinks.plugin_geoip_isp_maxmind.$StaticExt")."\"$NewLinkTarget>GeoIP<br />ISP</a>";
236
print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_$PluginName"):"$StaticLinks.plugin_$PluginName.$StaticExt")."\"$NewLinkTarget>GeoIP<br />ISP</a>";
242
251
if ($key && $ip==4) {
244
if ($type eq 'geoippureperl')
252
my $isp = TmpLookup_geoip_isp_maxmind($param);
253
if (!$isp && $type eq 'geoippureperl')
246
255
# Function isp_by_addr does not exists in PurePerl but isp_by_name do same
247
256
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
251
260
# Function isp_by_addr does not exits, so we use org_by_addr
252
261
$isp=$geoip_isp_maxmind->org_by_addr($param) if $geoip_isp_maxmind;
254
if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); }
263
if ($Debug) { debug(" Plugin $PluginName: GetIspByIp for $param: [$isp]",5); }
256
265
if (length($isp) <= $MAXLENGTH) {
266
275
print "<span style=\"color: #$color_other\">$Message[0]</span>";
270
if ($type eq 'geoippureperl')
278
my $isp = TmpLookup_geoip_isp_maxmind($param);
279
if (!$isp && $type eq 'geoippureperl')
272
281
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
276
285
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
278
if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); }
287
if ($Debug) { debug(" Plugin $PluginName: GetIspByHostname for $param: [$isp]",5); }
280
289
if (length($isp) <= $MAXLENGTH) {
317
326
sub SectionProcessIp_geoip_isp_maxmind {
318
327
my $param="$_[0]"; # Param must be an IP
321
if ($type eq 'geoippureperl')
329
my $isp = TmpLookup_geoip_isp_maxmind($param);
330
if (!$isp && $type eq 'geoippureperl')
323
332
# Function isp_by_addr does not exists in PurePerl but isp_by_name do same
324
333
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
328
337
# Function isp_by_addr does not exits, so we use org_by_addr
329
338
$isp=$geoip_isp_maxmind->org_by_addr($param) if $geoip_isp_maxmind;
331
if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); }
340
if ($Debug) { debug(" Plugin $PluginName: GetIspByIp for $param: [$isp]",5); }
333
342
$isp =~ s/\s/_/g;
348
357
sub SectionProcessHostname_geoip_isp_maxmind {
349
358
my $param="$_[0]"; # Param must be an IP
352
if ($type eq 'geoippureperl')
354
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
358
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
360
if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); }
360
my $isp = TmpLookup_geoip_isp_maxmind($param);
361
if (!$isp && $type eq 'geoippureperl')
363
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
367
$isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
369
if ($Debug) { debug(" Plugin $PluginName: GetIspByHostname for $param: [$isp]",5); }
362
371
$isp =~ s/\s/_/g;
396
405
@field=split(/\s+/,($xmlold?XMLDecodeFromHisto($_):$_));
399
until ($field[0] eq 'END_PLUGIN_geoip_isp_maxmind' || $field[0] eq "${xmleb}END_PLUGIN_geoip_isp_maxmind" || ! $_);
400
if ($field[0] ne 'END_PLUGIN_geoip_isp_maxmind' && $field[0] ne "${xmleb}END_PLUGIN_geoip_isp_maxmind") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); }
401
if ($Debug) { debug(" Plugin geoip_isp_maxmind: End of PLUGIN_geoip_isp_maxmind section ($count entries, $countloaded loaded)"); }
408
until ($field[0] eq "END_PLUGIN_$PluginName" || $field[0] eq "${xmleb}END_PLUGIN_$PluginName" || ! $_);
409
if ($field[0] ne "END_PLUGIN_$PluginName" && $field[0] ne "${xmleb}END_PLUGIN_$PluginName") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); }
410
if ($Debug) { debug(" Plugin $PluginName: End of PLUGIN_$PluginName ($count entries, $countloaded loaded)"); }
409
418
#-----------------------------------------------------------------------------
410
419
sub SectionWriteHistory_geoip_isp_maxmind {
411
420
my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift);
412
if ($Debug) { debug(" Plugin geoip_isp_maxmind: SectionWriteHistory_geoip_isp_maxmind start - ".(scalar keys %_isp_h)); }
421
if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_isp_h)); }
414
423
print HISTORYTMP "\n";
415
if ($xml) { print HISTORYTMP "<section id='plugin_geoip_isp_maxmind'><sortfor>$MAXNBOFSECTIONGIR</sortfor><comment>\n"; }
424
if ($xml) { print HISTORYTMP "<section id='plugin_$PluginName'><sortfor>$MAXNBOFSECTIONGIR</sortfor><comment>\n"; }
416
425
print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n";
417
426
#print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n";
418
$ValueInFile{'plugin_geoip_isp_maxmind'}=tell HISTORYTMP;
419
print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_geoip_isp_maxmind${xmlbs}".(scalar keys %_isp_h)."${xmlbe}\n";
427
$ValueInFile{"plugin_$PluginName"}=tell HISTORYTMP;
428
print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_isp_h)."${xmlbe}\n";
420
429
&BuildKeyList($MAXNBOFSECTIONGIR,1,\%_isp_h,\%_isp_h);
421
430
my %keysinkeylist=();
422
431
foreach (@keylist) {
433
442
#my $lastaccess=$_isp_l{$_}||'';
434
443
print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_isp_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next;
436
print HISTORYTMP "${xmleb}END_PLUGIN_geoip_isp_maxmind${xmlee}\n";
445
print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n";
450
#-----------------------------------------------------------------------------
451
# PLUGIN FUNCTION: LoadOverrideFile
452
# Attempts to load a comma delimited file that will override the GeoIP database
453
# Useful for Intranet records
454
# CSV format: IP,"isp"
455
#-----------------------------------------------------------------------------
456
sub LoadOverrideFile_geoip_isp_maxmind{
459
if (!open(GEOIPFILE, $OverrideFile)){
460
debug("Plugin $PluginName: Unable to open override file: $OverrideFile");
465
my $conf = (exists(&Get_Config_Name) ? Get_Config_Name() : $SiteConfig);
466
if ($conf && open(GEOIPFILE,"$DirData/$PluginName.$conf.txt")) { $filetoload="$DirData/$PluginName.$conf.txt"; }
467
elsif (open(GEOIPFILE,"$DirData/$PluginName.txt")) { $filetoload="$DirData/$PluginName.txt"; }
468
else { debug("No override file \"$DirData/$PluginName.txt\": $!"); }
472
# This is the fastest way to load with regexp that I know
476
my @record = split(",", $_);
477
# replace quotes if they were used in the file
478
foreach (@record){ $_ =~ s/"//g; }
480
$TmpDomainLookup{$record[0]} = $record[1];
483
debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found.");
489
#-----------------------------------------------------------------------------
490
# PLUGIN FUNCTION: TmpLookup
491
# Searches the temporary hash for the parameter value and returns the corresponding
493
#-----------------------------------------------------------------------------
494
sub TmpLookup_geoip_isp_maxmind(){
496
if (!$LoadedOverride){&LoadOverrideFile_geoip_isp_maxmind();}
498
#if ($geoip_isp_maxmind &&
499
#(($type eq 'geoip' && $geoip_isp_maxmind->VERSION >= 1.30) ||
500
# $type eq 'geoippureperl' && $geoip_isp_maxmind->VERSION >= 1.17)){
501
# $val = $TmpDomainLookup{$geoip_isp_maxmind->get_ip_address($param)};
503
#else {$val = $TmpDomainLookup{$param};}
505
return $TmpDomainLookup{$param}||'';
443
508
1; # Do not remove this line