152
155
# ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
153
debug(" Plugin geoip_region_maxmind: InitParams=$InitParams",1);
154
my ($mode,$datafile)=split(/\s+/,$InitParams,2);
156
debug(" Plugin $PluginName: InitParams=$InitParams",1);
157
my ($mode,$tmpdatafile)=split(/\s+/,$InitParams,2);
158
my ($datafile,$override)=split(/\+/,$tmpdatafile,2);
155
159
if (! $datafile) { $datafile="GeoIPRegion.dat"; }
160
else { $datafile =~ s/%20/ /g; }
156
161
if ($type eq 'geoippureperl') {
157
# With pureperl with always use GEOIP_STANDARD.
162
# With pureperl we always use GEOIP_STANDARD.
158
163
# GEOIP_MEMORY_CACHE seems to fail with ActiveState
159
164
if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); }
160
165
else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); }
162
167
if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
163
168
else { $mode=Geo::IP::GEOIP_STANDARD(); }
170
if ($override){ $override =~ s/%20/ /g; $OverrideFile=$override; }
165
171
%TmpDomainLookup=();
166
debug(" Plugin geoip_region_maxmind: GeoIP initialized type=$type mode=$mode",1);
172
debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode",1);
167
173
if ($type eq 'geoippureperl') {
168
174
$geoip_region_maxmind = Geo::IP::PurePerl->open($datafile, $mode);
170
176
$geoip_region_maxmind = Geo::IP->open($datafile, $mode);
172
179
# Fails with some geoip versions
173
180
# debug(" Plugin geoip_region_maxmind: GeoIP initialized database_info=".$geoip_region_maxmind->database_info());
187
194
my $menulink=$_[2];
188
195
my $menutext=$_[3];
190
if ($Debug) { debug(" Plugin geoip_region_maxmind: AddHTMLMenuLink"); }
197
if ($Debug) { debug(" Plugin $PluginName: AddHTMLMenuLink"); }
191
198
if ($categ eq 'who') {
192
$menu->{'plugin_geoip_region_maxmind'}=2.1; # Pos
193
$menulink->{'plugin_geoip_region_maxmind'}=2; # Type of link
194
$menutext->{'plugin_geoip_region_maxmind'}="Regions"; # Text
199
$menu->{"plugin_$PluginName"}=2.1; # Pos
200
$menulink->{"plugin_$PluginName"}=2; # Type of link
201
$menutext->{"plugin_$PluginName"}="Regions"; # Text
213
220
my $total_p; my $total_h; my $total_k;
214
221
my $rest_p; my $rest_h; my $rest_k;
216
if ($Debug) { debug(" Plugin geoip_region_maxmind: AddHTMLGraph"); }
223
if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph"); }
217
224
my $title='Regions';
218
225
&tab_head("$title",19,0,'regions');
219
226
print "<tr bgcolor=\"#$color_TableBGRowTitle\"><th>US and CA Regions : ".((scalar keys %_region_h)-($_region_h{'unknown'}?1:0))."</th>";
246
253
if ($TotalPages) { $p_p=int($_region_p{$key}/$TotalPages*1000)/10; }
247
254
if ($TotalHits) { $p_h=int($_region_h{$key}/$TotalHits*1000)/10; }
248
255
print "<tr><td class=\"aws\">".$region{$countrycode}{uc($regioncode)}." ($regioncode)</td>";
249
if ($ShowRegions =~ /P/i) { print "<td>".($_region_p{$key}?$_region_p{$key}:" ")."</td>"; }
256
if ($ShowRegions =~ /P/i) { print "<td>".($_region_p{$key}?Format_Number($_region_p{$key}):" ")."</td>"; }
250
257
if ($ShowRegions =~ /P/i) { print "<td>".($_region_p{$key}?"$p_p %":' ')."</td>"; }
251
if ($ShowRegions =~ /H/i) { print "<td>".($_region_h{$key}?$_region_h{$key}:" ")."</td>"; }
258
if ($ShowRegions =~ /H/i) { print "<td>".($_region_h{$key}?Format_Number($_region_h{$key}):" ")."</td>"; }
252
259
if ($ShowRegions =~ /H/i) { print "<td>".($_region_h{$key}?"$p_h %":' ')."</td>"; }
253
260
if ($ShowRegions =~ /B/i) { print "<td>".Format_Bytes($_region_k{$key})."</td>"; }
254
261
if ($ShowRegions =~ /L/i) { print "<td>".($_region_p{$key}?Format_Date($_region_l{$key},1):'-')."</td>"; }
277
284
if ($TotalPages) { $p_p=int($rest_p/$TotalPages*1000)/10; }
278
285
if ($TotalHits) { $p_h=int($rest_h/$TotalHits*1000)/10; }
279
286
print "<tr><td class=\"aws\"><span style=\"color: #$color_other\">$Message[2]/$Message[0]</span></td>";
280
if ($ShowRegions =~ /P/i) { print "<td>".($rest_p?$rest_p:" ")."</td>"; }
287
if ($ShowRegions =~ /P/i) { print "<td>".($rest_p?Format_Number($rest_p):" ")."</td>"; }
281
288
if ($ShowRegions =~ /P/i) { print "<td>".($rest_p?"$p_p %":' ')."</td>"; }
282
if ($ShowRegions =~ /H/i) { print "<td>".($rest_h?$rest_h:" ")."</td>"; }
289
if ($ShowRegions =~ /H/i) { print "<td>".($rest_h?Format_Number($rest_h):" ")."</td>"; }
283
290
if ($ShowRegions =~ /H/i) { print "<td>".($rest_h?"$p_h %":' ')."</td>"; }
284
291
if ($ShowRegions =~ /B/i) { print "<td>".Format_Bytes($rest_k)."</td>"; }
285
292
if ($ShowRegions =~ /L/i) { print "<td> </td>"; }
301
308
sub GetCountryCodeByAddr_geoip_region_maxmind {
302
309
my $param="$_[0]";
311
if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();}
304
312
my $res=$TmpDomainLookup{$param}||'';
306
314
my ($res1,$res2,$countryregion)=();
307
315
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
308
316
$res=lc($res1) || 'unknown';
309
317
$TmpDomainLookup{$param}=$res;
310
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: [$res]",5); }
318
if ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByAddr for $param: [$res]",5); }
312
elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
320
elsif ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
324
332
sub GetCountryCodeByName_geoip_region_maxmind {
325
333
my $param="$_[0]";
335
if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();}
327
336
my $res=$TmpDomainLookup{$param}||'';
329
338
my ($res1,$res2,$countryregion)=();
330
339
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
331
340
$res=lc($res1) || 'unknown';
332
341
$TmpDomainLookup{$param}=$res;
333
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: [$res]",5); }
342
if ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByName for $param: [$res]",5); }
335
elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
344
elsif ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
367
376
if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
369
378
print "<th width=\"80\">";
370
print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_geoip_region_maxmind"):"$PROG$StaticLinks.plugin_geoip_region_maxmind.$StaticExt")."\"$NewLinkTarget>GeoIP<br />Region</a>";
379
print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_$PluginName"):"$StaticLinks.plugin_$PluginName.$StaticExt")."\"$NewLinkTarget>GeoIP<br />Region</a>";
383
# try loading our override file if we haven't yet
384
if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();}
376
387
if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address
385
396
if ($key && $ip==4) {
386
397
my ($res1,$res2,$countryregion)=();
387
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
388
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByIp for $param: [${res1}_${res2}]",5); }
398
my @res = TmpLookup_geoip_region_maxmind($param);
403
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
405
if ($Debug) { debug(" Plugin $PluginName: GetRegionByIp for $param: [${res1}_${res2}]",5); }
389
406
if (! $PluginsLoaded{'init'}{'geoip'}) {
391
408
if ($res1 =~ /\w\w/) { print $DomainsHashIDLib{lc($res1)}||uc($res1); }
412
429
my ($res1,$res2,$countryregion)=();
413
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
414
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByName for $param: [${res1}_${res2}]",5); }
430
my @res = TmpLookup_geoip_region_maxmind($param);
435
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
437
if ($Debug) { debug(" Plugin $PluginName: GetRegionByName for $param: [${res1}_${res2}]",5); }
415
438
if (! $PluginsLoaded{'init'}{'geoip'}) {
417
440
if ($res1 =~ /\w\w/) { print $DomainsHashIDLib{lc($res1)}||uc($res1); }
463
486
my $param="$_[0]"; # Param must be an IP
465
488
my ($res1,$res2,$countryregion)=();
466
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
467
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByIp for $param: [${res1}_${res2}]",5); }
489
my @res = TmpLookup_geoip_region_maxmind($param);
494
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
496
if ($Debug) { debug(" Plugin $PluginName: GetRegionByIp for $param: [${res1}_${res2}]",5); }
468
497
if ($res2 =~ /\w\w/) { $countryregion=lc("${res1}_${res2}"); }
469
498
else { $countryregion='unknown'; }
470
499
# if ($PageBool) { $_region_p{$countryregion}++; }
482
511
my $param="$_[0]"; # Param must be a hostname
484
513
my ($res1,$res2,$countryregion)=();
485
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
486
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByName for $param: [${res1}_${res2}]",5); }
514
my @res = TmpLookup_geoip_region_maxmind($param);
519
($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
521
if ($Debug) { debug(" Plugin $PluginName: GetRegionByName for $param: [${res1}_${res2}]",5); }
487
522
if ($res2 =~ /\w\w/) { $countryregion=lc("${res1}_${res2}"); }
488
523
else { $countryregion='unknown'; }
489
524
# if ($PageBool) { $_region_p{$countryregion}++; }
520
555
@field=split(/\s+/,($xmlold?XMLDecodeFromHisto($_):$_));
523
until ($field[0] eq 'END_PLUGIN_geoip_region_maxmind' || $field[0] eq "${xmleb}END_PLUGIN_geoip_region_maxmind" || ! $_);
524
if ($field[0] ne 'END_PLUGIN_geoip_region_maxmind' && $field[0] ne "${xmleb}END_PLUGIN_geoip_region_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); }
525
if ($Debug) { debug(" Plugin geoip_region_maxmind: End of PLUGIN_geoip_region_maxmind section ($count entries, $countloaded loaded)"); }
558
until ($field[0] eq "END_PLUGIN_$PluginName" || $field[0] eq "${xmleb}END_PLUGIN_$PluginName" || ! $_);
559
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); }
560
if ($Debug) { debug(" Plugin $PluginName: End of PLUGIN_$PluginName section ($count entries, $countloaded loaded)"); }
533
568
#-----------------------------------------------------------------------------
534
569
sub SectionWriteHistory_geoip_region_maxmind {
535
570
my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift);
536
if ($Debug) { debug(" Plugin geoip_region_maxmind: SectionWriteHistory_geoip_region_maxmind start - ".(scalar keys %_region_h)); }
571
if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_region_h)); }
538
573
print HISTORYTMP "\n";
539
if ($xml) { print HISTORYTMP "<section id='plugin_geoip_region_maxmind'><sortfor>$MAXNBOFSECTIONGIR</sortfor><comment>\n"; }
574
if ($xml) { print HISTORYTMP "<section id='plugin_$PluginName'><sortfor>$MAXNBOFSECTIONGIR</sortfor><comment>\n"; }
540
575
print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n";
541
576
#print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n";
542
$ValueInFile{'plugin_geoip_region_maxmind'}=tell HISTORYTMP;
543
print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_geoip_region_maxmind${xmlbs}".(scalar keys %_region_h)."${xmlbe}\n";
577
$ValueInFile{"plugin_$PluginName"}=tell HISTORYTMP;
578
print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_region_h)."${xmlbe}\n";
544
579
&BuildKeyList($MAXNBOFSECTIONGIR,1,\%_region_h,\%_region_h);
545
580
my %keysinkeylist=();
546
581
foreach (@keylist) {
557
592
#my $lastaccess=$_region_l{$_}||'';
558
593
print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_region_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next;
560
print HISTORYTMP "${xmleb}END_PLUGIN_geoip_region_maxmind${xmlee}\n";
595
print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n";
600
#-----------------------------------------------------------------------------
601
# PLUGIN FUNCTION: LoadOverrideFile
602
# Attempts to load a comma delimited file that will override the GeoIP database
603
# Useful for Intranet records
604
# CSV format: IP,2-char Country code, region
605
#-----------------------------------------------------------------------------
606
sub LoadOverrideFile_geoip_region_maxmind{
609
if (!open(GEOIPFILE, $OverrideFile)){
610
debug("Plugin $PluginName: Unable to open override file: $OverrideFile");
615
my $conf = (exists(&Get_Config_Name) ? Get_Config_Name() : $SiteConfig);
616
if ($conf && open(GEOIPFILE,"$DirData/$PluginName.$conf.txt")) { $filetoload="$DirData/$PluginName.$conf.txt"; }
617
elsif (open(GEOIPFILE,"$DirData/$PluginName.txt")) { $filetoload="$DirData/$PluginName.txt"; }
618
else { debug("No override file \"$DirData/$PluginName.txt\": $!"); }
622
# This is the fastest way to load with regexp that I know
626
my @record = split(",", $_);
627
# replace quotes if they were used in the file
628
foreach (@record){ $_ =~ s/"//g; }
629
# now we need to copy our file values in the order to mimic the lookup values
631
$res[0] = $record[1]; # country code
632
$res[1] = $record[2]; # region code
634
$TmpDomainLookup{$record[0]} = [@res];
637
debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found.");
643
#-----------------------------------------------------------------------------
644
# PLUGIN FUNCTION: TmpLookup
645
# Searches the temporary hash for the parameter value and returns the corresponding
647
#-----------------------------------------------------------------------------
648
sub TmpLookup_geoip_region_maxmind(){
650
if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();}
652
#if ($geoip_region_maxmind &&
653
#(($type eq 'geoip' && $geoip_region_maxmind->VERSION >= 1.30) ||
654
# $type eq 'geoippureperl' && $geoip_region_maxmind->VERSION >= 1.17)){
655
# @val = @{$TmpDomainLookup{$geoip_region_maxmind->get_ip_address($param)}};
657
#else {@val = @{$TmpDomainLookup{$param};}}
659
if ($TmpDomainLookup{$param}) { return @{$TmpDomainLookup{$param};} }
567
663
1; # Do not remove this line