--- /dev/null
+<?php
+/* IPAddr.class.php - IPv4/IPv6 Address Class
+ * Copyright (C) 2011-2013 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class IPAddr {
+ 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})$/';
+ private static $pattern_IPv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
+ private $ip6 = array();
+ private $addr_is_ipv6 = false;
+
+ public function __construct($parse_ip) {
+ if($parse_ip == null) {
+ //nothing
+ } elseif(preg_match(self::$pattern_IPv6, $parse_ip)) {
+ $this->parseIPv6($parse_ip);
+ } elseif(preg_match(self::$pattern_IPv4, $parse_ip)) {
+ $this->parseIPv4($parse_ip);
+ }
+ }
+
+ public function parseIPv6($ipv6) {
+ if(substr($ipv6,0,2) == "::") $ipv6 = substr($ipv6, 1);
+ if(substr($ipv6,-2) == "::") $ipv6 = substr($ipv6, 0, -1);
+ $ipv6blocks = explode(":", $ipv6);
+ $j = 0;
+ foreach($ipv6blocks as $i => $block) {
+ if($block == "") {
+ $skipBlocks = 8 - count($ipv6blocks);
+ $j += $skipBlocks + 1;
+ } else if(strpos($block, '.')) { //ipv4 mapped
+ $this->parseIPv4($block);
+ return;
+ } else {
+ $this->ip6[$j++] = hexdec($block);
+ }
+ }
+ $this->addr_is_ipv6 = true;
+ }
+
+ public function parseIPv4($ipv4) {
+ $ipv4blocks = explode(".",$ipv4);
+ $this->ip6[6] = intval($ipv4blocks[0]) << 8;
+ $this->ip6[6] |= intval($ipv4blocks[1]);
+ $this->ip6[7] = intval($ipv4blocks[2]) << 8;
+ $this->ip6[7] |= intval($ipv4blocks[3]);
+ $this->addr_is_ipv6 = false;
+ }
+
+ public function isIPv6() {
+ return $this->addr_is_ipv6;
+ }
+
+ public function isLocalAddress() {
+ /* checks if address is out of:
+ * 127.0.0.1/32
+ * 10.0.0.0/8
+ * 192.168.0.0/16
+ * 172.16.0.0/12
+ * ::1/128
+ * fc00::/7
+ */
+ if($this->addr_is_ipv6) {
+ if(
+ (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
+ ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 &&
+ $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
+ )
+ return true;
+ } else {
+ if(
+ (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
+ (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
+ (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
+ (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
+ )
+ return true;
+ }
+ return false;
+ }
+
+ public function getAddress($options = array()) {
+ $shorten_ipv6 = (isset($options['shorten_ipv6']) ? $options['shorten_ipv6'] : true);
+ $ipv4_mapped = (isset($options['ipv4_mapped']) ? $options['ipv4_mapped'] : true);
+ if($this->isIPv6()) {
+ $max_start = 0;
+ $max_zeros = 0;
+ $curr_zeros = 0;
+ if($shorten_ipv6) {
+ for ($i = 0; $i < 8; $i++) {
+ if ($this->ip6[$i] == 0)
+ $curr_zeros++;
+ else if ($curr_zeros > $max_zeros) {
+ $max_start = $i - $curr_zeros;
+ $max_zeros = $curr_zeros;
+ $curr_zeros = 0;
+ }
+ }
+ if ($curr_zeros > $max_zeros) {
+ $max_start = $i - $curr_zeros;
+ $max_zeros = $curr_zeros;
+ }
+ }
+ $ipv6 = "";
+ for($i = 0; $i < 8; $i++) {
+ if($max_zeros > 1 && $i == $max_start) {
+ $ipv6 .= "::";
+ $i += $max_zeros - 1;
+ } else {
+ if($ipv6 != "") $ipv6 .= ":";
+ $hex = dechex($this->ip6[$i]);
+ if(!$shorten_ipv6) {
+ while(strlen($hex) < 4)
+ $hex = '0'.$hex;
+ }
+ $ipv6 .= $hex;
+ }
+ }
+ return $ipv6;
+ } else {
+ $ipv4 = array();
+ $ipv4[0] = ($this->ip6[6] >> 8) & 0xff;
+ $ipv4[1] = ($this->ip6[6]) & 0xff;
+ $ipv4[2] = ($this->ip6[7] >> 8) & 0xff;
+ $ipv4[3] = ($this->ip6[7]) & 0xff;
+ $ipv4 = implode(".", $ipv4);
+ if($ipv4_mapped)
+ $ipv4 = '::ffff:'.$ipv4;
+ return $ipv4;
+ }
+ }
+
+ public function ipMatches($ip) {
+ if(!is_a($ip, __CLASS__)) {
+ $class = __CLASS__;
+ $ip = new $class($ip);
+ }
+ if(!$ip)
+ return false;
+ if(!($this->addr_is_ipv6 xor $ip->addr_is_ipv6))
+ return false;
+ $i = ($this->addr_is_ipv6 ? 0 : 6);
+ for(;$i < 8; $i++) {
+ if($this->ip6[$i] != $ip->ip6[$i])
+ return false;
+ }
+ return true;
+ }
+
+}
+
+?>
\ No newline at end of file