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.9 2005/02/13 13:04:51 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.9 2005/02/13 13:04:51 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(-1 == $c1 && -1 == $c2// ::
  78.                 $uip "0:0:0:0:0:0:0:0";
  79.             else if(-1==$c1{              // ::xxx
  80.                 $fill str_repeat('0:'7-$c2);
  81.                 $uip =  str_replace('::'$fill$uip);
  82.             else if(-1==$c2{              // xxx::
  83.                 $fill str_repeat(':0'7-$c1);
  84.                 $uip =  str_replace('::'$fill$uip);
  85.             else {                          // xxx::xxx
  86.                 $fill str_repeat(':0:'6-$c2-$c1);
  87.                 $uip =  str_replace('::'$fill$uip);
  88.                 $uip =  str_replace('::'':'$uip);
  89.             }
  90.         }
  91.         return $uip;
  92.     }
  93.  
  94.     // }}}
  95.     // {{{ Compress()
  96.  
  97.     /**
  98.      * Compresses an IPv6 adress
  99.      *
  100.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  101.      * function expects an valid IPv6 adress and compresses successive zeros
  102.      * to '::'
  103.      *
  104.      * Example:  FF01:0:0:0:0:0:0:101     -> FF01::101
  105.      *           0:0:0:0:0:0:0:1        -> ::1
  106.      *
  107.      * @access public
  108.      * @see Uncompress()
  109.      * @static
  110.      * @param string $ip    a valid IPv6-adress (hex format)
  111.      * @return string    the compressed IPv6-adress (hex format)
  112.      */
  113.     function Compress($ip)    {
  114.         $cip $ip;
  115.         if (!strstr($ip"::")) {
  116.             $ipp explode(':',$ip);
  117.             for($i=0; $i<count($ipp)$i++{
  118.                 $ipp[$idechex(hexdec($ipp[$i]));
  119.                 if(hexdec($ipp[$i])>0{
  120.                     $ipp[$i]=$ipp[$i].'_';
  121.                 }
  122.             }
  123.             $cip join(':',$ipp);
  124.             $stop = false;
  125.             $pattern "0:0";
  126.             $pos=-1;
  127.  
  128.             while(!$stop{
  129.                    $pos strpos($cip$pattern);
  130.                 if($pos === false{
  131.                     $stop = true;
  132.                     $pos = -1;
  133.                 else {
  134.                     $pattern $pattern.":0";
  135.                 }
  136.             }
  137.             $cip preg_replace("/".substr($pattern,0,-2)."/"':'$cip,1);
  138.             if(1!=strlen($cip)) {
  139.                 $cip str_replace(':::''::'$cip);
  140.                 $cip str_replace('_'''$cip);
  141.             else {
  142.                 $cip "::";
  143.             }
  144.  
  145.         }
  146.         return $cip;
  147.  
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ SplitV64()
  152.  
  153.     /**
  154.      * Splits an IPv6 adress into the IPv6 and a possible IPv4 part
  155.      *
  156.      * RFC 2373 allows you to note the last two parts of an IPv6 adress as
  157.      * an IPv4 compatible adress
  158.      *
  159.      * Example:  0:0:0:0:0:0:13.1.68.3
  160.      *           0:0:0:0:0:FFFF:129.144.52.38
  161.      *
  162.      * @access public
  163.      * @static
  164.      * @param string $ip    a valid IPv6-adress (hex format)
  165.      * @return array        [0] contains the IPv6 part, [1] the IPv4 part (hex format)
  166.      */
  167.     function SplitV64($ip{
  168.         $ip Net_IPv6::Uncompress($ip);
  169.         if (strstr($ip'.')) {
  170.             $pos strrpos($ip':');
  171.             $ip{$pos'_';
  172.             $ipPart explode('_'$ip);
  173.             return $ipPart;
  174.         else {
  175.             return array($ip"");
  176.         }
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ checkIPv6
  181.  
  182.     /**
  183.      * Checks an IPv6 adress
  184.      *
  185.      * Checks if the given IP is IPv6-compatible
  186.      *
  187.      * @access public
  188.      * @static
  189.      * @param string $ip    a valid IPv6-adress
  190.      * @return boolean    true if $ip is an IPv6 adress
  191.      */
  192.     function checkIPv6($ip{
  193.  
  194.         $ipPart Net_IPv6::SplitV64($ip);
  195.         $count = 0;
  196.         if (!empty($ipPart[0])) {
  197.             $ipv6 =explode(':'$ipPart[0]);
  198.             for ($i = 0; $i count($ipv6)$i++{
  199.                 $dec hexdec($ipv6[$i]);
  200.                 $hex strtoupper(preg_replace("/^[0]{1,3}(.*[0-9a-fA-F])$/""\\1"$ipv6[$i]));
  201.                 if ($ipv6[$i>= 0 && $dec <= 65535 && $hex == strtoupper(dechex($dec))) {
  202.                     $count++;
  203.                 }
  204.             }
  205.             if (8 == $count{
  206.                 return true;
  207.             elseif (6 == $count and !empty($ipPart[1])) {
  208.                 $ipv4 explode('.',$ipPart[1]);
  209.                 $count = 0;
  210.                 for ($i = 0; $i count($ipv4)$i++{
  211.                     if ($ipv4[$i>= 0 && (integer)$ipv4[$i<= 255 && preg_match("/^\d{1,3}$/"$ipv4[$i])) {
  212.                         $count++;
  213.                     }
  214.                 }
  215.                 if (4 == $count{
  216.                     return true;
  217.                 }
  218.             else {
  219.                 return false;
  220.             }
  221.  
  222.         else {
  223.             return false;
  224.         }
  225.     }
  226.     // }}}
  227. }
  228. ?>

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