661a8e209ac694230b790a261c92da3c60b3d2e4
[PHP-P10.git] / Uplink / IPAddr.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/IpAddr.class.php
24  *
25  * This class represents an IPv4 or IPv6 address.
26  *
27  */
28
29 class IPAddr {
30         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})$/';
31         private static $pattern_IPv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
32         private $ip6 = array();
33         private $addr_is_ipv6 = false;
34         
35         public function __construct($initial_value) {
36                 for($i = 0; $i < 8; $i++) {
37                         $this->ip6[$i] = 0;
38                 }
39                 if($initial_value == null) {
40                         //nothing
41                 } elseif(preg_match(self::$pattern_IPv6, $initial_value)) {
42                         $this->parseIPv6($initial_value);
43                 } elseif(preg_match(self::$pattern_IPv4, $initial_value)) {
44                         $this->parseIPv4($initial_value);
45                 } else {
46                         $this->parseNumeric($initial_value);
47                 }
48         }
49         
50         public function parseNumeric($numeric) {
51                 if(strlen($numeric) == 6) { //IPv4
52                         $value = Numerics::numToInt($numeric);
53                         $this->ip6[6] = ($value & 0xffff0000) >> 16;
54                         $this->ip6[7] = ($value & 0x0000ffff);
55                         $this->addr_is_ipv6 = false;
56                 } else { //IPv6
57                         $j = 0;
58                         for($i = 0; $i < strlen($numeric);) {
59                                 if($numeric[$i] == "_") {
60                                         $rightBlocks = (strlen($numeric) - $i - 1) / 3;
61                                         $skipBlocks = 8 - $j - $rightBlocks;
62                                         $j += $skipBlocks;
63                                         $i++;
64                                 } else {
65                                         $value = Numerics::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]);
66                                         $this->ip6[$j++] = dechex($value);
67                                         $i += 3;
68                                 }
69                         }
70                         $this->addr_is_ipv6 = true;
71                 }
72         }
73         
74         public function parseIPv6($ipv6) {
75                 if(substr($ipv6,0,2) == "::") $ipv6 = substr($ipv6, 1);
76                 if(substr($ipv6,-2) == "::") $ipv6 = substr($ipv6, 0, -1);
77                 $ipv6blocks = explode(":", $ipv6);
78                 $j = 0;
79                 foreach($ipv6blocks as $i => $block) {
80                         if($block == "") {
81                                 $skipBlocks = 8 - count($ipv6blocks);
82                                 $j += $skipBlocks + 1;
83                         } else {
84                                 $this->ip6[$j++] = hexdec($block);
85                         }
86                 }
87                 $this->addr_is_ipv6 = true;
88         }
89         
90         public function parseIPv4($ipv4) {
91                 $ipv4blocks = explode(".",$ipv4);
92                 $this->ip6[6] = intval($ipv4blocks[0]) << 8;
93                 $this->ip6[6] |= intval($ipv4blocks[1]);
94                 $this->ip6[7] = intval($ipv4blocks[2]) << 8;
95                 $this->ip6[7] |= intval($ipv4blocks[3]);
96                 $this->addr_is_ipv6 = false;
97         }
98         
99         public function isIPv6() {
100                 return $this->addr_is_ipv6;
101         }
102         
103         public function getAddress() {
104                 if($this->isIPv6()) {   
105                         $max_start = 0;
106                         $max_zeros = 0;
107                         $curr_zeros = 0;
108                         for ($i = 0; $i < 8; $i++) {
109                                 if ($this->ip6[$i] == 0)
110                                         $curr_zeros++;
111                                 else if ($curr_zeros > $max_zeros) {
112                                         $max_start = $i - $curr_zeros;
113                                         $max_zeros = $curr_zeros;
114                                         $curr_zeros = 0;
115                                 }
116                         }
117                         if ($curr_zeros > $max_zeros) {
118                                 $max_start = $i - $curr_zeros;
119                                 $max_zeros = $curr_zeros;
120                         }
121                         $ipv6 = "";
122                         for($i = 0; $i < 8; $i++) {
123                                 if($max_zeros > 1 && $i == $max_start) {
124                                         $ipv6 .= "::";
125                                         $i += $max_zeros - 1;
126                                 } else {
127                                         if($ipv6 != "") $ipv6 .= ":";
128                                         $ipv6 .= dechex($this->ip6[$i]);
129                                 }
130                         }
131                         return $ipv6;
132                 } else {
133                         $ipv4 = array();
134                         $ipv4[0] = ($this->ip6[6] >> 8) & 0xff;
135                         $ipv4[1] = ($this->ip6[6]) & 0xff;
136                         $ipv4[2] = ($this->ip6[7] >> 8) & 0xff;
137                         $ipv4[3] = ($this->ip6[7]) & 0xff;
138                         return implode(".", $ipv4);
139                 }
140         }
141         
142         public function getNumeric() {
143                 if($this->isIPv6()) {   
144                         $max_start = 0;
145                         $max_zeros = 0;
146                         $curr_zeros = 0;
147                         for ($i = 0; $i < 8; $i++) {
148                                 if ($this->ip6[$i] == 0)
149                                         $curr_zeros++;
150                                 else if ($curr_zeros > $max_zeros) {
151                                         $max_start = $i - $curr_zeros;
152                                         $max_zeros = $curr_zeros;
153                                         $curr_zeros = 0;
154                                 }
155                         }
156                         if ($curr_zeros > $max_zeros) {
157                                 $max_start = $i - $curr_zeros;
158                                 $max_zeros = $curr_zeros;
159                         }
160                         $ipv6 = "";
161                         for($i = 0; $i < 8; $i++) {
162                                 if($max_zeros > 0 && $i == $max_start) {
163                                         $ipv6 .= "_";
164                                         $i += $max_zeros - 1;
165                                 } else {
166                                         $ipv6 .= Numerics::intToNum($this->ip6[$i],3);
167                                 }
168                         }
169                         return $ipv6;
170                 } else {
171                         $ipv4 = array();
172                         $ipv4[0] = Numerics::intToNum($this->ip6[6], 3);
173                         $ipv4[1] = Numerics::intToNum($this->ip6[7], 3);
174                         return $ipv4[0].$ipv4[1];
175                 }
176         }
177         
178 }
179
180 ?>