[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/validator/rules/ -> iprangerule.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/data/validator/rules/rule.class.php");
   4  
   5      define( "ERROR_RULE_IP_NOT_IN_RANGE", "error_rule_ip_not_in_range");
   6  
   7      /**
   8       * \ingroup Validator_Rules
   9       *
  10       * This rule returns true if the given IP address is within a certain range. Use the constructor
  11       * or the setRange() method for setting the right range. The range should be given in a submask format,
  12       * so for example 1.2.3.4 would be within the range 1.2.3.255
  13       *
  14       * It will set the error flag ERROR_RULE_IP_NOT_IN_RANGE error if the address is not in the
  15       * given range.
  16       */
  17      class IpRangeRule extends Rule
  18      {
  19          var $_range;
  20  
  21          /**
  22           * Initializes the rule with the given range
  23           *
  24           * @param range The range.
  25           */
  26          function IpRangeRule($range)
  27          {
  28              $this->Rule();
  29              $this->_range = $range;
  30          }
  31  
  32          /**
  33           * Sets a different range than the one given in the constructor
  34           *
  35           * @param range the new range
  36           */
  37          function setRange($range)
  38          {
  39              $this->_range = $range;
  40          }
  41  
  42          /**
  43           * Returns the current range being used for the calculations
  44           *
  45           * @return A string representing the range
  46           */
  47          function getRange()
  48          {
  49              return $this->_range;
  50          }
  51  
  52          /**
  53           * Returns true if the address is within the given range or false otherwise. It will
  54           * also set the error ERROR_RULE_IP_NOT_IN_RANGE
  55           *
  56           * @param value The IP address to validate
  57           * @return True if within range or false otherwise
  58           */
  59          function validate($value)
  60          {
  61              $counter = 0;
  62              $range   = explode("/", $this->_range);
  63  
  64              if ($range[1] < 32)
  65              {
  66                  $maskBits  = $range[1];
  67                  $hostBits  = 32 - $maskBits;
  68                  $hostCount = pow(2, $hostBits) - 1;
  69                  $ipStart   = ip2long($range[0]);
  70                  $ipEnd     = $ipStart + $hostCount;
  71  
  72                  if ((ip2long($value) > $ipStart) && (ip2long($value) < $ipEnd))
  73                  {
  74                      $this->_setError(false);
  75                      return true;
  76                  }
  77              }
  78              elseif (ip2long($value) == ip2long($range[0]))
  79              {
  80                  $this->_setError(false);
  81                  return true;
  82              }
  83  
  84              $this->_setError(ERROR_RULE_IP_NOT_IN_RANGE);
  85              return false;
  86          }
  87      }
  88  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics