13018e686ce59c9dd5b02953ff4c48bfc91b2f58
[CodeSnippets.git] / PHP / IPAddr.class.php
1 <?php
2 /* IPAddr.class.php - IPv4/IPv6 Address Class
3  * Copyright (C) 2011-2013  Philipp Kreil (pk910)
4  * 
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.
9  * 
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.
14  * 
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/>. 
17  */
18
19 class IPAddr {
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;
24
25         public function __construct($parse_ip) {
26                 if($parse_ip == null) {
27                         //nothing
28                 } elseif(preg_match(self::$pattern_IPv6, $parse_ip)) {
29                         $this->parseIPv6($parse_ip);
30                 } elseif(preg_match(self::$pattern_IPv4, $parse_ip)) {
31                         $this->parseIPv4($parse_ip);
32                 }
33         }
34
35         public function parseIPv6($ipv6) {
36                 if(substr($ipv6,0,2) == "::") $ipv6 = substr($ipv6, 1);
37                 if(substr($ipv6,-2) == "::") $ipv6 = substr($ipv6, 0, -1);
38                 $ipv6blocks = explode(":", $ipv6);
39                 $j = 0;
40                 foreach($ipv6blocks as $i => $block) {
41                         if($block == "") {
42                                 $skipBlocks = 8 - count($ipv6blocks);
43                                 $j += $skipBlocks + 1;
44                         } else if(strpos($block, '.')) { //ipv4 mapped
45                                 $this->parseIPv4($block);
46                                 return;
47                         } else {
48                                 $this->ip6[$j++] = hexdec($block);
49                         }
50                 }
51                 $this->addr_is_ipv6 = true;
52         }
53
54         public function parseIPv4($ipv4) {
55                 $ipv4blocks = explode(".",$ipv4);
56                 $this->ip6[6] = intval($ipv4blocks[0]) << 8;
57                 $this->ip6[6] |= intval($ipv4blocks[1]);
58                 $this->ip6[7] = intval($ipv4blocks[2]) << 8;
59                 $this->ip6[7] |= intval($ipv4blocks[3]);
60                 $this->addr_is_ipv6 = false;
61         }
62
63         public function isIPv6() {
64                 return $this->addr_is_ipv6;
65         }
66         
67         public function isLocalAddress() {
68                 /* checks if address is out of:
69                 * 127.0.0.1/32
70                 * 10.0.0.0/8
71                 * 192.168.0.0/16
72                 * 172.16.0.0/12
73                 * ::1/128
74                 * fc00::/7
75                 */
76                 if($this->addr_is_ipv6) {
77                         if(
78                                 (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
79                                 ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 && 
80                                  $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
81                           )
82                                 return true;
83                 } else {
84                         if(
85                                 (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
86                                 (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
87                                 (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
88                                 (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
89                           )
90                                 return true;
91                 }
92                 return false;
93         }
94
95         public function getAddress($options = array()) {
96                 $shorten_ipv6 = (isset($options['shorten_ipv6']) ? $options['shorten_ipv6'] : true);
97                 $ipv4_mapped = (isset($options['ipv4_mapped']) ? $options['ipv4_mapped'] : true);
98                 if($this->isIPv6()) {
99                         $max_start = 0;
100                         $max_zeros = 0;
101                         $curr_zeros = 0;
102                         if($shorten_ipv6) {
103                                 for ($i = 0; $i < 8; $i++) {
104                                         if ($this->ip6[$i] == 0)
105                                         $curr_zeros++;
106                                         else if ($curr_zeros > $max_zeros) {
107                                                 $max_start = $i - $curr_zeros;
108                                                 $max_zeros = $curr_zeros;
109                                                 $curr_zeros = 0;
110                                         }
111                                 }
112                                 if ($curr_zeros > $max_zeros) {
113                                         $max_start = $i - $curr_zeros;
114                                         $max_zeros = $curr_zeros;
115                                 }
116                         }
117                         $ipv6 = "";
118                         for($i = 0; $i < 8; $i++) {
119                                 if($max_zeros > 1 && $i == $max_start) {
120                                         $ipv6 .= "::";
121                                         $i += $max_zeros - 1;
122                                 } else {
123                                         if($ipv6 != "") $ipv6 .= ":";
124                                         $hex = dechex($this->ip6[$i]);
125                                         if(!$shorten_ipv6) {
126                                                 while(strlen($hex) < 4)
127                                                         $hex = '0'.$hex;
128                                         }
129                                         $ipv6 .= $hex;
130                                 }
131                         }
132                         return $ipv6;
133                 } else {
134                         $ipv4 = array();
135                         $ipv4[0] = ($this->ip6[6] >> 8) & 0xff;
136                         $ipv4[1] = ($this->ip6[6]) & 0xff;
137                         $ipv4[2] = ($this->ip6[7] >> 8) & 0xff;
138                         $ipv4[3] = ($this->ip6[7]) & 0xff;
139                         $ipv4 = implode(".", $ipv4);
140                         if($ipv4_mapped)
141                                 $ipv4 = '::ffff:'.$ipv4;
142                         return $ipv4;
143                 }
144         }
145         
146         public function ipMatches($ip) {
147                 if(!is_a($ip, __CLASS__)) {
148                         $class = __CLASS__;
149                         $ip = new $class($ip);
150                 }
151                 if(!$ip)
152                         return false;
153                 if(!($this->addr_is_ipv6 xor $ip->addr_is_ipv6))
154                         return false;
155                 $i = ($this->addr_is_ipv6 ? 0 : 6);
156                 for(;$i < 8; $i++) {
157                         if($this->ip6[$i] != $ip->ip6[$i])
158                                 return false;
159                 }
160                 return true;
161         }
162
163 }
164
165 ?>