home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / cgi-bin / geoip.pm < prev    next >
Encoding:
Text File  |  2004-05-20  |  4.0 KB  |  116 lines

  1. #!/usr/bin/perl
  2. #-----------------------------------------------------------------------------
  3. # GeoIp AWStats plugin
  4. # This plugin allow you to get AWStats country report with countries detected
  5. # from a Geographical database (GeoIP internal database) instead of domain
  6. # hostname suffix.
  7. #-----------------------------------------------------------------------------
  8. # Perl Required Modules: Geo::IP or Geo::IP::PurePerl
  9. #-----------------------------------------------------------------------------
  10. # $Revision: 1.4 $ - $Author: joker $ - $Date: 2004/05/20 20:38:42 $
  11.  
  12.  
  13. # <-----
  14. # ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES
  15. use vars qw/ $type /;
  16. $type='geoip';
  17. if (!eval ('require "Geo/IP.pm";'))     {
  18.     $type='geoippureperl';
  19.     if (!eval ('require "Geo/IP/PurePerl.pm";')) { return $@?"Error: $@":"Error: Need Perl module Geo::IP or Geo::IP::PurePerl"; }
  20. }
  21. # ----->
  22. use strict;no strict "refs";
  23.  
  24.  
  25.  
  26. #-----------------------------------------------------------------------------
  27. # PLUGIN VARIABLES
  28. #-----------------------------------------------------------------------------
  29. # <-----
  30. # ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
  31. # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
  32. my $PluginNeedAWStatsVersion="5.4";
  33. my $PluginHooksFunctions="GetCountryCodeByAddr GetCountryCodeByName";
  34. # ----->
  35.  
  36. # <-----
  37. # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
  38. use vars qw/
  39. %TmpDomainLookup
  40. $gi
  41. /;
  42. # ----->
  43.  
  44.  
  45. #-----------------------------------------------------------------------------
  46. # PLUGIN FUNCTION: Init_pluginname
  47. #-----------------------------------------------------------------------------
  48. sub Init_geoip {
  49.     my $InitParams=shift;
  50.     my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion);
  51.  
  52.     # <-----
  53.     # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
  54.     debug(" InitParams=$InitParams",1);
  55.     my $mode=$InitParams;
  56.     if ($type eq 'geoippureperl') {
  57.         if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE')  { $mode=Geo::IP::PurePerl::GEOIP_MEMORY_CACHE(); }
  58.         else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); }
  59.     } else {
  60.         if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE')  { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
  61.         else { $mode=Geo::IP::GEOIP_STANDARD(); }
  62.     }
  63.     %TmpDomainLookup=();
  64.     debug(" GeoIP working in mode $type $mode",1);
  65.     if ($type eq 'geoippureperl') {
  66.         $gi = Geo::IP::PurePerl->new($mode);
  67.     } else {
  68.         $gi = Geo::IP->new($mode);
  69.     }
  70.     # ----->
  71.  
  72.     return ($checkversion?$checkversion:"$PluginHooksFunctions");
  73. }
  74.  
  75.  
  76. #-----------------------------------------------------------------------------
  77. # PLUGIN FUNCTION: GetCountryCodeByName_pluginname
  78. # UNIQUE: YES (Only one plugin using this function can be loaded)
  79. # GetCountryCodeByName is called to translate a host name into a country name.
  80. #-----------------------------------------------------------------------------
  81. sub GetCountryCodeByName_geoip {
  82.     my $param="$_[0]";
  83.     # <-----
  84.     my $res=$TmpDomainLookup{$param}||'';
  85.     if (! $res) {
  86.         $res=lc($gi->country_code_by_name($param));
  87.         $TmpDomainLookup{$param}=$res;
  88.         if ($Debug) { debug("  GetCountryCodeByName for $param: [$res]",5); }
  89.     }
  90.     elsif ($Debug) { debug("  GetCountryCodeByName for $param: Already resolved to $res",5); }
  91.     # ----->
  92.     return $res;
  93. }
  94.  
  95. #-----------------------------------------------------------------------------
  96. # PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname
  97. # UNIQUE: YES (Only one plugin using this function can be loaded)
  98. # GetCountryCodeByAddr is called to translate an ip into a country name.
  99. #-----------------------------------------------------------------------------
  100. sub GetCountryCodeByAddr_geoip {
  101.     my $param="$_[0]";
  102.     # <-----
  103.     my $res=$TmpDomainLookup{$param}||'';
  104.     if (! $res) {
  105.         $res=lc($gi->country_code_by_addr($param));
  106.         $TmpDomainLookup{$param}=$res;
  107.         if ($Debug) { debug("  GetCountryCodeByAddr for $param: $res",5); }
  108.     }
  109.     elsif ($Debug) { debug("  GetCountryCodeByAddr for $param: Already resolved to $res",5); }
  110.     # ----->
  111.     return $res;
  112. }
  113.  
  114.  
  115. 1;    # Do not remove this line
  116.