IPv6 support added
authorMarcel Werk <burntime@woltlab.com>
Wed, 10 Aug 2011 12:15:51 +0000 (14:15 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 10 Aug 2011 12:15:51 +0000 (14:15 +0200)
wcfsetup/install/files/lib/util/UserUtil.class.php

index 0bf5998a43c7ff60f4c52e48e597d73272ec6994..d2fb155abcec84a079b58984dd0e86938ed0683f 100644 (file)
@@ -102,31 +102,47 @@ class UserUtil {
        }
        
        /**
-        * Returns the ip address of the client.
+        * Returns the ipv6 address of the client.
         *
-        * @return      string          ip address
+        * @return      string          ipv6 address
         */
        public static function getIpAddress() {
                $REMOTE_ADDR = '';
                if (isset($_SERVER['REMOTE_ADDR'])) $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
-               if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
-               else $HTTP_X_FORWARDED_FOR = '';
-       
-               if (!empty($HTTP_X_FORWARDED_FOR)) {
-                       $match = array();
-                       if (preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $HTTP_X_FORWARDED_FOR, $match)) {
-                               $REMOTE_ADDR = preg_replace(self::$privateIpList, $REMOTE_ADDR, $match[1]);     
-                       }
-               }
-       
+               
                // darwin fix
                if ($REMOTE_ADDR == '::1' || $REMOTE_ADDR == 'fe80::1') {
                        $REMOTE_ADDR = '127.0.0.1'; 
                }
                
+               $REMOTE_ADDR = self::convertIPv4To6($REMOTE_ADDR);
+               
                return $REMOTE_ADDR;
        }
        
+       /**
+        * Converts given ipv4 to ipv6.
+        * 
+        * @param       string          $ip
+        * @return      string
+        */
+       public static function convertIPv4To6($ip) {
+               if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
+                       // given ip is already ipv6
+                       return $ip;
+               }
+               
+               if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
+                       // invalid ip given
+                       return '';
+               }
+               
+               $ipArray = array_pad(explode('.', $ip), 4, 0);
+               $part7 = base_convert(($ipArray[0] * 256) + $ipArray[1], 10, 16);
+               $part8 = base_convert(($ipArray[2] * 256) + $ipArray[3], 10, 16);
+               return '::ffff:'.$part7.':'.$part8;
+       }
+       
        /**
         * Returns the request uri of the active request.
         *