Changeset 7768 for trunk/src/inc/Network.class.php
- Timestamp:
- Apr 15, 2010, 5:24:10 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/inc/Network.class.php
r7766 r7768 8 8 class Network { 9 9 public $netmaskNumber; // Netmask number 10 public $netmaskBina ir; // Binairvalue of netmask address10 public $netmaskBinary; // Binary value of netmask address 11 11 public $netmaskDecimal; // Decimal value of netmask address 12 public $wildcardBina ir; // Binairvalue of wildcard address12 public $wildcardBinary; // Binary value of wildcard address 13 13 public $wildcardDecimal; // Decimal value of wildcard address 14 public $networkBina ir; // Binairvalue of network address14 public $networkBinary; // Binary value of network address 15 15 public $networkDecimal; // Decimal value of network address 16 public $broadcastBina ir; // Binairvalue of broadcast address16 public $broadcastBinary; // Binary value of broadcast address 17 17 public $broadcastDecimal; // Decimal value of broadcast address 18 public $hostminBina ir; // Binairvalue of hostmin address18 public $hostminBinary; // Binary value of hostmin address 19 19 public $hostminDecimal; // Decimal value of hostmin address 20 public $hostmaxBina ir; // Binairvalue of hostmax address20 public $hostmaxBinary; // Binary value of hostmax address 21 21 public $hostmaxDecimal; // Decimal value of hostmax address 22 22 public $numberHosts; // Number of possible hosts in this network … … 29 29 */ 30 30 public function __construct($ip_address, $ip_netmask) { 31 // Bina irvalue of given IP address32 $bina ir= $this->binIP($ip_address);31 // Binary value of given IP address 32 $binary = $this->binIP($ip_address); 33 33 34 34 // Let's store the IP netmask number … … 36 36 37 37 // Netmask address 38 $this->netmaskBina ir= str_repeat('1', $ip_netmask) . str_repeat('0', 32 - $ip_netmask);39 $this->netmaskDecimal = $this->decIP($this->netmaskBina ir);38 $this->netmaskBinary = str_repeat('1', $ip_netmask) . str_repeat('0', 32 - $ip_netmask); 39 $this->netmaskDecimal = $this->decIP($this->netmaskBinary); 40 40 41 41 // Wildcard address 42 $this->wildcardBina ir= str_repeat('0', $ip_netmask) . str_repeat('1', 32 - $ip_netmask);43 $this->wildcardDecimal = $this->decIP($this->wildcardBina ir);42 $this->wildcardBinary = str_repeat('0', $ip_netmask) . str_repeat('1', 32 - $ip_netmask); 43 $this->wildcardDecimal = $this->decIP($this->wildcardBinary); 44 44 45 45 // Network address 46 $this->networkBina ir = substr($binair, 0, $ip_netmask) . str_repeat('0', 32 - $ip_netmask);47 $this->networkDecimal = $this->decIP($this->networkBina ir);46 $this->networkBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 32 - $ip_netmask); 47 $this->networkDecimal = $this->decIP($this->networkBinary); 48 48 49 49 // Broadcast address 50 $this->broadcastBina ir = substr($binair, 0, $ip_netmask) . str_repeat('1', 32 - $ip_netmask);51 $this->broadcastDecimal = $this->decIP($this->broadcastBina ir);50 $this->broadcastBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 32 - $ip_netmask); 51 $this->broadcastDecimal = $this->decIP($this->broadcastBinary); 52 52 53 53 // HostMin address 54 $this->hostminBina ir = substr($binair, 0, $ip_netmask) . str_repeat('0', 31 - $ip_netmask) . '1';55 $this->hostminDecimal = $this->decIP($this->hostminBina ir);54 $this->hostminBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 31 - $ip_netmask) . '1'; 55 $this->hostminDecimal = $this->decIP($this->hostminBinary); 56 56 57 57 // HostMax address 58 $this->hostmaxBina ir = substr($binair, 0, $ip_netmask) . str_repeat('1', 31 - $ip_netmask) . '0';59 $this->hostmaxDecimal = $this->decIP($this->hostmaxBina ir);58 $this->hostmaxBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 31 - $ip_netmask) . '0'; 59 $this->hostmaxDecimal = $this->decIP($this->hostmaxBinary); 60 60 61 61 // Numbers of hosts 62 $this->numberHosts = bindec(substr($this->broadcastBina ir, $ip_netmask)) - bindec(substr($this->networkBinair, $ip_netmask)) - 1;62 $this->numberHosts = bindec(substr($this->broadcastBinary, $ip_netmask)) - bindec(substr($this->networkBinary, $ip_netmask)) - 1; 63 63 } 64 64 65 /* 66 * Function: binIP 67 * Description: Converts a IP address like 192.168.0.1 to it's binary value 68 * Parameters: string $address 69 * Returns: string $address_binary 70 */ 65 71 function binIP($address) { 66 72 $address_array = explode('.', $address); 67 $address_bina ir= '';73 $address_binary = ''; 68 74 69 75 for ($i = 0; $i < 4; $i++) { 70 // Create bina irvalue71 $bina ir= decbin($address_array[$i]);76 // Create binary value 77 $binary = decbin($address_array[$i]); 72 78 73 79 // Trim value to the length 74 $bina ir = trim($binair, 8);80 $binary = trim($binary, 8); 75 81 76 82 // Add zeros at the left to fit to $size 77 $bina ir = str_repeat('0', 8 - strlen($binair)) . $binair;83 $binary = str_repeat('0', 8 - strlen($binary)) . $binary; 78 84 79 $address_bina ir .= $binair;85 $address_binary .= $binary; 80 86 } 81 87 82 return $address_bina ir;88 return $address_binary; 83 89 } 84 90 85 function decIP($binair) { 91 /* 92 * Function: decIP 93 * Description: Converts a binary IP address to it's decimal value like 192.168.0.1 94 * Parameters: string $binary 95 * Returns: string $address 96 */ 97 function decIP($binary) { 86 98 $address_array = array(); 87 99 88 100 for ($i = 0; $i < 4; $i++) { 89 $block = substr($bina ir, $i * 8, 8) . '<br/>';101 $block = substr($binary, $i * 8, 8) . '<br/>'; 90 102 $address_array[] = bindec(intval($block)); 91 103 } 92 104 93 return implode('.', $address_array); 105 $address = implode('.', $address_array); 106 107 return $address; 94 108 } 95 109 96 function inNetwork($ip_address) { 97 return false; 110 /* 111 * Function: compare 112 * Description: Checks if to networks are the same using network IP address 113 * Parameters: Network $otherNetwork 114 * Returns: true if the networks are the same, otherwise false 115 */ 116 function compare(Network $otherNetwork) { 117 $check = false; 118 119 // Compare the network address 120 if ($this->networkDecimal == $otherNetwork->networkDecimal) { 121 $check = true; 122 } 123 124 return $check; 125 } 126 127 /* 128 * Function: inNetwork 129 * Description: Checks if the given IP is in the IP range of the current network 130 * Parameters: string $ipDecimal 131 * Returns: true if the IP address is in the IP range, otherwise false 132 */ 133 function inNetwork($ipDecimal) { 134 $check = true; 135 136 // Check if given IP is bigger than our HostMin IP Address 137 if (ip2long($ipDecimal) < ip2long($this->hostminDecimal)) { 138 $check = false; 139 } 140 141 // Check if given IP is smaller than our HostMax IP Address 142 if (ip2long($ipDecimal) > ip2long($this->hostmaxDecimal)) { 143 $check = false; 144 } 145 146 return $check; 98 147 } 99 148 } 100 101 $ip = '172.16.0.245';102 $network = new Network($ip, 30);103 echo '104 Address: ' . $ip . ' / ' . Network::binIP($ip) . '<br/>105 Netmask: ' . $network->netmaskDecimal . ' / ' . $network->netmaskBinair . '<br/>106 Wildcard: ' . $network->wildcardDecimal . ' / ' . $network->wildcardBinair . '<br/>107 Network: ' . $network->networkDecimal . ' / ' . $network->networkBinair . '<br/>108 Broadcast: ' . $network->broadcastDecimal . ' / ' . $network->broadcastBinair . '<br/>109 HostMin: ' . $network->hostminDecimal . ' / ' . $network->hostminBinair . '<br/>110 HostMax: ' . $network->hostmaxDecimal . ' / ' . $network->hostmaxBinair . '<br/>111 Hosts/Net: ' . $network->numberHosts . '<br/>';112 149 ?>
Note:
See TracChangeset
for help on using the changeset viewer.