b1912e8926c3c503d395415aa0b2b46c275ba707
[PHP-P10.git] / Uplink / IPAddr.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  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  *
20  *  Uplink/IpAddr.class.php
21  *
22  * This class represents an IPv4 or IPv6 address.
23  *
24  */
25
26 class IPAddr {
27         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})$/';
28         private static $pattern_IPv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
29         private $ip6 = array();
30         private $addr_is_ipv6 = false;
31         private $is_server_addr = false;
32
33         public function __construct($initial_value) {
34                 for($i = 0; $i < 8; $i++) {
35                         $this->ip6[$i] = 0;
36                 }
37                 if($initial_value == null) {
38                         //nothing
39                 } elseif(preg_match(self::$pattern_IPv6, $initial_value)) {
40                         $this->parseIPv6($initial_value);
41                 } elseif(preg_match(self::$pattern_IPv4, $initial_value)) {
42                         $this->parseIPv4($initial_value);
43                 } else {
44                         $this->parseNumeric($initial_value);
45                 }
46         }
47
48         public function parseNumeric($numeric) {
49                 if(strlen($numeric) == 6) { //IPv4
50                         $value = Numerics::numToInt($numeric);
51                         $this->ip6[6] = ($value & 0xffff0000) >> 16;
52                         $this->ip6[7] = ($value & 0x0000ffff);
53                         $this->addr_is_ipv6 = false;
54                 } else { //IPv6
55                         $j = 0;
56                         for($i = 0; $i < strlen($numeric);) {
57                                 if($numeric[$i] == "_") {
58                                         $rightBlocks = (strlen($numeric) - $i - 1) / 3;
59                                         $skipBlocks = 8 - $j - $rightBlocks;
60                                         $j += $skipBlocks;
61                                         $i++;
62                                 } else {
63                                         $value = Numerics::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]);
64                                         $this->ip6[$j++] = dechex($value);
65                                         $i += 3;
66                                 }
67                         }
68                         $this->addr_is_ipv6 = true;
69                 }
70         }
71
72         public function parseIPv6($ipv6) {
73                 if(substr($ipv6,0,2) == "::") $ipv6 = substr($ipv6, 1);
74                 if(substr($ipv6,-2) == "::") $ipv6 = substr($ipv6, 0, -1);
75                 $ipv6blocks = explode(":", $ipv6);
76                 $j = 0;
77                 foreach($ipv6blocks as $i => $block) {
78                         if($block == "") {
79                                 $skipBlocks = 8 - count($ipv6blocks);
80                                 $j += $skipBlocks + 1;
81                         } else {
82                                 $this->ip6[$j++] = hexdec($block);
83                         }
84                 }
85                 $this->addr_is_ipv6 = true;
86         }
87
88         public function parseIPv4($ipv4) {
89                 $ipv4blocks = explode(".",$ipv4);
90                 $this->ip6[6] = intval($ipv4blocks[0]) << 8;
91                 $this->ip6[6] |= intval($ipv4blocks[1]);
92                 $this->ip6[7] = intval($ipv4blocks[2]) << 8;
93                 $this->ip6[7] |= intval($ipv4blocks[3]);
94                 $this->addr_is_ipv6 = false;
95         }
96
97         public function isIPv6() {
98                 return $this->addr_is_ipv6;
99         }
100         
101         public function setServerAddr($serverAddr) {
102                 $this->is_server_addr = $serverAddr;
103         }
104         
105         public function isServerAddr() {
106                 return $this->is_server_addr;
107         }
108         
109         public function isLocalAddress($serverAddr = false) {
110                 /* checks if address is out of:
111                 * 127.0.0.1/32
112                 * 10.0.0.0/8
113                 * 192.168.0.0/16
114                 * 172.16.0.0/12
115                 * ::1/128
116                 * fc00::/7
117                 *
118                 * if $serverAddr is true also return true for server IP's
119                 */
120                 if($this->addr_is_ipv6) {
121                         if(
122                                 (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
123                                 ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 && 
124                                  $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
125                           )
126                                 return true;
127                 } else {
128                         if(
129                                 (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
130                                 (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
131                                 (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
132                                 (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
133                           )
134                                 return true;
135                 }
136                 if($serverAddr && $this->is_server_addr)
137                         return true;
138                 return false;
139         }
140
141         public function getAddress() {
142                 if($this->isIPv6()) {
143                         $max_start = 0;
144                         $max_zeros = 0;
145                         $curr_zeros = 0;
146                         for ($i = 0; $i < 8; $i++) {
147                                 if ($this->ip6[$i] == 0)
148                                 $curr_zeros++;
149                                 else if ($curr_zeros > $max_zeros) {
150                                         $max_start = $i - $curr_zeros;
151                                         $max_zeros = $curr_zeros;
152                                         $curr_zeros = 0;
153                                 }
154                         }
155                         if ($curr_zeros > $max_zeros) {
156                                 $max_start = $i - $curr_zeros;
157                                 $max_zeros = $curr_zeros;
158                         }
159                         $ipv6 = "";
160                         for($i = 0; $i < 8; $i++) {
161                                 if($max_zeros > 1 && $i == $max_start) {
162                                         $ipv6 .= "::";
163                                         $i += $max_zeros - 1;
164                                 } else {
165                                         if($ipv6 != "") $ipv6 .= ":";
166                                         $ipv6 .= dechex($this->ip6[$i]);
167                                 }
168                         }
169                         return $ipv6;
170                 } else {
171                         $ipv4 = array();
172                         $ipv4[0] = ($this->ip6[6] >> 8) & 0xff;
173                         $ipv4[1] = ($this->ip6[6]) & 0xff;
174                         $ipv4[2] = ($this->ip6[7] >> 8) & 0xff;
175                         $ipv4[3] = ($this->ip6[7]) & 0xff;
176                         return implode(".", $ipv4);
177                 }
178         }
179
180         public function getNumeric() {
181                 if($this->isIPv6()) {
182                         $max_start = 0;
183                         $max_zeros = 0;
184                         $curr_zeros = 0;
185                         for ($i = 0; $i < 8; $i++) {
186                                 if ($this->ip6[$i] == 0)
187                                 $curr_zeros++;
188                                 else if ($curr_zeros > $max_zeros) {
189                                         $max_start = $i - $curr_zeros;
190                                         $max_zeros = $curr_zeros;
191                                         $curr_zeros = 0;
192                                 }
193                         }
194                         if ($curr_zeros > $max_zeros) {
195                                 $max_start = $i - $curr_zeros;
196                                 $max_zeros = $curr_zeros;
197                         }
198                         $ipv6 = "";
199                         for($i = 0; $i < 8; $i++) {
200                                 if($max_zeros > 0 && $i == $max_start) {
201                                         $ipv6 .= "_";
202                                         $i += $max_zeros - 1;
203                                 } else {
204                                         $ipv6 .= Numerics::intToNum($this->ip6[$i],3);
205                                 }
206                         }
207                         return $ipv6;
208                 } else {
209                         $ipv4 = array();
210                         $ipv4[0] = Numerics::intToNum($this->ip6[6], 3);
211                         $ipv4[1] = Numerics::intToNum($this->ip6[7], 3);
212                         return $ipv4[0].$ipv4[1];
213                 }
214         }
215
216 }
217
218 ?>