source: trunk/Common/NetworkAddress.php@ 668

Last change on this file since 668 was 548, checked in by chronos, 12 years ago
  • Odstraněno: Ukončovací PHP značka.
  • Property svn:executable set to *
File size: 1.6 KB
Line 
1<?php
2
3class NetworkAddressIPv4
4{
5 var $Address;
6 var $Prefix;
7
8 function __construct()
9 {
10 $this->Address = 0;
11 $this->Prefix = 0;
12 }
13
14 function GetNetMask()
15 {
16 return(0xffffffff ^ ((1 << (32 - $this->Prefix)) - 1));
17 }
18
19 function AddressToString()
20 {
21 return(implode('.', array(($this->Address >> 24) & 255, ($this->Address >> 16) & 255, ($this->Address >> 8) & 255, ($this->Address & 255))));
22 }
23
24 function AddressFromString($Value)
25 {
26 $Parts = explode('.', $Value);
27 $this->Address = ($Parts[0] << 24) | ($Parts[1] << 16) | ($Parts[2] << 8) | $Parts[3];
28 }
29
30 function GetRange()
31 {
32 $From = new NetworkAddressIPv4();
33 $From->Address = $this->Address;
34 $From->Prefix = 32;
35 $HostMask = 0xffffffff ^ $this->GetNetMask();
36 $To = new NetworkAddressIPv4();
37 $To->Address = $From->Address + $HostMask;
38 $To->Prefix = 32;
39 return(array('From' => $From, 'To' => $To));
40 }
41
42 function ChangePrefix($NewPrefix)
43 {
44 $this->Prefix = $NewPrefix;
45 if($this->Prefix > 32) $this->Prefix = 32;
46 if($this->Prefix < 0) $this->Prefix = 0;
47 $this->Address = $this->Address & $this->GetNetMask();
48 }
49
50 function Contain($Address)
51 {
52 $UpperNetmask = $this->GetNetMask();
53 if(($this->Prefix < $Address->Prefix) and (($Address->Address & $UpperNetmask) == ($this->Address & $UpperNetmask))) $Result = true;
54 else $Result = false;
55 //echo($Address->AddressToString().'/'.$Address->Prefix.' in '.$this->AddressToString().'/'.$this->Prefix.' '.$Result."\n");
56 return($Result);
57 }
58}
Note: See TracBrowser for help on using the repository browser.