added own IPAddr Class and rewrote the IP Address parser & builder
authorpk910 <philipp@zoelle1.de>
Thu, 28 Jul 2011 07:57:48 +0000 (09:57 +0200)
committerpk910 <philipp@zoelle1.de>
Thu, 28 Jul 2011 07:57:48 +0000 (09:57 +0200)
Bots/IPv6.class.php
Uplink/IPAddr.class.php [new file with mode: 0644]
Uplink/Numerics.class.php
Uplink/Uplink.class.php

index 9dc8f847bd55974b9a532063ffff067c588027cc..44c79d7fdf9c6100acd58e20add9fd3c1c8ad038 100644 (file)
@@ -81,9 +81,7 @@ class {$_NAME} extends Bot {
        
        public function recive_join($user, $channel, $isBurst) {
                if(!$this->botOppedOnChannel($channel)) return false;
-               $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})$/';
-               echo $user->getIP()."\n";
-               if(preg_match($ipv6, $user->getIP())) {
+               if($user->getIP()->isIPv6()) {
                        $this->uplink->mode($this->ipv6, $channel, "+ov ".$user->getNumeric()." ".$user->getNumeric());
                } else {
                        $this->uplink->mode($this->ipv6, $channel, "+v ".$user->getNumeric());
diff --git a/Uplink/IPAddr.class.php b/Uplink/IPAddr.class.php
new file mode 100644 (file)
index 0000000..217fbe7
--- /dev/null
@@ -0,0 +1,176 @@
+<?php
+/********************************* PHP-P10 ******************************
+ *    P10 uplink class by pk910   (c)2011 pk910                         *
+ ************************************************************************
+ *                          Version 2 (OOP)                             *
+ *                                                                      *
+ * PHP-P10 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 2 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 PHP-P10; if not, write to the Free Software Foundation,   *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
+ *                                                                      *
+ ************************************************************************
+ * 
+ *  Uplink/IpAddr.class.php
+ *
+ * This class represents an IPv4 or IPv6 address.
+ *
+ */
+
+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($initial_value) {
+               for($i = 0; $i < 8; $i++) {
+                       $ip6[$i] = 0;
+               }
+               if($initial_value == null) {
+                       //nothing
+               } elseif(preg_match(self::$pattern_IPv6, $initial_value)) {
+                       $this->parseIPv6($initial_value);
+               } elseif(preg_match(self::$pattern_IPv4, $initial_value)) {
+                       $this->parseIPv4($initial_value);
+               } else {
+                       $this->parseNumeric($initial_value);
+               }
+       }
+       
+       public function parseNumeric($numeric) {
+               if(strlen($numeric) == 6) { //IPv4
+                       $value = self::numToInt($numeric);
+                       $this->ip6[6] = ($value & 0xffff0000) >> 16;
+                       $this->ip6[7] = ($value & 0x0000ffff);
+                       $this->addr_is_ipv6 = false;
+               } else { //IPv6
+                       $j = 0;
+                       for($i = 0; $i < strlen($numeric);) {
+                               if($numeric[$i] == "_") {
+                                       $rightBlocks = (strlen($numeric) - $i - 1) / 3;
+                                       $skipBlocks = 8 - $j - $rightBlocks;
+                                       $j += $skipBlocks;
+                               } else {
+                                       $value = self::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]);
+                                       $this->ip6[$j++] = dechex($value);
+                                       $i += 3;
+                               }
+                       }
+               }
+       }
+       
+       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 {
+                               $this->ip6[$j++] = hexdec($block);
+                       }
+               }
+       }
+       
+       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]);
+       }
+       
+       public function isIPv6() {
+               return $this->addr_is_ipv6;
+       }
+       
+       public function getAddress() {
+               if($this->isIPv6()) {   
+                       $max_start = 0;
+                       $max_zeros = 0;
+                       $curr_zeros = 0;
+                       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 .= ":";
+                                       $ipv6 .= dechex($this->ip6[$i]);
+                               }
+                       }
+                       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;
+                       return implode(".", $ipv4);
+               }
+       }
+       
+       public function getNumeric() {
+               if($this->isIPv6()) {   
+                       $max_start = 0;
+                       $max_zeros = 0;
+                       $curr_zeros = 0;
+                       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 > 0 && $i == $max_start) {
+                                       $ipv6 .= "_";
+                                       $i += $max_zeros - 1;
+                               } else {
+                                       $ipv6 .= Numerics::intToNum($this->ip6[$i],3);
+                               }
+                       }
+                       return $ipv6;
+               } else {
+                       $ipv4 = array();
+                       $ipv4[0] = Numerics::intToNum($this->ip6[6], 3);
+                       $ipv4[1] = Numerics::intToNum($this->ip6[7], 3);
+                       return $ipv4[0].$ipv4[1];
+               }
+       }
+       
+}
+
+?>
\ No newline at end of file
index 7b6bee116e219da74223c50deb4d5ff1f215d480..01944dcb9aa6d42776c97e38593287fcc10fefb0 100644 (file)
@@ -84,117 +84,6 @@ class Numerics {
                return $int;
        }
        
-       public static function parseIP($numeric, $shortForm = true) {
-               if(strlen($numeric) == 6) { //IPv4
-                       $value = self::numToInt($numeric);
-                       $ip = array();
-                       $ip[0] = ($value & 0xff000000) >> 24;
-                       $ip[1] = ($value & 0x00ff0000) >> 16;
-                       $ip[2] = ($value & 0x0000ff00) >> 8;
-                       $ip[3] = ($value & 0x000000ff);
-                       return implode(".", $ip);
-               } else { //IPv6
-                       $ip = array();
-                       for($i = 0; $i < strlen($numeric);) {
-                               if($numeric[$i] == "_") {
-                                       if($shortForm) {
-                                               $ip[] = ""; //::
-                                       } else {
-                                               $rightBlocks = (strlen($numeric) - ($i + 1)) / 3;
-                                               $skipCount = 8 - count($ip) - $rightBlocks;
-                                               for($j = 0; $j < $skipBlocks; $j++) {
-                                                       $ip[] = "0";
-                                               }
-                                       }
-                                       $i++;
-                               } else {
-                                       $value = self::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]);
-                                       $ip[] = dechex($value);
-                                       $i += 3;
-                               }
-                       }
-                       return implode(":", $ip);
-               }
-       }
-       
-       public static function numericFromIP($ip) {
-               $pattern = array();
-               $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})$/';
-               $pattern['ipv4'] = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
-               if(preg_match($pattern['ipv4'], $ip)) {
-                       //thats quite simple here
-                       $ip = explode(".", $ip);
-                       $ipValue = intval($ip[3]);
-                       $ipValue <<= 8;
-                       $ipValue |= intval($ip[2]);
-                       $ipValue <<= 8;
-                       $ipValue |= intval($ip[1]);
-                       $ipValue <<= 8;
-                       $ipValue |= intval($ip[0]);
-                       $ip = self::intToNum($ipValue,6);
-               } else if(preg_match($pattern['ipv6'], $ip)) {
-                       //thats a little bit complicated :D (we may only have one _)
-                       $ipv6 = array();
-                       $ip = explode(":",$ip);
-                       $last_zero = false; $zero_sequence = 0; $biggest_zero_sequence = 0; $max_start = -1;
-                       $i = 0;
-                       foreach($ip as $v) {
-                               if($v == "") {
-                                       $skipBlocks = (8 - count($ip));
-                                       for($j = 0; $j < $skipBlocks; $j++) {
-                                               $ipv6[$i+$j] = "_";
-                                       }
-                                       $max_start = $i;
-                                       if($last_zero) {
-                                               $zero_sequence += $skipBlocks;
-                                       } else {
-                                               $last_zero = true;
-                                               $zero_sequence = $skipBlocks;
-                                       }
-                                       $i+=$skipBlocks;
-                                       if($zero_sequence > $biggest_zero_sequence) {
-                                               $biggest_zero_sequence = $zero_sequence;
-                                               $max_start = $i-($zero_sequence-1);
-                                       }
-                               } else {
-                                       $value = hexdec($v);
-                                       if($value == 0) {
-                                               $ipv6[$i] = "_";
-                                               if($last_zero) {
-                                                       $zero_sequence++;
-                                               } else {
-                                                       $last_zero = true;
-                                                       $zero_sequence = 1;
-                                               }
-                                               if($zero_sequence > $biggest_zero_sequence) {
-                                                       $biggest_zero_sequence = $zero_sequence;
-                                                       $max_start = $i-($zero_sequence-1);
-                                               }
-                                       } else {
-                                               $ipv6[$i] = self::intToNum($value,3);
-                                               $last_zero = false;
-                                       }
-                                       $i++;
-                               }
-                       }
-                       $ip = "";
-                       for($i = 0; $i < 8; $i++) {
-                               if($i == $max_start) { //merge the biggest sequence of _'s
-                                       $ip .= "_";
-                                       $i += ($biggest_zero_sequence-1);
-                               } elseif($ipv6[$i] == "_") {
-                                       $ip .= "AAA";
-                               } else {
-                                       $ip .= $ipv6[$i];
-                               }
-                       }
-                       return $ip;
-               } else {
-                       $ip = "AAAAAA";
-               }
-               return $ip;
-       }
-       
 }
 
 ?>
\ No newline at end of file
index f83c7e35a85cb3e64f0cd49efba41edb60be1704..a23f4cee487c3543c8958a67b94668fcbd8a0967 100644 (file)
@@ -61,6 +61,7 @@ require_once("P10_User.class.php");
 require_once("P10_Channel.class.php");
 require_once("P10_ModeSets.class.php");
 require_once("EventHandler.interface.php");
+require_once("IPAddr.class.php");
 
 $e=1;
 define("ERR_NICK_IN_USE", $e++);
@@ -394,7 +395,7 @@ class Uplink {
                        $host = $args[4];
                        $modes = implode(" ",array_slice($args, 5, count($args)-8));
                        $modes = new P10_UserModeSet($modes);
-                       $ip = Numerics::parseIP($args[count($args)-3]);
+                       $ip = new IPAddr($args[count($args)-3]);
                        $realname = $args[count($args)-1];
                        $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
                        if($this->eventHandler)
@@ -785,9 +786,10 @@ class Uplink {
                }
                $this->last_local_numeric++;
                $modes = new P10_UserModeSet($modes);
+               $ip = new IPAddr($ip);
                $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes);
                if(($this->flags & self::FLAG_CONNECTED)) {
-                       $ip = Numerics::numericFromIP($user->getIP());
+                       $ip = $user->getIP()->getNumeric();
                        $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname);
                }
                return $user;