IpNetwork.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Vladimir A. Altuchov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: IpNetwork.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class IpNetwork implements SingleRange
00017     {
00018         const MASK_MAX_SIZE = 31;
00019         
00020         private $ip         = null;
00021         private $end        = null;
00022         private $mask       = null;
00023         private $longMask   = null;
00024         
00028         public static function create(IpAddress $ip, $mask)
00029         {
00030             return new self($ip, $mask);
00031         }
00032         
00033         public function __construct(IpAddress $ip, $mask)
00034         {
00035             Assert::isInteger($mask);
00036             
00037             if ($mask == 0 || self::MASK_MAX_SIZE < $mask)
00038                 throw new WrongArgumentException('wrong mask given');
00039             
00040             $this->longMask =
00041                 (int) (pow(2, (32 - $mask)) * (pow(2, $mask) - 1));
00042             
00043             if (($ip->getLongIp() & $this->longMask) != $ip->getLongIp())
00044                 throw new WrongArgumentException('wrong ip network given');
00045             
00046             $this->ip = $ip;
00047             $this->mask = $mask;
00048         }
00049         
00050         public function getMask()
00051         {
00052             return $this->mask;
00053         }
00054         
00058         public function getStart()
00059         {
00060             return $this->ip;
00061         }
00062         
00066         public function getEnd()
00067         {
00068             if (!$this->end) {
00069                 $this->end =
00070                     IpAddress::create(
00071                         long2ip($this->ip->getLongIp() | ~$this->longMask)
00072                     );
00073             }
00074             
00075             return $this->end;
00076         }
00077         
00078         public function contains(/*IpAddress*/ $probe)
00079         {
00080             return
00081                 ($probe->getLongIp() & $this->longMask)
00082                 == $this->ip->getLongIp();
00083         }
00084     }
00085 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4