Revert r49669, r49670 "extract text layer from djvu file (see bug 18046)"
[lhc/web/wiklou.git] / includes / IP.php
index 653fbbc..a208542 100644 (file)
@@ -141,7 +141,7 @@ class IP {
        public static function toOctet( $ip_int ) {
                // Convert to padded uppercase hex
                $ip_hex = wfBaseConvert($ip_int, 10, 16, 32, false);
-               // Seperate into 8 octets
+               // Separate into 8 octets
                $ip_oct = substr( $ip_hex, 0, 4 );
                for ($n=1; $n < 8; $n++) {
                        $ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
@@ -150,16 +150,27 @@ class IP {
                $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
                return $ip_oct;
        }
+
+       /**
+        * Convert an IPv4 or IPv6 hexadecimal representation back to readable format
+        */
+       public static function formatHex( $hex ) {
+               if ( substr( $hex, 0, 3 ) == 'v6-' ) {
+                       return self::hexToOctet( $hex );
+               } else {
+                       return self::hexToQuad( $hex );
+               }
+       }
        
        /**
-        * Given an unsigned integer, returns an IPv6 address in octet notation
+        * Given a hexadecimal number, returns to an IPv6 address in octet notation
         * @param $ip string hex IP
         * @return string
         */
-       public static function HextoOctet( $ip_hex ) {
+       public static function hextoOctet( $ip_hex ) {
                // Convert to padded uppercase hex
                $ip_hex = str_pad( strtoupper($ip_hex), 32, '0');
-               // Seperate into 8 octets
+               // Separate into 8 octets
                $ip_oct = substr( $ip_hex, 0, 4 );
                for ($n=1; $n < 8; $n++) {
                        $ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
@@ -168,6 +179,23 @@ class IP {
                $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
                return $ip_oct;
        }
+       
+       /**
+        * Converts a hexadecimal number to an IPv4 address in octet notation
+        * @param $ip string Hex IP
+        * @return string
+        */ 
+       public static function hexToQuad( $ip ) {
+               // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
+               $s = '';
+               for ( $i = 0; $i < 4; $i++ ) {
+                       if ( $s !== '' ) {
+                               $s .= '.';
+                       }
+                       $s .= base_convert( substr( $ip, $i * 2, 2 ), 16, 10 );
+               }
+               return $s;
+       }
 
        /**
         * Convert a network specification in IPv6 CIDR notation to an integer network and a number of bits
@@ -338,7 +366,7 @@ class IP {
        public static function toHex( $ip ) {
                $n = self::toUnsigned( $ip );
                if ( $n !== false ) {
-                       $n = ( self::isIPv6($ip) ) ? "v6-" . wfBaseConvert( $n, 10, 16, 32, false ) : wfBaseConvert( $n, 10, 16, 8, false );
+                       $n = self::isIPv6($ip) ? "v6-" . wfBaseConvert( $n, 10, 16, 32, false ) : wfBaseConvert( $n, 10, 16, 8, false );
                }
                return $n;
        }