fixed small port fail in HTTPConnector.class.php
[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                 for($i = 0; $i < 8; $i++)
27                         $this->ip6[$i] = 0;
28                 
29                 if($parse_ip == null) {
30                         //nothing
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);
35                 }
36         }
37
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);
42                 $j = 0;
43                 foreach($ipv6blocks as $i => $block) {
44                         if($block == "") {
45                                 $skipBlocks = 8 - count($ipv6blocks);
46                                 $j += $skipBlocks + 1;
47                         } else if(strpos($block, '.')) { //ipv4 mapped
48                                 $this->parseIPv4($block);
49                                 return;
50                         } else {
51                                 $this->ip6[$j++] = hexdec($block);
52                         }
53                 }
54                 $this->addr_is_ipv6 = true;
55         }
56
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;
64         }
65
66         public function isIPv6() {
67                 return $this->addr_is_ipv6;
68         }
69         
70         public function isLocalAddress() {
71                 /* checks if address is out of:
72                 * 127.0.0.1/32
73                 * 10.0.0.0/8
74                 * 192.168.0.0/16
75                 * 172.16.0.0/12
76                 * ::1/128
77                 * fc00::/7
78                 */
79                 if($this->addr_is_ipv6) {
80                         if(
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)
84                           )
85                                 return true;
86                 } else {
87                         if(
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 */
92                           )
93                                 return true;
94                 }
95                 return false;
96         }
97
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()) {
102                         $max_start = 0;
103                         $max_zeros = 0;
104                         $curr_zeros = 0;
105                         if($shorten_ipv6) {
106                                 for ($i = 0; $i < 8; $i++) {
107                                         if ($this->ip6[$i] == 0)
108                                         $curr_zeros++;
109                                         else if ($curr_zeros > $max_zeros) {
110                                                 $max_start = $i - $curr_zeros;
111                                                 $max_zeros = $curr_zeros;
112                                                 $curr_zeros = 0;
113                                         }
114                                 }
115                                 if ($curr_zeros > $max_zeros) {
116                                         $max_start = $i - $curr_zeros;
117                                         $max_zeros = $curr_zeros;
118                                 }
119                         }
120                         $ipv6 = "";
121                         for($i = 0; $i < 8; $i++) {
122                                 if($max_zeros > 1 && $i == $max_start) {
123                                         $ipv6 .= "::";
124                                         $i += $max_zeros - 1;
125                                 } else {
126                                         if($ipv6 != "") $ipv6 .= ":";
127                                         $hex = dechex($this->ip6[$i]);
128                                         if(!$shorten_ipv6) {
129                                                 while(strlen($hex) < 4)
130                                                         $hex = '0'.$hex;
131                                         }
132                                         $ipv6 .= $hex;
133                                 }
134                         }
135                         return $ipv6;
136                 } else {
137                         $ipv4 = array();
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);
143                         if($ipv4_mapped)
144                                 $ipv4 = '::ffff:'.$ipv4;
145                         return $ipv4;
146                 }
147         }
148         
149         public function ipMatches($ip) {
150                 if(!is_a($ip, __CLASS__)) {
151                         $class = __CLASS__;
152                         $ip = new $class($ip);
153                 }
154                 if(!$ip)
155                         return false;
156                 if($this->addr_is_ipv6 xor $ip->addr_is_ipv6)
157                         return false;
158                 $i = ($this->addr_is_ipv6 ? 0 : 6);
159                 for(;$i < 8; $i++) {
160                         if($this->ip6[$i] != $ip->ip6[$i])
161                                 return false;
162                 }
163                 return true;
164         }
165
166 }
167
168 ?>