+ /**
+ * Given a string, determine if it as valid IP
+ * Unlike isValid(), this looks for networks too
+ * @param $ip IP address.
+ * @return string
+ */
+ public static function isIPAddress( $ip ) {
+ if ( !$ip ) return false;
+ if ( is_array( $ip ) ) {
+ throw new MWException( "invalid value passed to " . __METHOD__ );
+ }
+ // IPv6 IPs with two "::" strings are ambiguous and thus invalid
+ return preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip) && ( substr_count($ip, '::') < 2 );
+ }
+
+ public static function isIPv6( $ip ) {
+ if ( !$ip ) return false;
+ if( is_array( $ip ) ) {
+ throw new MWException( "invalid value passed to " . __METHOD__ );
+ }
+ // IPv6 IPs with two "::" strings are ambiguous and thus invalid
+ return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip) && ( substr_count($ip, '::') < 2);
+ }
+
+ public static function isIPv4( $ip ) {
+ if ( !$ip ) return false;
+ return preg_match( '/^' . RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)$/', $ip);
+ }
+
+ /**
+ * Given an IP address in dotted-quad notation, returns an IPv6 octet.
+ * See http://www.answers.com/topic/ipv4-compatible-address
+ * IPs with the first 92 bits as zeros are reserved from IPv6
+ * @param $ip quad-dotted IP address.
+ * @return string
+ */
+ public static function IPv4toIPv6( $ip ) {
+ if ( !$ip ) return null;
+ // Convert only if needed
+ if ( self::isIPv6( $ip ) ) return $ip;
+ // IPv4 CIDRs
+ if ( strpos( $ip, '/' ) !== false ) {
+ $parts = explode( '/', $ip, 2 );
+ if ( count( $parts ) != 2 ) {
+ return false;
+ }
+ $network = self::toUnsigned( $parts[0] );
+ if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
+ $bits = $parts[1] + 96;
+ return self::toOctet( $network ) . "/$bits";
+ } else {
+ return false;
+ }
+ }
+ return self::toOctet( self::toUnsigned( $ip ) );
+ }
+
+ /**
+ * Given an IPv6 address in octet notation, returns an unsigned integer.
+ * @param $ip octet ipv6 IP address.
+ * @return string
+ */
+ public static function toUnsigned6( $ip ) {
+ if ( !$ip ) return null;
+ $ip = explode(':', self::sanitizeIP( $ip ) );
+ $r_ip = '';
+ foreach ($ip as $v) {
+ $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
+ }
+ $r_ip = wfBaseConvert( $r_ip, 16, 10 );
+ return $r_ip;
+ }
+
+ /**
+ * Given an IPv6 address in octet notation, returns the expanded octet.
+ * IPv4 IPs will be trimmed, thats it...
+ * @param $ip octet ipv6 IP address.
+ * @return string
+ */
+ public static function sanitizeIP( $ip ) {
+ $ip = trim( $ip );
+ if ( $ip === '' ) return null;
+ // Trim and return IPv4 addresses
+ if ( self::isIPv4($ip) ) return $ip;
+ // Only IPv6 addresses can be expanded
+ if ( !self::isIPv6($ip) ) return $ip;
+ // Remove any whitespaces, convert to upper case
+ $ip = strtoupper( $ip );
+ // Expand zero abbreviations
+ if ( strpos( $ip, '::' ) !== false ) {
+ $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
+ }
+ // For IPs that start with "::", correct the final IP so that it starts with '0' and not ':'
+ if ( $ip[0] == ':' ) $ip = "0$ip";
+ // Remove leading zereos from each bloc as needed
+ $ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip );
+ return $ip;
+ }
+
+ /**
+ * Given an unsigned integer, returns an IPv6 address in octet notation
+ * @param $ip integer IP address.
+ * @return string
+ */
+ public static function toOctet( $ip_int ) {
+ // Convert to padded uppercase hex
+ $ip_hex = wfBaseConvert($ip_int, 10, 16, 32, false);
+ // Seperate into 8 octets
+ $ip_oct = substr( $ip_hex, 0, 4 );
+ for ($n=1; $n < 8; $n++) {
+ $ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
+ }
+ // NO leading zeroes
+ $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
+ return $ip_oct;
+ }