home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / IPv4.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  10.5 KB  |  314 lines

  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: Eric Kilfoil <eric@ypass.net>                               |
  17. // +----------------------------------------------------------------------+
  18. //
  19.  
  20. require_once("PEAR.php");
  21.  
  22. /**
  23.  * Map of bitmasks to subnets
  24.  *
  25.  * This array contains every valid netmask.  The index of the dot quad
  26.  * netmask value is the corresponding CIDR notation (bitmask).
  27.  */
  28. $Net_IPv4_Netmask_Map = array(
  29.             0 => "0.0.0.0",
  30.             1 => "128.0.0.0",
  31.             2 => "192.0.0.0",
  32.             3 => "224.0.0.0",
  33.             4 => "240.0.0.0",
  34.             5 => "248.0.0.0",
  35.             6 => "252.0.0.0",
  36.             7 => "254.0.0.0",
  37.             8 => "255.0.0.0",
  38.             9 => "255.128.0.0",
  39.             10 => "255.192.0.0",
  40.             11 => "255.224.0.0",
  41.             12 => "255.240.0.0",
  42.             13 => "255.248.0.0",
  43.             14 => "255.252.0.0",
  44.             15 => "255.254.0.0",
  45.             16 => "255.255.0.0",
  46.             17 => "255.255.128.0",
  47.             18 => "255.255.192.0",
  48.             19 => "255.255.224.0",
  49.             20 => "255.255.240.0",
  50.             21 => "255.255.248.0",
  51.             22 => "255.255.252.0",
  52.             23 => "255.255.254.0",
  53.             24 => "255.255.255.0",
  54.             25 => "255.255.255.128",
  55.             26 => "255.255.255.192",
  56.             27 => "255.255.255.224",
  57.             28 => "255.255.255.240",
  58.             29 => "255.255.255.248",
  59.             30 => "255.255.255.252",
  60.             31 => "255.255.255.254",
  61.             32 => "255.255.255.255"
  62.         );
  63.  
  64.  
  65. /**
  66. * Class to provide IPv4 calculations
  67. *
  68. * Provides methods for validating IP addresses, calculating netmasks,
  69. * broadcast addresses, network addresses, conversion routines, etc.
  70. *
  71. * @author  Eric Kilfoil <edk@ypass.net>
  72. * @package Net_IPv4
  73. * @version 1.0
  74. * @access  public
  75. */
  76. class Net_IPv4 extends PEAR
  77. {
  78.     var $ip = "";
  79.     var $bitmask = false;
  80.     var $netmask = "";
  81.     var $network = "";
  82.     var $broadcast = "";
  83.     var $long = 0;
  84.  
  85.     /**
  86.      * Validate the syntax of the given IP adress
  87.      *
  88.      * Using the PHP long2ip() and ip2long() functions, convert the IP
  89.      * address from a string to a long and back.  If the original still
  90.      * matches the converted IP address, it's a valid address.  This
  91.      * function does not allow for IP addresses to be formatted as long
  92.      * integers.
  93.      *
  94.      * @param  string $ip IP address in the format x.x.x.x
  95.      * @return bool       true if syntax is valid, otherwise false
  96.      */
  97.     function validateIP($ip)
  98.     {
  99.         if ($ip == long2ip(ip2long($ip))) {
  100.             return(TRUE);
  101.         } else {
  102.             return(FALSE);
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Validate the syntax of the given IP address (compatibility)
  108.      *
  109.      * This function is identical to Net_IPv4::validateIP().  It is included
  110.      * merely for compatibility reasons.
  111.      *
  112.      * @param  string $ip IP address
  113.      * @return bool       true if syntax is valid, otherwise false
  114.      */
  115.     function check_ip($ip)
  116.     {
  117.         return($this->validateIP($ip));
  118.     }
  119.  
  120.     /**
  121.      * Validate the syntax of a four octet netmask
  122.      *
  123.      * There are 33 valid netmask values.  This function will compare the
  124.      * string passed as $netmask to the predefined 33 values and return
  125.      * true or false.  This is most likely much faster than performing the
  126.      * calculation to determine the validity of the netmask.
  127.      *
  128.      * @param  string $netmask Netmask
  129.      * @return bool       true if syntax is valid, otherwise false
  130.      */
  131.     function validateNetmask($netmask)
  132.     {
  133.         global $Net_IPv4_Netmask_Map;
  134.         if (! in_array($netmask, $Net_IPv4_Netmask_Map)) {
  135.             return(FALSE);
  136.         }
  137.         return(TRUE);
  138.     }
  139.  
  140.     /**
  141.      * Parse a formatted IP address
  142.      *
  143.      * Given a network qualified IP address, attempt to parse out the parts
  144.      * and calculate qualities of the address.
  145.      *
  146.      * The following formats are possible:
  147.      *
  148.      * [dot quad ip]/[ bitmask ]
  149.      * [dot quad ip]/[ dot quad netmask ]
  150.      * [dot quad ip]/[ hex string netmask ]
  151.      *
  152.      * The first would be [IP Address]/[BitMask]:
  153.      * 192.168.0.0/16
  154.      *
  155.      * The second would be [IP Address] [Subnet Mask in quad dot notation]:
  156.      * 192.168.0.0/255.255.0.0
  157.      *
  158.      * The third would be [IP Address] [Subnet Mask as Hex string]
  159.      * 192.168.0.0/ffff0000
  160.      *
  161.      * Usage:
  162.      *
  163.      * $cidr = '192.168.0.50/16';
  164.      * $net = Net_IPv4::parseAddress($cidr);
  165.      * echo $net->network; // 192.168.0.0
  166.      * echo $net->ip; // 192.168.0.50
  167.      * echo $net->broadcast; // 192.168.255.255
  168.      * echo $net->bitmask; // 16
  169.      * echo $net->long; // 3232235520 (long/double version of 192.168.0.50)
  170.      * echo $net->netmask; // 255.255.0.0
  171.      *
  172.      * @param  string $ip IP address netmask combination
  173.      * @return object     true if syntax is valid, otherwise false
  174.      */
  175.     function parseAddress($address)
  176.     {
  177.         $myself = new Net_IPv4;
  178.         if (strchr($address, "/")) {
  179.             $parts = explode("/", $address);
  180.             if (! $myself->validateIP($parts[0])) {
  181.                 return($myself->raiseError("invalid IP address"));
  182.             }
  183.             $myself->ip = $parts[0];
  184.  
  185.             // Check the style of netmask that was entered 
  186.             /*
  187.              *  a hexadecimal string was entered
  188.              */
  189.             if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$", $parts[1], $regs)) {
  190.                 // hexadecimal string
  191.                 $myself->netmask = hexdec($regs[1]) . "." .  hexdec($regs[2]) . "." .
  192.                     hexdec($regs[3]) . "." .  hexdec($regs[4]);
  193.  
  194.             /*
  195.              *  a standard dot quad netmask was entered.
  196.              */
  197.             } else if (strchr($parts[1], ".")) {
  198.                 if (! $myself->validateNetmask($parts[1])) {
  199.                     return($myself->raiseError("invalid netmask value"));
  200.                 }
  201.                 $myself->netmask = $parts[1];
  202.  
  203.             /*
  204.              *  a CIDR bitmask type was entered
  205.              */
  206.             } else if ($parts[1] > 0 && $parts[1] <= 32) {
  207.                 // bitmask was entered
  208.                 $myself->bitmask = $parts[1];
  209.  
  210.             /*
  211.              *  Some unknown format of netmask was entered
  212.              */
  213.             } else {
  214.                 return($myself->raiseError("invalid netmask value"));
  215.             }
  216.             $myself->calculate();
  217.             return($myself);
  218.         } else if ($myself->validateIP($address)) {
  219.             $myself->ip = $address;
  220.             return($myself);
  221.         } else {
  222.             return($myself->raiseError("invalid IP address"));
  223.         }
  224.     }
  225.     
  226.     /**
  227.      * Calculates network information based on an IP address and netmask.
  228.      *
  229.      * Fully populates the object properties based on the IP address and
  230.      * netmask/bitmask properties.  Once these two fields are populated,
  231.      * calculate() will perform calculations to determine the network and
  232.      * broadcast address of the network.
  233.      *
  234.      * @return mixed     true if no errors occured, otherwise PEAR_Error object
  235.      */
  236.     function calculate() {
  237.         $validNM = $GLOBALS["Net_IPv4_Netmask_Map"];
  238.  
  239.         if (! is_a($this, "net_ipv4")) {
  240.             $myself = new Net_IPv4;
  241.             return($myself->raiseError("cannot calculate on uninstantiated Net_IPv4 class"));
  242.         }
  243.  
  244.         /* Find out if we were given an ip address in dot quad notation or
  245.          * a network long ip address.  Whichever was given, populate the
  246.          * other field
  247.          */
  248.         if (strlen($this->ip)) {
  249.             if (! $this->validateIP($this->ip)) {
  250.                 return($this->raiseError("invalid IP address"));
  251.             }
  252.             $this->long = $this->ip2double($this->ip);
  253.         } else if (is_numeric($this->long)) {
  254.             $this->ip = long2ip($this->long);
  255.         } else {
  256.            return($this->raiseError("ip address not specified"));
  257.         }
  258.  
  259.         /*
  260.          * Check to see if we were supplied with a bitmask or a netmask.
  261.          * Populate the other field as needed.
  262.          */
  263.         if (strlen($this->bitmask)) {
  264.             $this->netmask = $validNM[$this->bitmask];
  265.         } else if (strlen($this->netmask)) {
  266.             $validNM_rev = array_flip($validNM);
  267.             $this->bitmask = $validNM_rev[$this->netmask];
  268.         } else {
  269.             return($this->raiseError("netmask or bitmask are required for calculation"));
  270.         }
  271.         $this->network = long2ip(ip2long($this->ip) & ip2long($this->netmask));
  272.         $this->broadcast = long2ip(ip2long($this->ip) |
  273.                 (ip2long($this->netmask) ^ ip2long("255.255.255.255")));
  274.         return(TRUE);
  275.     }
  276.  
  277.     /**
  278.      * Converts a dot-quad formmated IP address into a hexadecimal string
  279.      */
  280.     function atoh($addr)
  281.     {
  282.         if (! Net_IPv4::validateIP($addr)) {
  283.             return(FALSE);
  284.         }
  285.         $ap = explode(".", $addr);
  286.         return(sprintf("%02x%02x%02x%02x",
  287.                     $ap[0], $ap[1],
  288.                     $ap[2], $ap[3]));
  289.     }
  290.  
  291.     /**
  292.      * Converts a hexadecimal string into a dot-quad formatted IP address
  293.      */
  294.     function htoa($addr)
  295.     {
  296.         if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$",
  297.                     $addr, $regs)) {
  298.             return(hexdec($regs[1]) . "." .  hexdec($regs[2]) . "." .
  299.                 hexdec($regs[3]) . "." .  hexdec($regs[4]));
  300.         }
  301.         return(FALSE);
  302.     }
  303.     /**
  304.      * Converts an IP address to a PHP double.  Better than ip2long because
  305.      * a long in PHP is a signed integer.
  306.      */
  307.     function ip2double($ip)
  308.     {
  309.         return((double)(sprintf("%u", ip2long($ip))));
  310.     }
  311. }
  312.  
  313. ?>
  314.