Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 269
- Log:
Overdue upgrade to AWStats v6.9, the most recent version
available at the time of writing.
- Author:
- rool
- Date:
- Wed May 27 23:57:15 +0100 2009
- Size:
- 7205 Bytes
- Properties:
- Property svn:executable is set
1 | #!/usr/bin/perl |
2 | #----------------------------------------------------------------------------- |
3 | # GeoIp Maxmind AWStats plugin |
4 | # This plugin allow you to get country report with countries detected |
5 | # from a Geographical database (GeoIP internal database) instead of domain |
6 | # hostname suffix. |
7 | # Need the country database from Maxmind (free). |
8 | #----------------------------------------------------------------------------- |
9 | # Perl Required Modules: Geo::IP or Geo::IP::PurePerl |
10 | #----------------------------------------------------------------------------- |
11 | # $Revision: 1.25 $ - $Author: eldy $ - $Date: 2008/11/15 16:03:42 $ |
12 | |
13 | |
14 | # <----- |
15 | # ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES |
16 | use vars qw/ $type /; |
17 | $type='geoip'; |
18 | if (!eval ('require "Geo/IP.pm";')) { |
19 | $error1=$@; |
20 | $type='geoippureperl'; |
21 | if (!eval ('require "Geo/IP/PurePerl.pm";')) { |
22 | $error2=$@; |
23 | $ret=($error1||$error2)?"Error:\n$error1$error2":""; |
24 | $ret.="Error: Need Perl module Geo::IP or Geo::IP::PurePerl"; |
25 | return $ret; |
26 | } |
27 | } |
28 | # -----> |
29 | #use strict; |
30 | no strict "refs"; |
31 | |
32 | |
33 | |
34 | #----------------------------------------------------------------------------- |
35 | # PLUGIN VARIABLES |
36 | #----------------------------------------------------------------------------- |
37 | # <----- |
38 | # ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN |
39 | # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE. |
40 | my $PluginNeedAWStatsVersion="5.4"; |
41 | my $PluginHooksFunctions="GetCountryCodeByAddr GetCountryCodeByName ShowInfoHost"; |
42 | # -----> |
43 | |
44 | # <----- |
45 | # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. |
46 | use vars qw/ |
47 | %TmpDomainLookup |
48 | $gi |
49 | /; |
50 | # -----> |
51 | |
52 | |
53 | #----------------------------------------------------------------------------- |
54 | # PLUGIN FUNCTION: Init_pluginname |
55 | #----------------------------------------------------------------------------- |
56 | sub Init_geoip { |
57 | my $InitParams=shift; |
58 | my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion); |
59 | |
60 | # <----- |
61 | # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS |
62 | debug(" Plugin geoip: InitParams=$InitParams",1); |
63 | my ($mode,$datafile)=split(/\s+/,$InitParams,2); |
64 | if (! $datafile) { $datafile="GeoIP.dat"; } |
65 | if ($type eq 'geoippureperl') { |
66 | if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::PurePerl::GEOIP_MEMORY_CACHE(); } |
67 | else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); } |
68 | } else { |
69 | if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } |
70 | else { $mode=Geo::IP::GEOIP_STANDARD(); } |
71 | } |
72 | %TmpDomainLookup=(); |
73 | debug(" Plugin geoip: GeoIP initialized type=$type mode=$mode",1); |
74 | if ($type eq 'geoippureperl') { |
75 | $gi = Geo::IP::PurePerl->open($datafile, $mode); |
76 | } else { |
77 | $gi = Geo::IP->open($datafile, $mode); |
78 | } |
79 | # Fails on some GeoIP version |
80 | # debug(" Plugin geoip: GeoIP initialized database_info=".$gi->database_info()); |
81 | # -----> |
82 | |
83 | return ($checkversion?$checkversion:"$PluginHooksFunctions"); |
84 | } |
85 | |
86 | |
87 | #----------------------------------------------------------------------------- |
88 | # PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname |
89 | # UNIQUE: YES (Only one plugin using this function can be loaded) |
90 | # GetCountryCodeByAddr is called to translate an ip into a country code in lower case. |
91 | #----------------------------------------------------------------------------- |
92 | sub GetCountryCodeByAddr_geoip { |
93 | my $param="$_[0]"; |
94 | # <----- |
95 | my $res=$TmpDomainLookup{$param}||''; |
96 | if (! $res) { |
97 | $res=lc($gi->country_code_by_addr($param)) || 'unknown'; |
98 | $TmpDomainLookup{$param}=$res; |
99 | if ($Debug) { debug(" Plugin geoip: GetCountryCodeByAddr for $param: [$res]",5); } |
100 | } |
101 | elsif ($Debug) { debug(" Plugin geoip: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); } |
102 | # -----> |
103 | return $res; |
104 | } |
105 | |
106 | |
107 | #----------------------------------------------------------------------------- |
108 | # PLUGIN FUNCTION: GetCountryCodeByName_pluginname |
109 | # UNIQUE: YES (Only one plugin using this function can be loaded) |
110 | # GetCountryCodeByName is called to translate a host name into a country code in lower case. |
111 | #----------------------------------------------------------------------------- |
112 | sub GetCountryCodeByName_geoip { |
113 | my $param="$_[0]"; |
114 | # <----- |
115 | my $res=$TmpDomainLookup{$param}||''; |
116 | if (! $res) { |
117 | $res=lc($gi->country_code_by_name($param)) || 'unknown'; |
118 | $TmpDomainLookup{$param}=$res; |
119 | if ($Debug) { debug(" Plugin geoip: GetCountryCodeByName for $param: [$res]",5); } |
120 | } |
121 | elsif ($Debug) { debug(" Plugin geoip: GetCountryCodeByName for $param: Already resolved to [$res]",5); } |
122 | # -----> |
123 | return $res; |
124 | } |
125 | |
126 | |
127 | #----------------------------------------------------------------------------- |
128 | # PLUGIN FUNCTION: ShowInfoHost_pluginname |
129 | # UNIQUE: NO (Several plugins using this function can be loaded) |
130 | # Function called to add additionnal columns to the Hosts report. |
131 | # This function is called when building rows of the report (One call for each |
132 | # row). So it allows you to add a column in report, for example with code : |
133 | # print "<TD>This is a new cell for $param</TD>"; |
134 | # Parameters: Host name or ip |
135 | #----------------------------------------------------------------------------- |
136 | sub ShowInfoHost_geoip { |
137 | my $param="$_[0]"; |
138 | # <----- |
139 | if ($param eq '__title__') { |
140 | my $NewLinkParams=${QueryString}; |
141 | $NewLinkParams =~ s/(^|&)update(=\w*|$)//i; |
142 | $NewLinkParams =~ s/(^|&)output(=\w*|$)//i; |
143 | $NewLinkParams =~ s/(^|&)staticlinks(=\w*|$)//i; |
144 | $NewLinkParams =~ s/(^|&)framename=[^&]*//i; |
145 | my $NewLinkTarget=''; |
146 | if ($DetailedReportsOnNewWindows) { $NewLinkTarget=" target=\"awstatsbis\""; } |
147 | if (($FrameName eq 'mainleft' || $FrameName eq 'mainright') && $DetailedReportsOnNewWindows < 2) { |
148 | $NewLinkParams.="&framename=mainright"; |
149 | $NewLinkTarget=" target=\"mainright\""; |
150 | } |
151 | $NewLinkParams =~ tr/&/&/s; $NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//; |
152 | if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; } |
153 | |
154 | print "<th width=\"80\">"; |
155 | print "<a href=\"#countries\">GeoIP<br />Country</a>"; |
156 | print "</th>"; |
157 | } |
158 | elsif ($param) { |
159 | my $ip=0; |
160 | my $key; |
161 | if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address |
162 | $ip=4; |
163 | $key=$param; |
164 | } |
165 | elsif ($param =~ /^[0-9A-F]*:/i) { # IPv6 address |
166 | $ip=6; |
167 | $key=$param; |
168 | } |
169 | print "<td>"; |
170 | if ($key && $ip==4) { |
171 | my $res=lc($gi->country_code_by_addr($param)) if $gi; |
172 | if ($Debug) { debug(" Plugin geoip: GetCountryByIp for $param: [$res]",5); } |
173 | if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"<span style=\"color: #$color_other\">$Message[0]</span>"; } |
174 | else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; } |
175 | } |
176 | if ($key && $ip==6) { |
177 | print "<span style=\"color: #$color_other\">$Message[0]</span>"; |
178 | } |
179 | if (! $key) { |
180 | my $res=lc($gi->country_code_by_name($param)) if $gi; |
181 | if ($Debug) { debug(" Plugin geoip: GetCountryByHostname for $param: [$res]",5); } |
182 | if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"<span style=\"color: #$color_other\">$Message[0]</span>"; } |
183 | else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; } |
184 | } |
185 | print "</td>"; |
186 | } |
187 | else { |
188 | print "<td> </td>"; |
189 | } |
190 | return 1; |
191 | # -----> |
192 | } |
193 | |
194 | |
195 | 1; # Do not remove this line |