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