2 /* IPAddr.class.php - IPv4/IPv6 Address Class
3 * Copyright (C) 2011-2013 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 private static $pattern_IPv6 = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/';
21 private static $pattern_IPv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
22 private $ip6 = array();
23 private $addr_is_ipv6 = false;
25 public function __construct($parse_ip) {
26 for($i = 0; $i < 8; $i++)
29 if($parse_ip == null) {
31 } elseif(preg_match(self::$pattern_IPv6, $parse_ip)) {
32 $this->parseIPv6($parse_ip);
33 } elseif(preg_match(self::$pattern_IPv4, $parse_ip)) {
34 $this->parseIPv4($parse_ip);
38 public function parseIPv6($ipv6) {
39 if(substr($ipv6,0,2) == "::") $ipv6 = substr($ipv6, 1);
40 if(substr($ipv6,-2) == "::") $ipv6 = substr($ipv6, 0, -1);
41 $ipv6blocks = explode(":", $ipv6);
43 foreach($ipv6blocks as $i => $block) {
45 $skipBlocks = 8 - count($ipv6blocks);
46 $j += $skipBlocks + 1;
47 } else if(strpos($block, '.')) { //ipv4 mapped
48 $this->parseIPv4($block);
51 $this->ip6[$j++] = hexdec($block);
54 $this->addr_is_ipv6 = true;
57 public function parseIPv4($ipv4) {
58 $ipv4blocks = explode(".",$ipv4);
59 $this->ip6[6] = intval($ipv4blocks[0]) << 8;
60 $this->ip6[6] |= intval($ipv4blocks[1]);
61 $this->ip6[7] = intval($ipv4blocks[2]) << 8;
62 $this->ip6[7] |= intval($ipv4blocks[3]);
63 $this->addr_is_ipv6 = false;
66 public function isIPv6() {
67 return $this->addr_is_ipv6;
70 public function isLocalAddress() {
71 /* checks if address is out of:
79 if($this->addr_is_ipv6) {
81 (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
82 ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 &&
83 $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
88 (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
89 (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
90 (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
91 (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
98 public function getAddress($options = array()) {
99 $shorten_ipv6 = (isset($options['shorten_ipv6']) ? $options['shorten_ipv6'] : true);
100 $ipv4_mapped = (isset($options['ipv4_mapped']) ? $options['ipv4_mapped'] : true);
101 if($this->isIPv6()) {
106 for ($i = 0; $i < 8; $i++) {
107 if ($this->ip6[$i] == 0)
109 else if ($curr_zeros > $max_zeros) {
110 $max_start = $i - $curr_zeros;
111 $max_zeros = $curr_zeros;
115 if ($curr_zeros > $max_zeros) {
116 $max_start = $i - $curr_zeros;
117 $max_zeros = $curr_zeros;
121 for($i = 0; $i < 8; $i++) {
122 if($max_zeros > 1 && $i == $max_start) {
124 $i += $max_zeros - 1;
126 if($ipv6 != "") $ipv6 .= ":";
127 $hex = dechex($this->ip6[$i]);
129 while(strlen($hex) < 4)
138 $ipv4[0] = ($this->ip6[6] >> 8) & 0xff;
139 $ipv4[1] = ($this->ip6[6]) & 0xff;
140 $ipv4[2] = ($this->ip6[7] >> 8) & 0xff;
141 $ipv4[3] = ($this->ip6[7]) & 0xff;
142 $ipv4 = implode(".", $ipv4);
144 $ipv4 = '::ffff:'.$ipv4;
149 public function ipMatches($ip) {
150 if(!is_a($ip, __CLASS__)) {
152 $ip = new $class($ip);
156 if($this->addr_is_ipv6 xor $ip->addr_is_ipv6)
158 $i = ($this->addr_is_ipv6 ? 0 : 6);
160 if($this->ip6[$i] != $ip->ip6[$i])