binIP($ip_address);
// Let's store the IP netmask number
$this->netmaskNumber = $ip_netmask;
// Netmask address
$this->netmaskBinary = str_repeat('1', $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
$this->netmaskDecimal = $this->decIP($this->netmaskBinary);
// Wildcard address
$this->wildcardBinary = str_repeat('0', $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
$this->wildcardDecimal = $this->decIP($this->wildcardBinary);
// Network address
$this->networkBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
$this->networkDecimal = $this->decIP($this->networkBinary);
// Broadcast address
$this->broadcastBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
$this->broadcastDecimal = $this->decIP($this->broadcastBinary);
// HostMin address
$this->hostminBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 31 - $ip_netmask) . '1';
$this->hostminDecimal = $this->decIP($this->hostminBinary);
// HostMax address
$this->hostmaxBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 31 - $ip_netmask) . '0';
$this->hostmaxDecimal = $this->decIP($this->hostmaxBinary);
// Numbers of hosts
$this->numberHosts = bindec(substr($this->broadcastBinary, $ip_netmask)) - bindec(substr($this->networkBinary, $ip_netmask)) - 1;
}
/*
* Function: binIP
* Description: Converts a IP address like 192.168.0.1 to it's binary value
* Parameters: string $address
* Returns: string $address_binary
*/
function binIP($address) {
$address_array = explode('.', $address);
$address_binary = '';
for ($i = 0; $i < 4; $i++) {
// Create binary value
$binary = decbin($address_array[$i]);
// Trim value to the length
$binary = trim($binary, 8);
// Add zeros at the left to fit to $size
$binary = str_repeat('0', 8 - strlen($binary)) . $binary;
$address_binary .= $binary;
}
return $address_binary;
}
/*
* Function: decIP
* Description: Converts a binary IP address to it's decimal value like 192.168.0.1
* Parameters: string $binary
* Returns: string $address
*/
function decIP($binary) {
$address_array = array();
for ($i = 0; $i < 4; $i++) {
$block = substr($binary, $i * 8, 8) . '
';
$address_array[] = bindec(intval($block));
}
$address = implode('.', $address_array);
return $address;
}
/*
* Function: compare
* Description: Checks if to networks are the same using network IP address
* Parameters: Network $otherNetwork
* Returns: true if the networks are the same, otherwise false
*/
function compare(Network $otherNetwork) {
$check = false;
// Compare the network address
if ($this->networkDecimal == $otherNetwork->networkDecimal) {
$check = true;
}
return $check;
}
/*
* Function: inNetwork
* Description: Checks if the given IP is in the IP range of the current network
* Parameters: string $ipDecimal
* Returns: true if the IP address is in the IP range, otherwise false
*/
function inNetwork($ipDecimal) {
$check = true;
// Check if given IP is bigger than our HostMin IP Address
if (ip2long($ipDecimal) < ip2long($this->hostminDecimal)) {
$check = false;
}
// Check if given IP is smaller than our HostMax IP Address
if (ip2long($ipDecimal) > ip2long($this->hostmaxDecimal)) {
$check = false;
}
return $check;
}
/*
* Function: toString
* Description: Converts the data of the network to a string
* Parameters: -
* Returns: string with all data
*/
function toString() {
return '
Netmask: ' . $this->netmaskDecimal . ' / ' . $this->netmaskBinary . '
Wildcard: ' . $this->wildcardDecimal . ' / ' . $this->wildcardBinary . '
Network: ' . $this->networkDecimal . ' / ' . $this->networkBinary . '
Broadcast: ' . $this->broadcastDecimal . ' / ' . $this->broadcastBinary . '
HostMin: ' . $this->hostminDecimal . ' / ' . $this->hostminBinary . '
HostMax: ' . $this->hostmaxDecimal . ' / ' . $this->hostmaxBinary . '
Hosts/Net: ' . $this->numberHosts . '
';
}
}
?>