Index: trunk/phase3/includes/IP.php |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | |
27 | 27 | // An IPv4 address is made of 4 bytes from x00 to xFF which is d0 to d255 |
28 | 28 | define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' ); |
29 | | -define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE ); |
| 29 | +define( 'RE_IP_ADD', RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE ); |
30 | 30 | // An IPv4 block is an IP address and a prefix (d1 to d32) |
31 | 31 | define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)' ); |
32 | 32 | define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX ); |
— | — | @@ -35,14 +35,18 @@ |
36 | 36 | define( 'RE_IPV6_WORD', '([0-9A-Fa-f]{1,4})' ); |
37 | 37 | define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)'); |
38 | 38 | define( 'RE_IPV6_ADD', |
39 | | - '(' . // starts with "::" (includes the address "::") |
40 | | - '(::|:(:' . RE_IPV6_WORD . '){1,7})' . |
| 39 | + '(?:' . // starts with "::" (includes the address "::") |
| 40 | + '::|:(?::' . RE_IPV6_WORD . '){1,7}' . |
41 | 41 | '|' . // ends with "::" (not including the address "::") |
42 | | - RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){0,6}::' . |
| 42 | + RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){0,6}::' . |
43 | 43 | '|' . // has no "::" |
44 | | - RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){7}' . |
45 | | - '|' . // contains one "::" in the middle ("^" check always fails if no "::" found) |
46 | | - RE_IPV6_WORD . '(:(?P<abbr>(?(abbr)|:))?' . RE_IPV6_WORD . '){1,6}(?(abbr)|^)' . |
| 44 | + RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){7}' . |
| 45 | + '|' . // contains one "::" in the middle (awkward regex for PCRE 4.0+ compatibility) |
| 46 | + RE_IPV6_WORD . '(?::(?!(?P=abn))(?P<abn>:(?P<iabn>))?' . RE_IPV6_WORD . '){1,6}(?P=iabn)' . |
| 47 | + // NOTE: (?!(?P=abn)) fails iff "::" used twice; (?P=iabn) passes iff a "::" was found. |
| 48 | + |
| 49 | + // Better regexp (PCRE 7.2+ only), allows intuitive regex concatenation |
| 50 | + #RE_IPV6_WORD . '(?::((?(-1)|:))?' . RE_IPV6_WORD . '){1,6}(?(-2)|^)' . |
47 | 51 | ')' |
48 | 52 | ); |
49 | 53 | // An IPv6 block is an IP address and a prefix (d1 to d128) |
— | — | @@ -54,9 +58,9 @@ |
55 | 59 | // This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network |
56 | 60 | define( 'IP_ADDRESS_STRING', |
57 | 61 | '(?:' . |
58 | | - RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' . // IPv4 |
| 62 | + RE_IP_ADD . '(?:\/' . RE_IP_PREFIX . ')?' . // IPv4 |
59 | 63 | '|' . |
60 | | - RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' . // IPv6 |
| 64 | + RE_IPV6_ADD . '(?:\/' . RE_IPV6_PREFIX . ')?' . // IPv6 |
61 | 65 | ')' |
62 | 66 | ); |
63 | 67 | |
— | — | @@ -85,7 +89,7 @@ |
86 | 90 | * @return Boolean |
87 | 91 | */ |
88 | 92 | public static function isIPv6( $ip ) { |
89 | | - return (bool)preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip ); |
| 93 | + return (bool)preg_match( '/^' . RE_IPV6_ADD . '(?:\/' . RE_IPV6_PREFIX . ')?$/', $ip ); |
90 | 94 | } |
91 | 95 | |
92 | 96 | /** |
— | — | @@ -96,7 +100,7 @@ |
97 | 101 | * @return Boolean |
98 | 102 | */ |
99 | 103 | public static function isIPv4( $ip ) { |
100 | | - return (bool)preg_match( '/^' . RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)$/', $ip ); |
| 104 | + return (bool)preg_match( '/^' . RE_IP_ADD . '(?:\/' . RE_IP_PREFIX . ')?$/', $ip ); |
101 | 105 | } |
102 | 106 | |
103 | 107 | /** |
— | — | @@ -174,7 +178,7 @@ |
175 | 179 | ); |
176 | 180 | } |
177 | 181 | // Remove leading zereos from each bloc as needed |
178 | | - $ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip ); |
| 182 | + $ip = preg_replace( '/(^|:)0+(' . RE_IPV6_WORD . ')/', '$1$2', $ip ); |
179 | 183 | return $ip; |
180 | 184 | } |
181 | 185 | |
— | — | @@ -217,7 +221,7 @@ |
218 | 222 | $ip_oct .= ':' . substr( $ip_hex, 4 * $n, 4 ); |
219 | 223 | } |
220 | 224 | // NO leading zeroes |
221 | | - $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct ); |
| 225 | + $ip_oct = preg_replace( '/(^|:)0+(' . RE_IPV6_WORD . ')/', '$1$2', $ip_oct ); |
222 | 226 | return $ip_oct; |
223 | 227 | } |
224 | 228 | |