| 1 | package IP::Country::MaxMind; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Socket qw ( inet_ntoa ); | ||
| 5 | |||
| 6 | use vars qw ( @ISA $VERSION @EXPORT ); | ||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | $VERSION = 1.0; | ||
| 10 | |||
| 11 | @ISA = qw( Exporter ); | ||
| 12 | @EXPORT = qw( GEOIP_STANDARD GEOIP_MEMORY_CACHE GEOIP_CHECK_CACHE ); | ||
| 13 | |||
| 14 | my $MM; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | eval 'use Geo::IP'; | ||
| 18 | if($@){ | ||
| 19 | eval 'use Geo::IP::PurePerl'; | ||
| 20 | if($@){ | ||
| 21 | die($@); | ||
| 22 | } else { | ||
| 23 | $MM = 'Geo::IP::PurePerl'; | ||
| 24 | } | ||
| 25 | } else { | ||
| 26 | $MM = 'Geo::IP'; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | my $ip_match = qr/^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])$/o; | ||
| 31 | |||
| 32 | sub new | ||
| 33 | { | ||
| 34 | my($caller,$flags) = @_; | ||
| 35 | my $class = ref($caller) || $caller; | ||
| 36 | $flags = GEOIP_STANDARD unless (defined $flags); | ||
| 37 | my $gi; | ||
| 38 | eval "\$gi = $MM->new(\$flags)"; | ||
| 39 | die($@) if $@; | ||
| 40 | bless \$gi,$class; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub open | ||
| 44 | { | ||
| 45 | my($caller,$database_filename,$flags) = @_; | ||
| 46 | my $class = ref($caller) || $caller; | ||
| 47 | $flags = GEOIP_STANDARD unless (defined $flags); | ||
| 48 | my $gi; | ||
| 49 | eval "\$gi = $MM->open(\$database_filename,\$flags)"; | ||
| 50 | die($@) if $@; | ||
| 51 | bless \$gi,$class; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub inet_atocc | ||
| 55 | { | ||
| 56 | my ($gi,$host) = (${$_[0]},$_[1]); | ||
| 57 | if($host=~$ip_match){ | ||
| 58 | return $gi->country_code_by_addr($host); | ||
| 59 | } else { | ||
| 60 | return $gi->country_code_by_name($host); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub inet_ntocc | ||
| 65 | { | ||
| 66 | return ${$_[0]}->country_code_by_addr(inet_ntoa($_[1])); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub db_time | ||
| 70 | { | ||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | __END__ |