<?php
class IPCalc {
const NO_ERROR = 0;
const IP_ERROR = 1;
const NETMASK_ERROR = 2;
private $ip = array(0,0,0,0);
private $netmask = array(0,0,0,0);
private $wildcard = array(0,0,0,0);
private $netid = array(0,0,0,0);
private $broadcast = array(0,0,0,0);
private $hostmin = array(0,0,0,0);
private $hostmax = array(0,0,0,0);
private $nhosts = 0;
private $nnetmask = 24;
private $class = "";
private $error = self::IP_ERROR;
private $classes = array(
//min max 1st octet network=1,hosts=0
"A" => array(1, 126, 0, array(1,0,0,0)),
"A localhost" => array(127, 127, 0, array(1,0,0,0)),
"B" => array(128, 191, 10, array(1,1,0,0)),
"C" => array(192, 223, 110, array(1,1,1,0)),
"D" => array(224, 239, 1110, array()),
"E" => array(240, 254, 1111, array())
);
function setIp($ip) {
if( $this->checkValidIp( $ip ) ) {
$tmp = explode(".",$ip);
foreach( $tmp as $k => $v )
$this->ip[$k] = ( $v );
$this->error = self::NO_ERROR;
}
}
function getError() {
return $this->error;
}
function checkValidIp( $ip ) {
return (filter_var($ip, FILTER_VALIDATE_IP) !== false);
}
function setNetwork( $ip ) {
$tmp = explode("/",$ip);
$this->setIp($tmp[0]);
$this->setNetMask(count($tmp)>1?$tmp[1]:24);
$this->setWildcard();
$this->setNetId();
$this->setBroadcast();
$this->setHosts();
$this->setClass();
}
function setNetMask( $mask = 24 ) {
$this->nnetmask = (int)$mask;
if( $mask < 1 || $mask > 32 ) $this->error = self::NETMASK_ERROR;
for( $i = 0; $i < 32; $i++ ) {
$this->netmask[floor($i/8)] += ( $i < $mask ? pow(2, 7-$i%8) : 0 );
}
//foreach( $this->netmask as $k => $v ) $this->netmask[$k] = ($v);
}
function setWildcard() {
foreach( $this->netmask as $k => $v ) $this->wildcard[$k] = 255 ^ $v ;
}
function setNetId() {
foreach( $this->netmask as $k => $v ) $this->netid[$k] = $this->ip[$k] & $v ;
}
function setBroadcast() {
foreach( $this->wildcard as $k => $v ) $this->broadcast[$k] = $v | $this->ip[$k] ;
}
function setHosts() {
$this->hostmin = $this->netid;
$this->hostmin[3]++;
$this->hostmax = $this->broadcast;
$this->hostmax[3]--;
$this->nhosts = pow(2,32-$this->nnetmask) - 2;
}
function setClass() {
foreach( $this->classes as $c => $vals ) {
if( $this->ip[0] >= $vals[0] && $this->ip[0] <= $vals[1] ) $this->class = $c;
}
}
function getBinary($val) {
$tmp = array();
foreach( $val as $add ) {
array_push( $tmp, str_pad( decbin($add) , 8, "0", STR_PAD_LEFT ) );
}
return implode(".",$tmp);
}
function getDecimal($val) {
$tmp = array();
foreach( $val as $add ) {
array_push( $tmp, ($add) );
}
return implode(".",$tmp);
}
function getBinaryIp() {
return $this->getBinary( $this->ip );
}
function getBinaryMask() {
return $this->getBinary( $this->netmask );
}
function getBinaryWildcard() {
return $this->getBinary( $this->wildcard );
}
function getBinaryNetID() {
return $this->getBinary( $this->netid );
}
function getBinaryBroadcast() {
return $this->getBinary( $this->broadcast );
}
function getBinaryHostMax() {
return $this->getBinary( $this->hostmax );
}
function getBinaryHostMin() {
return $this->getBinary( $this->hostmin );
}
function getIp() {
return $this->getDecimal( $this->ip );
}
function getMask() {
return $this->getDecimal( $this->netmask );
}
function getWildcard() {
return $this->getDecimal( $this->wildcard );
}
function getNetID() {
return $this->getDecimal( $this->netid );
}
function getBroadcast() {
return $this->getDecimal( $this->broadcast );
}
function getHostMax() {
return $this->getDecimal( $this->hostmax );
}
function getHostMin() {
return $this->getDecimal( $this->hostmin );
}
function getNHosts() {
return $this->nhosts;
}
function getNNetMask() {
return $this->nnetmask;
}
function getClass($pos) {
if( $pos == -1 ) return $this->class;
else return $this->classes[$this->class][$pos];
}
}
?>