[ Index ]

PHP Cross Reference of phool

title

Body

[close]

/Phool/Network/ -> IP.php (source)

   1  <?php
   2  //============================================================================
   3  // This program is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU Lesser General Public License (LGPL) as
   5  // published by the Free Software Foundation, either version 3 of the License,
   6  // or (at your option) any later version.
   7  //
   8  // This program is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU Lesser General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU Lesser General Public License
  14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15  //============================================================================
  16  /**
  17  * @copyright Francois Laupretre <phool@tekwire.net>
  18  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  19  * @category phool
  20  * @package phool
  21  */
  22  //============================================================================
  23  
  24  namespace Phool\Network;
  25  
  26  class IP
  27  {
  28  
  29  public function network($ip,$mask)
  30  {
  31  self::validate($ip);
  32  self::validate($mask);
  33  
  34  $a_ip=self::stringToArray($ip);
  35  $a_mask=self::stringToArray($mask);
  36  
  37  $a_res=array();
  38  for ($i=0;$i<4;$i++) $a_res[]=intval($a_ip[$i]) & intval($a_mask[$i]);
  39  
  40  return self::arrayToString($a_res);
  41  }
  42  
  43  //-----------
  44  
  45  public function validate($string)
  46  {
  47  $a=self::stringToArray($string);
  48  for ($i=0;$i<4;$i++)
  49      {
  50      $val=$a[$i];
  51      if ((!is_numeric($val))||($val < 0) || ($val > 255))
  52          throw new \Exception("$val/$string: Invalid IP address");
  53      }
  54  return $string;
  55  }
  56  
  57  //-----------
  58  
  59  public function stringToArray($string)
  60  {
  61  $res=explode('.',$string);
  62  if (count($res)!==4) throw new \Exception("$string: Invalid IP address");
  63  return $res;
  64  }
  65  
  66  //-----------
  67  
  68  public function arrayToString($a)
  69  {
  70  return implode('.',$a);
  71  }
  72  
  73  //-----------
  74  }
  75  ?>


Generated: Thu Jun 4 19:17:11 2015 Cross-referenced by PHPXref 0.7.1