Exemple #1
0
 /**
  * sets new ip address
  *
  * @param string $ip
  * @throws Zend_Service_DeveloperGarden_Exception
  * @return Zend_Service_DeveloperGarden_IpLocation_IpAddress
  */
 public function setIp($ip)
 {
     $validator = new Zend\Validator\Ip();
     if (!$validator->isValid($ip)) {
         $message = $validator->getMessages();
         throw new Zend_Service_DeveloperGarden_Exception($message['notIpAddress']);
     }
     $this->_address = $ip;
     return $this;
 }
 /**
  * Validate address and mask
  *
  * @param string $address
  * @param string $mask
  * @throws \UnexpectedValueException if $address or $mask are invalid
  */
 protected function _validate($address, $mask)
 {
     $validator = new \Zend\Validator\Ip(array('allowipv6' => false));
     if (!$validator->isValid($address)) {
         $messages = $validator->getMessages();
         throw new \UnexpectedValueException(sprintf('Not an IPv4 address: "%s" (%s)', $address, array_shift($messages)));
     }
     // Check $mask for valid syntax and consecutive leading 1 bits.
     // The explicit 0 check is required on 32 bit systems.
     $bits = ip2long($mask);
     if ($bits === false or !($bits === 0 or ctype_digit((string) log(($bits ^ 4294967295.0) + 1, 2)))) {
         throw new \UnexpectedValueException(sprintf('Not an IPv4 mask: "%s"', $address));
     }
 }