vadvs (vadvs) wrote in changelog,
vadvs
vadvs
changelog

[ljcom] r10905: LJSUP-9611: Check format of IP provided ...

Committer: vsukhanov
LJSUP-9611: Check format of IP provided to IpMap->Resolve method.
U   trunk/cgi-bin/LJ/GeoLocation.pm
A   trunk/cgi-bin/LJ/IpMap.pm
Modified: trunk/cgi-bin/LJ/GeoLocation.pm
===================================================================
--- trunk/cgi-bin/LJ/GeoLocation.pm	2011-08-23 09:45:22 UTC (rev 10904)
+++ trunk/cgi-bin/LJ/GeoLocation.pm	2011-08-24 03:00:14 UTC (rev 10905)
@@ -4,7 +4,7 @@
 use warnings;
 
 use Encode qw//;
-require 'IpMap.pm' if $LJ::USE_IPMAP;
+use LJ::IpMap;
 
 ##
 ## Usage:
@@ -43,7 +43,7 @@
     return unless $LJ::USE_IPMAP;
     my $ipmap = $LJ::IPMAP_INSTANCE;
     unless (defined $ipmap) {
-        $ipmap = IpMap->new();
+        $ipmap = LJ::IpMap->new();
         if ($IpMap::VERSION ge "1.1.0") {
             $ipmap->LoadFromBinary("$LJ::HOME/bin/upgrading/ipmap_lj.bin");
         } else {

Added: trunk/cgi-bin/LJ/IpMap.pm
===================================================================
--- trunk/cgi-bin/LJ/IpMap.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/IpMap.pm	2011-08-24 03:00:14 UTC (rev 10905)
@@ -0,0 +1,35 @@
+package LJ::IpMap;
+use strict;
+use base 'IpMap';
+
+sub Resolve {
+    my $self = shift;
+    my $ip   = shift;
+    
+    return undef unless is_ipv4($ip);
+    return $self->SUPER::Resolve($ip);
+}
+
+sub ResolveCity {
+    my $self = shift;
+    my $ip   = shift;
+
+    return undef unless is_ipv4($ip);
+    return $self->SUPER::ResolveCity($ip);
+}
+
+sub is_ipv4 {
+    my $value = shift;
+    return unless $value;
+        
+    my (@octets) = $value =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
+    return unless @octets == 4;
+    foreach my $octet (@octets) {
+        return unless ($octet >= 0 && $octet <= 255 && $octet !~ /^0\d{1,2}$/);
+    }
+
+    return 1;
+}
+
+1;
+

Tags: ljcom, pm, vadvs
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments