Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 297
- Log:
Updated to AWStats 7.0.
- Author:
- rool
- Date:
- Fri Mar 18 13:33:29 +0000 2011
- Size:
- 9736 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.31 $ - $Author: eldy $ - $Date: 2010/08/04 12:38:02 $ |
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 | my $PluginName = "geoip"; |
43 | my $LoadedOverride=0; |
44 | my $OverrideFile=""; |
45 | my %TmpDomainLookup; |
46 | # -----> |
47 | |
48 | # <----- |
49 | # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. |
50 | use vars qw/ |
51 | $gi |
52 | /; |
53 | # -----> |
54 | |
55 | |
56 | #----------------------------------------------------------------------------- |
57 | # PLUGIN FUNCTION: Init_pluginname |
58 | #----------------------------------------------------------------------------- |
59 | sub Init_geoip { |
60 | my $InitParams=shift; |
61 | my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion); |
62 | |
63 | # <----- |
64 | # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS |
65 | debug(" Plugin $PluginName: InitParams=$InitParams",1); |
66 | my ($mode,$tmpdatafile)=split(/\s+/,$InitParams,2); |
67 | my ($datafile,$override)=split(/\+/,$tmpdatafile,2); |
68 | if (! $datafile) { $datafile="$PluginName.dat"; } |
69 | else { $datafile =~ s/%20/ /g; } |
70 | if ($type eq 'geoippureperl') { |
71 | if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::PurePerl::GEOIP_MEMORY_CACHE(); } |
72 | else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); } |
73 | } else { |
74 | if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } |
75 | else { $mode=Geo::IP::GEOIP_STANDARD(); } |
76 | } |
77 | if ($override){$OverrideFile=$override;} |
78 | %TmpDomainLookup=(); |
79 | debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode override=$override",1); |
80 | if ($type eq 'geoippureperl') { |
81 | $gi = Geo::IP::PurePerl->open($datafile, $mode); |
82 | } else { |
83 | $gi = Geo::IP->open($datafile, $mode); |
84 | } |
85 | |
86 | # Fails on some GeoIP version |
87 | # debug(" Plugin geoip: GeoIP initialized database_info=".$gi->database_info()); |
88 | # -----> |
89 | |
90 | return ($checkversion?$checkversion:"$PluginHooksFunctions"); |
91 | } |
92 | |
93 | |
94 | #----------------------------------------------------------------------------- |
95 | # PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname |
96 | # UNIQUE: YES (Only one plugin using this function can be loaded) |
97 | # GetCountryCodeByAddr is called to translate an ip into a country code in lower case. |
98 | #----------------------------------------------------------------------------- |
99 | sub GetCountryCodeByAddr_geoip { |
100 | my $param="$_[0]"; |
101 | # <----- |
102 | if (! $param) { return ''; } |
103 | my $res= TmpLookup_geoip($param); |
104 | if (! $res) { |
105 | $res=lc($gi->country_code_by_addr($param)) || 'unknown'; |
106 | $TmpDomainLookup{$param}=$res; |
107 | if ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByAddr for $param: [$res]",5); } |
108 | } |
109 | elsif ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); } |
110 | # -----> |
111 | return $res; |
112 | } |
113 | |
114 | |
115 | #----------------------------------------------------------------------------- |
116 | # PLUGIN FUNCTION: GetCountryCodeByName_pluginname |
117 | # UNIQUE: YES (Only one plugin using this function can be loaded) |
118 | # GetCountryCodeByName is called to translate a host name into a country code in lower case. |
119 | #----------------------------------------------------------------------------- |
120 | sub GetCountryCodeByName_geoip { |
121 | my $param="$_[0]"; |
122 | # <----- |
123 | if (! $param) { return ''; } |
124 | my $res = TmpLookup_geoip($param); |
125 | if (! $res) { |
126 | $res=lc($gi->country_code_by_name($param)) || 'unknown'; |
127 | $TmpDomainLookup{$param}=$res; |
128 | if ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByName for $param: [$res]",5); } |
129 | } |
130 | elsif ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByName for $param: Already resolved to [$res]",5); } |
131 | # -----> |
132 | return $res; |
133 | } |
134 | |
135 | |
136 | #----------------------------------------------------------------------------- |
137 | # PLUGIN FUNCTION: ShowInfoHost_pluginname |
138 | # UNIQUE: NO (Several plugins using this function can be loaded) |
139 | # Function called to add additionnal columns to the Hosts report. |
140 | # This function is called when building rows of the report (One call for each |
141 | # row). So it allows you to add a column in report, for example with code : |
142 | # print "<TD>This is a new cell for $param</TD>"; |
143 | # Parameters: Host name or ip |
144 | #----------------------------------------------------------------------------- |
145 | sub ShowInfoHost_geoip { |
146 | my $param="$_[0]"; |
147 | # <----- |
148 | if ($param eq '__title__') { |
149 | my $NewLinkParams=${QueryString}; |
150 | $NewLinkParams =~ s/(^|&)update(=\w*|$)//i; |
151 | $NewLinkParams =~ s/(^|&)output(=\w*|$)//i; |
152 | $NewLinkParams =~ s/(^|&)staticlinks(=\w*|$)//i; |
153 | $NewLinkParams =~ s/(^|&)framename=[^&]*//i; |
154 | my $NewLinkTarget=''; |
155 | if ($DetailedReportsOnNewWindows) { $NewLinkTarget=" target=\"awstatsbis\""; } |
156 | if (($FrameName eq 'mainleft' || $FrameName eq 'mainright') && $DetailedReportsOnNewWindows < 2) { |
157 | $NewLinkParams.="&framename=mainright"; |
158 | $NewLinkTarget=" target=\"mainright\""; |
159 | } |
160 | $NewLinkParams =~ tr/&/&/s; $NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//; |
161 | if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; } |
162 | |
163 | print "<th width=\"80\">"; |
164 | print "<a href=\"#countries\">GeoIP<br />Country</a>"; |
165 | print "</th>"; |
166 | } |
167 | elsif ($param) { |
168 | my $ip=0; |
169 | my $key; |
170 | if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address |
171 | $ip=4; |
172 | $key=$param; |
173 | } |
174 | elsif ($param =~ /^[0-9A-F]*:/i) { # IPv6 address |
175 | $ip=6; |
176 | $key=$param; |
177 | } |
178 | print "<td>"; |
179 | if ($key && $ip==4) { |
180 | my $res = TmpLookup_geoip($param); |
181 | if (!$res){$res=lc($gi->country_code_by_addr($param)) if $gi;} |
182 | if ($Debug) { debug(" Plugin $PluginName: GetCountryByIp for $param: [$res]",5); } |
183 | if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"<span style=\"color: #$color_other\">$Message[0]</span>"; } |
184 | else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; } |
185 | } |
186 | if ($key && $ip==6) { |
187 | print "<span style=\"color: #$color_other\">$Message[0]</span>"; |
188 | } |
189 | if (! $key) { |
190 | my $res = TmpLookup_geoip($param); |
191 | if (!$res){$res=lc($gi->country_code_by_name($param)) if $gi;} |
192 | if ($Debug) { debug(" Plugin $PluginName: GetCountryByHostname for $param: [$res]",5); } |
193 | if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"<span style=\"color: #$color_other\">$Message[0]</span>"; } |
194 | else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; } |
195 | } |
196 | print "</td>"; |
197 | } |
198 | else { |
199 | print "<td> </td>"; |
200 | } |
201 | return 1; |
202 | # -----> |
203 | } |
204 | |
205 | #----------------------------------------------------------------------------- |
206 | # PLUGIN FUNCTION: LoadOverrideFile |
207 | # Attempts to load a comma delimited file that will override the GeoIP database |
208 | # Useful for Intranet records |
209 | # CSV format: IP,2-char Country code |
210 | #----------------------------------------------------------------------------- |
211 | sub LoadOverrideFile_geoip{ |
212 | my $filetoload=""; |
213 | if ($OverrideFile){ |
214 | if (!open(GEOIPFILE, $OverrideFile)){ |
215 | debug("Plugin $PluginName: Unable to open override file: $OverrideFile"); |
216 | $LoadedOverride = 1; |
217 | return; |
218 | } |
219 | }else{ |
220 | my $conf = (exists(&Get_Config_Name) ? Get_Config_Name() : $SiteConfig); |
221 | if ($conf && open(GEOIPFILE,"$DirData/$PluginName.$conf.txt")) { $filetoload="$DirData/$PluginName.$conf.txt"; } |
222 | elsif (open(GEOIPFILE,"$DirData/$PluginName.txt")) { $filetoload="$DirData/$PluginName.txt"; } |
223 | else { debug("No override file \"$DirData/$PluginName.txt\": $!"); } |
224 | } |
225 | if ($filetoload) |
226 | { |
227 | # This is the fastest way to load with regexp that I know |
228 | while (<GEOIPFILE>){ |
229 | chomp $_; |
230 | s/\r//; |
231 | my @record = split(",", $_); |
232 | # replace quotes if they were used in the file |
233 | foreach (@record){ $_ =~ s/"//g; } |
234 | # store in hash |
235 | $TmpDomainLookup{$record[0]} = $record[1]; |
236 | } |
237 | close GEOIPFILE; |
238 | debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found."); |
239 | } |
240 | $LoadedOverride = 1; |
241 | return; |
242 | } |
243 | |
244 | #----------------------------------------------------------------------------- |
245 | # PLUGIN FUNCTION: TmpLookup |
246 | # Searches the temporary hash for the parameter value and returns the corresponding |
247 | # GEOIP entry |
248 | #----------------------------------------------------------------------------- |
249 | sub TmpLookup_geoip(){ |
250 | $param = shift; |
251 | if (!$LoadedOverride){&LoadOverrideFile_geoip();} |
252 | #my $val; |
253 | #if ($gi && |
254 | #(($type eq 'geoip' && $gi->VERSION >= 1.30) || |
255 | # $type eq 'geoippureperl' && $gi->VERSION >= 1.17)){ |
256 | # $val = $TmpDomainLookup{$gi->get_ip_address($param)}; |
257 | #} |
258 | #else {$val = $TmpDomainLookup{$param};} |
259 | #return $val || ''; |
260 | return $TmpDomainLookup{$param}||''; |
261 | } |
262 | |
263 | 1; # Do not remove this line |