"Invalid type given, value should be a string", self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address" ); /** * Defined by Zend_Validate_Interface * * Returns true if and only if $value is a valid IP address * * @param mixed $value * @return boolean */ public function isValid($value) { if (!is_string($value)) { $this->_error(self::INVALID); return false; } $this->_setValue($value); if (!$this->_validateIPv4($value) && !$this->_validateIPv6($value)) { $this->_error(self::NOT_IP_ADDRESS); return false; } return true; } /** * Validates an IPv4 address * * @param string $value */ protected function _validateIPv4($value) { $ip2long = ip2long($value); if($ip2long === false) { return false; } return $value == long2ip($ip2long); } /** * Validates an IPv6 address * * @param string $value Value to check against * @return boolean True when $value is a valid ipv6 address * False otherwise */ protected function _validateIPv6($value) { if (strlen($value) < 3) { return $value == '::'; } if (strpos($value, '.')) { $lastcolon = strrpos($value, ':'); if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) { return false; } $value = substr($value, 0, $lastcolon) . ':0:0'; } if (strpos($value, '::') === false) { return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\Z/i', $value); } $colonCount = substr_count($value, ':'); if ($colonCount < 8) { return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\Z/i', $value); } // special case with ending or starting double colon if ($colonCount == 8) { return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\Z/i', $value); } return false; } }