Fixed detection of IPv4 addresses embedded into IPv6
authorAlexander Ebert <ebert@woltlab.com>
Mon, 3 Jun 2013 16:29:55 +0000 (18:29 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 3 Jun 2013 16:29:55 +0000 (18:29 +0200)
wcfsetup/install/files/lib/util/UserUtil.class.php

index 116ed6ee0fc1893f31aea4003d8f02f5948a3a10..ebcb8e438869eedcdc04e01c2d3f8a22b4175096 100644 (file)
@@ -166,19 +166,26 @@ final class UserUtil {
                
                // check if ip is a masked IPv4 address
                if (substr($ip, 0, 7) == '::ffff:') {
-                       $ip = explode(':', substr($ip, 7));
-                       $ip[0] = base_convert($ip[0], 16, 10);
-                       $ip[1] = base_convert($ip[1], 16, 10);
-                       
-                       $ipParts = array();
-                       $tmp = $ip[0] % 256;
-                       $ipParts[] = ($ip[0] - $tmp) / 256;
-                       $ipParts[] = $tmp;
-                       $tmp = $ip[1] % 256;
-                       $ipParts[] = ($ip[1] - $tmp) / 256;
-                       $ipParts[] = $tmp;
-                       
-                       return implode('.', $ipParts);
+                       $ip = substr($ip, 7);
+                       if (preg_match('~^([a-f0-9]{4}):([a-f0-9]{4})$~', $ip, $matches)) {
+                               $ip = array(
+                                       base_convert($matches[1], 16, 10),
+                                       base_convert($matches[2], 16, 10)
+                               );
+                               
+                               $ipParts = array();
+                               $tmp = $ip[0] % 256;
+                               $ipParts[] = ($ip[0] - $tmp) / 256;
+                               $ipParts[] = $tmp;
+                               $tmp = $ip[1] % 256;
+                               $ipParts[] = ($ip[1] - $tmp) / 256;
+                               $ipParts[] = $tmp;
+                               
+                               return implode('.', $ipParts);
+                       }
+                       else {
+                               return $ip;
+                       }
                }
                else {
                        // given ip is an IPv6 address and cannot be converted