~ubuntu-branches/ubuntu/quantal/awstats/quantal

« back to all changes in this revision

Viewing changes to wwwroot/cgi-bin/plugins/geoip_isp_maxmind.pm

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard, Sergey B Kirpichev, Jonas Smedegaard
  • Date: 2011-03-12 19:44:25 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20110312194425-hn7bo427pgqw2smz
Tags: 7.0~dfsg-1
* New upstream release.
  Closes: bug#613447.

[ Sergey B Kirpichev ]
* Unfuzz patches.
* Update patch 1008.
* Drop obsolete patches.
* Fix +x bit on *.js in docs/examples
* Avoid asterisks in debian/NEWS entries, to please lintian.
* Fix recode bulgarian tooltips file as utf-8.
  Closes: bug#610632.
* Forward patches 0007 and 1015 upstream.
* Allow DM (Debian Maintainer) uploads.

[ Jonas Smedegaard ]
* Update copyright file:
  + Rewrite using Subversion rev.173 of draft DEP5 format.
  + Add some previously missed authors and licenses.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 $
10
10
 
11
11
 
12
12
# <-----
37
37
# AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
38
38
my $PluginNeedAWStatsVersion="6.2";
39
39
my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory";
 
40
my $PluginName="geoip_isp_maxmind";
 
41
my $LoadedOverride=0;
 
42
my $OverrideFile="";
 
43
my %TmpDomainLookup; 
40
44
# ----->
41
45
 
42
46
# <-----
64
68
 
65
69
        # <-----
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(); }
78
84
        }
79
 
        debug(" Plugin geoip_isp_maxmind: GeoIP initialized type=$type mode=$mode",1);
 
85
        if ($override){ $override =~ s/%20/ /g; $OverrideFile=$override; }
 
86
        %TmpDomainLookup=();
 
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);
82
90
        } else {
83
91
                $geoip_isp_maxmind = Geo::IP->open($datafile, $mode);
84
92
        }
 
93
        $LoadedOverride=0;
85
94
# Fails on some GeoIP version
86
95
#       debug(" Plugin geoip_isp_maxmind: GeoIP initialized database_info=".$geoip_isp_maxmind->database_info());
87
96
        # ----->
100
109
    my $menulink=$_[2];
101
110
    my $menutext=$_[3];
102
111
        # <-----
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
108
117
    }
109
118
        # ----->
110
119
        return 0;
126
135
        my $total_p; my $total_h; my $total_k;
127
136
        my $rest_p; my $rest_h; my $rest_k;
128
137
 
129
 
        if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLGraph $categ $menu $menulink $menutext"); }
 
138
        if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph $categ $menu $menulink $menutext"); }
130
139
        my $title='ISP';
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>";
148
157
                    print "<tr>";
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}:"&nbsp;")."</td>"; }
 
160
                if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?Format_Number($_isp_p{$key}):"&nbsp;")."</td>"; }
152
161
                if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?"$p_p %":'&nbsp;')."</td>"; }
153
 
                if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?$_isp_h{$key}:"&nbsp;")."</td>"; }
 
162
                if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?Format_Number($_isp_h{$key}):"&nbsp;")."</td>"; }
154
163
                if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?"$p_h %":'&nbsp;')."</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; }
181
190
                print "<tr>";
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:"&nbsp;")."</td>"; }
 
192
                if ($ShowISP =~ /P/i) { print "<td>".($rest_p?Format_Number($rest_p):"&nbsp;")."</td>"; }
184
193
                if ($ShowISP =~ /P/i) { print "<td>".($rest_p?"$p_p %":'&nbsp;')."</td>"; }
185
 
                if ($ShowISP =~ /H/i) { print "<td>".($rest_h?$rest_h:"&nbsp;")."</td>"; }
 
194
                if ($ShowISP =~ /H/i) { print "<td>".($rest_h?Format_Number($rest_h):"&nbsp;")."</td>"; }
186
195
                if ($ShowISP =~ /H/i) { print "<td>".($rest_h?"$p_h %":'&nbsp;')."</td>"; }
187
196
                if ($ShowISP =~ /B/i) { print "<td>".Format_Bytes($rest_k)."</td>"; }
188
197
                if ($ShowISP =~ /L/i) { print "<td>&nbsp;</td>"; }
224
233
        if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
225
234
 
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>";
228
237
        print "</th>";
229
238
        }
230
239
        elsif ($param) {
240
249
                }
241
250
                print "<td>";
242
251
                if ($key && $ip==4) {
243
 
                my $isp;
244
 
                if ($type eq 'geoippureperl')
 
252
                my $isp = TmpLookup_geoip_isp_maxmind($param);
 
253
                if (!$isp && $type eq 'geoippureperl')
245
254
                        {
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;
248
257
                }
249
 
                else
 
258
                elsif (!$isp)
250
259
                {
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;
253
262
                }
254
 
                if ($Debug) { debug("  Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); }
 
263
                if ($Debug) { debug("  Plugin $PluginName: GetIspByIp for $param: [$isp]",5); }
255
264
                    if ($isp) {
256
265
                        if (length($isp) <= $MAXLENGTH) {
257
266
                            print "$isp";
266
275
                    print "<span style=\"color: #$color_other\">$Message[0]</span>";
267
276
                }
268
277
                if (! $key) {
269
 
                my $isp;
270
 
                if ($type eq 'geoippureperl')
 
278
                my $isp = TmpLookup_geoip_isp_maxmind($param);
 
279
                if (!$isp && $type eq 'geoippureperl')
271
280
                        {
272
281
                        $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
273
282
                }
274
 
                else
 
283
                elsif (!$isp)
275
284
                {
276
285
                        $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
277
286
                }
278
 
                if ($Debug) { debug("  Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); }
 
287
                if ($Debug) { debug("  Plugin $PluginName: GetIspByHostname for $param: [$isp]",5); }
279
288
                    if ($isp) {
280
289
                        if (length($isp) <= $MAXLENGTH) {
281
290
                            print "$isp";
303
312
sub SectionInitHashArray_geoip_isp_maxmind {
304
313
#    my $param="$_[0]";
305
314
        # <-----
306
 
        if ($Debug) { debug(" Plugin geoip_isp_maxmind: Init_HashArray"); }
 
315
        if ($Debug) { debug(" Plugin $PluginName: Init_HashArray"); }
307
316
        %_isp_p = %_isp_h = %_isp_k = %_isp_l =();
308
317
        # ----->
309
318
        return 0;
317
326
sub SectionProcessIp_geoip_isp_maxmind {
318
327
    my $param="$_[0]";      # Param must be an IP
319
328
        # <-----
320
 
        my $isp;
321
 
        if ($type eq 'geoippureperl')
 
329
        my $isp = TmpLookup_geoip_isp_maxmind($param);
 
330
        if (!$isp && $type eq 'geoippureperl')
322
331
        {
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;
325
334
        }
326
 
        else
 
335
        elsif(!$isp)
327
336
        {
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;
330
339
        }
331
 
        if ($Debug) { debug("  Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); }
 
340
        if ($Debug) { debug("  Plugin $PluginName: GetIspByIp for $param: [$isp]",5); }
332
341
    if ($isp) {
333
342
        $isp =~ s/\s/_/g;
334
343
        $_isp_h{$isp}++;
348
357
sub SectionProcessHostname_geoip_isp_maxmind {
349
358
    my $param="$_[0]";      # Param must be an IP
350
359
        # <-----
351
 
        my $isp;
352
 
        if ($type eq 'geoippureperl')
353
 
        {
354
 
                $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
355
 
        }
356
 
        else
357
 
        {
358
 
                $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
359
 
        }
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')
 
362
        {
 
363
                $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
 
364
        }
 
365
        elsif(!$isp)
 
366
        {
 
367
                $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind;
 
368
        }
 
369
        if ($Debug) { debug("  Plugin $PluginName: GetIspByHostname for $param: [$isp]",5); }
361
370
    if ($isp) {
362
371
        $isp =~ s/\s/_/g;
363
372
        $_isp_h{$isp}++;
380
389
    my $xmleb=shift;
381
390
        my $countlines=shift;
382
391
        # <-----
383
 
        if ($Debug) { debug(" Plugin geoip_isp_maxmind: Begin of PLUGIN_geoip_isp_maxmind section"); }
 
392
        if ($Debug) { debug(" Plugin $PluginName: Begin of PLUGIN_$PluginName section"); }
384
393
        my @field=();
385
394
        my $count=0;my $countloaded=0;
386
395
        do {
396
405
                @field=split(/\s+/,($xmlold?XMLDecodeFromHisto($_):$_));
397
406
                $countlines++;
398
407
        }
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)"); }
402
411
        # ----->
403
412
        return 0;
404
413
}
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)); }
413
422
        # <-----
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;
435
444
        }
436
 
        print HISTORYTMP "${xmleb}END_PLUGIN_geoip_isp_maxmind${xmlee}\n";
 
445
        print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n";
437
446
        # ----->
438
447
        return 0;
439
448
}
440
449
 
 
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{
 
457
        my $filetoload="";
 
458
        if ($OverrideFile){
 
459
                if (!open(GEOIPFILE, $OverrideFile)){
 
460
                        debug("Plugin $PluginName: Unable to open override file: $OverrideFile");
 
461
                        $LoadedOverride = 1;
 
462
                        return;
 
463
                }
 
464
        }else{
 
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\": $!"); }
 
469
        }
 
470
        if ($filetoload)
 
471
        {
 
472
                # This is the fastest way to load with regexp that I know
 
473
                while (<GEOIPFILE>){
 
474
                        chomp $_;
 
475
                        s/\r//;
 
476
                        my @record = split(",", $_);
 
477
                        # replace quotes if they were used in the file
 
478
                        foreach (@record){ $_ =~ s/"//g; }
 
479
                        # store in hash
 
480
                        $TmpDomainLookup{$record[0]} = $record[1];
 
481
                }
 
482
                close GEOIPFILE;
 
483
        debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found.");
 
484
        }
 
485
        $LoadedOverride = 1;
 
486
        return;
 
487
}
441
488
 
 
489
#-----------------------------------------------------------------------------
 
490
# PLUGIN FUNCTION: TmpLookup
 
491
# Searches the temporary hash for the parameter value and returns the corresponding
 
492
# GEOIP entry
 
493
#-----------------------------------------------------------------------------
 
494
sub TmpLookup_geoip_isp_maxmind(){
 
495
        $param = shift;
 
496
        if (!$LoadedOverride){&LoadOverrideFile_geoip_isp_maxmind();}
 
497
        #my $val;
 
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)};
 
502
        #}
 
503
    #else {$val = $TmpDomainLookup{$param};}
 
504
    #return $val || '';
 
505
    return $TmpDomainLookup{$param}||'';
 
506
}
442
507
 
443
508
1;      # Do not remove this line