Net_IPv6
[ class tree: Net_IPv6 ] [ index: Net_IPv6 ] [ all elements ]

Source for file IPv6.php

Documentation is available at IPv6.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Merz <alexander.merz@web.de>                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: IPv6.php,v 1.8 2004/11/24 23:23:35 alexmerz Exp $
  20.  
  21. /**
  22. * Class to validate and to work with IPv6
  23. *
  24. * Todo: some optimizations for checkIPv6()
  25. *
  26. @author  Alexander Merz <alexander.merz@t-online.de>
  27. @package Net_IPv6
  28. @version $Id: IPv6.php,v 1.8 2004/11/24 23:23:35 alexmerz Exp $
  29. @access  public
  30. */
  31. class Net_IPv6 {
  32.  
  33.     // {{{ Uncompress()
  34.  
  35.     /**
  36.      * Uncompresses an IPv6 adress
  37.      *
  38.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  39.      * function expects an valid IPv6 adress and expands the '::' to
  40.      * the required zeros.
  41.      *
  42.      * Example:  FF01::101    ->  FF01:0:0:0:0:0:0:101
  43.      *           ::1        ->  0:0:0:0:0:0:0:1
  44.      *
  45.      * @access public
  46.      * @see Compress()
  47.      * @static
  48.      * @param string $ip    a valid IPv6-adress (hex format)
  49.      * @return string    the uncompressed IPv6-adress (hex format)
  50.      */
  51.     function Uncompress($ip{
  52.         $uip $ip;
  53.         $c1 = -1;
  54.         $c2 = -1;
  55.         if (false !== strpos($ip'::') ) {
  56.             list($ip1$ip2explode('::'$ip);
  57.             if(""==$ip1{
  58.                 $c1 = -1;
  59.             else {
  60.                    $pos = 0;
  61.                 if(0 < ($pos substr_count($ip1':'))) {
  62.                     $c1 $pos;
  63.                 else {
  64.                     $c1 = 0;
  65.                 }
  66.             }
  67.             if(""==$ip2{
  68.                 $c2 = -1;
  69.             else {
  70.                 $pos = 0;
  71.                 if(0 < ($pos substr_count($ip2':'))) {
  72.                     $c2 $pos;
  73.                 else {
  74.                     $c2 = 0;
  75.                 }
  76.             }
  77.             if(strstr($ip2'.')) {
  78.                 $c2++;
  79.             }
  80.             if(-1 == $c1 && -1 == $c2// ::
  81.                 $uip "0:0:0:0:0:0:0:0";
  82.             else if(-1==$c1{              // ::xxx
  83.                 $fill str_repeat('0:'7-$c2);
  84.                 $uip =  str_replace('::'$fill$uip);
  85.             else if(-1==$c2{              // xxx::
  86.                 $fill str_repeat(':0'7-$c1);
  87.                 $uip =  str_replace('::'$fill$uip);
  88.             else {                          // xxx::xxx
  89.                 $fill str_repeat(':0:'6-$c2-$c1);
  90.                 $uip =  str_replace('::'$fill$uip);
  91.                 $uip =  str_replace('::'':'$uip);
  92.             }
  93.         }
  94.         return $uip;
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ Compress()
  99.  
  100.     /**
  101.      * Compresses an IPv6 adress
  102.      *
  103.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  104.      * function expects an valid IPv6 adress and compresses successive zeros
  105.      * to '::'
  106.      *
  107.      * Example:  FF01:0:0:0:0:0:0:101     -> FF01::101
  108.      *           0:0:0:0:0:0:0:1        -> ::1
  109.      *
  110.      * @access public
  111.      * @see Uncompress()
  112.      * @static
  113.      * @param string $ip    a valid IPv6-adress (hex format)
  114.      * @return string    the compressed IPv6-adress (hex format)
  115.      */
  116.     function Compress($ip)    {
  117.         $cip $ip;
  118.         if (!strstr($ip"::"&& strstr($ip'0:0')) {
  119.             $ipp explode(':',$ip);
  120.             for($i=0; $i<count($ipp)$i++{
  121.                 $ipp[$idechex(hexdec($ipp[$i]));
  122.                 if(hexdec($ipp[$i])>0{
  123.                     $ipp[$i]=$ipp[$i].'_';
  124.                 }
  125.             }
  126.             $cip join(':',$ipp);
  127.             $stop = false;
  128.             $pattern "0:0";
  129.             $pos=-1;
  130.  
  131.             while(!$stop{
  132.                    $pos strpos($cip$pattern);
  133.                 if($pos === false{
  134.                     $stop = true;
  135.                     $pos = -1;
  136.                 else {
  137.                     $pattern $pattern.":0";
  138.                 }
  139.             }
  140.  
  141.  
  142.             $cip preg_replace("/".substr($pattern,0,-2)."/"':'$cip,1);
  143.             if(1!=strlen($cip)) {
  144.                 $cip str_replace(':::''::'$cip);
  145.                 $cip str_replace('_'''$cip);
  146.             else {
  147.                 $cip "::";
  148.             }
  149.         }
  150.         return $cip;
  151.  
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ SplitV64()
  156.  
  157.     /**
  158.      * Splits an IPv6 adress into the IPv6 and a possible IPv4 part
  159.      *
  160.      * RFC 2373 allows you to note the last two parts of an IPv6 adress as
  161.      * an IPv4 compatible adress
  162.      *
  163.      * Example:  0:0:0:0:0:0:13.1.68.3
  164.      *           0:0:0:0:0:FFFF:129.144.52.38
  165.      *
  166.      * @access public
  167.      * @static
  168.      * @param string $ip    a valid IPv6-adress (hex format)
  169.      * @return array        [0] contains the IPv6 part, [1] the IPv4 part (hex format)
  170.      */
  171.     function SplitV64($ip{
  172.         $ip Net_IPv6::Uncompress($ip);
  173.         if (strstr($ip'.')) {
  174.             $pos strrpos($ip':');
  175.             $ip{$pos'_';
  176.             $ipPart explode('_'$ip);
  177.             return $ipPart;
  178.         else {
  179.             return array($ip"");
  180.         }
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ checkIPv6
  185.  
  186.     /**
  187.      * Checks an IPv6 adress
  188.      *
  189.      * Checks if the given IP is IPv6-compatible
  190.      *
  191.      * @access public
  192.      * @static
  193.      * @param string $ip    a valid IPv6-adress
  194.      * @return boolean    true if $ip is an IPv6 adress
  195.      */
  196.     function checkIPv6($ip{
  197.  
  198.         $ipPart Net_IPv6::SplitV64($ip);
  199.         $count = 0;
  200.         if (!empty($ipPart[0])) {
  201.             $ipv6 =explode(':'$ipPart[0]);
  202.             for ($i = 0; $i count($ipv6)$i++{
  203.                 $dec hexdec($ipv6[$i]);
  204.                 $hex strtoupper(preg_replace("/^[0]{1,3}(.*[0-9a-fA-F])$/""\\1"$ipv6[$i]));
  205.                 if ($ipv6[$i>= 0 && $dec <= 65535 && $hex == strtoupper(dechex($dec))) {
  206.                     $count++;
  207.                 }
  208.             }
  209.             if (8 == $count{
  210.                 return true;
  211.             elseif (6 == $count and !empty($ipPart[1])) {
  212.                 $ipv4 explode('.',$ipPart[1]);
  213.                 $count = 0;
  214.                 for ($i = 0; $i count($ipv4)$i++{
  215.                     if ($ipv4[$i>= 0 && (integer)$ipv4[$i<= 255 && preg_match("/^\d{1,3}$/"$ipv4[$i])) {
  216.                         $count++;
  217.                     }
  218.                 }
  219.                 if (4 == $count{
  220.                     return true;
  221.                 }
  222.             else {
  223.                 return false;
  224.             }
  225.  
  226.         else {
  227.             return false;
  228.         }
  229.     }
  230.     // }}}
  231. }
  232. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.