Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Validate_Ip Class Reference
Inheritance diagram for Zend_Validate_Ip:
Zend_Validate_Abstract Zend_Validate_Interface Ip

Public Member Functions

 __construct ($options=array())
 
 getOptions ()
 
 setOptions ($options)
 
 isValid ($value)
 
- Public Member Functions inherited from Zend_Validate_Abstract
 getMessages ()
 
 getMessageVariables ()
 
 getMessageTemplates ()
 
 setMessage ($messageString, $messageKey=null)
 
 setMessages (array $messages)
 
 __get ($property)
 
 getErrors ()
 
 setObscureValue ($flag)
 
 getObscureValue ()
 
 setTranslator ($translator=null)
 
 getTranslator ()
 
 hasTranslator ()
 
 setDisableTranslator ($flag)
 
 translatorIsDisabled ()
 

Data Fields

const INVALID = 'ipInvalid'
 
const NOT_IP_ADDRESS = 'notIpAddress'
 

Protected Member Functions

 _validateIPv4 ($value)
 
 _validateIPv6 ($value)
 
- Protected Member Functions inherited from Zend_Validate_Abstract
 _createMessage ($messageKey, $value)
 
 _implodeRecursive (array $pieces)
 
 _error ($messageKey, $value=null)
 
 _setValue ($value)
 

Protected Attributes

 $_messageTemplates
 
 $_options
 
- Protected Attributes inherited from Zend_Validate_Abstract
 $_value
 
 $_messageVariables = array()
 
 $_messageTemplates = array()
 
 $_messages = array()
 
 $_obscureValue = false
 
 $_errors = array()
 
 $_translator
 
 $_translatorDisabled = false
 

Additional Inherited Members

- Static Public Member Functions inherited from Zend_Validate_Abstract
static setDefaultTranslator ($translator=null)
 
static getDefaultTranslator ()
 
static hasDefaultTranslator ()
 
static getMessageLength ()
 
static setMessageLength ($length=-1)
 
- Static Protected Attributes inherited from Zend_Validate_Abstract
static $_defaultTranslator
 
static $_messageLength = -1
 

Detailed Description

Definition at line 33 of file Ip.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options = array())

Sets validator options

Parameters
array$optionsOPTIONAL Options to set, see the manual for all available options

Definition at line 61 of file Ip.php.

62  {
63  if ($options instanceof Zend_Config) {
64  $options = $options->toArray();
65  } else if (!is_array($options)) {
66  $options = func_get_args();
67  $temp['allowipv6'] = array_shift($options);
68  if (!empty($options)) {
69  $temp['allowipv4'] = array_shift($options);
70  }
71 
72  $options = $temp;
73  }
74 
76  $this->setOptions($options);
77  }
setOptions($options)
Definition: Ip.php:96

Member Function Documentation

◆ _validateIPv4()

_validateIPv4 (   $value)
protected

Validates an IPv4 address

Parameters
string$value
Returns
bool

Definition at line 146 of file Ip.php.

146  {
147  $ip2long = ip2long($value);
148  if($ip2long === false) {
149  return false;
150  }
151 
152  return $value == long2ip($ip2long);
153  }
$value
Definition: gender.phtml:16

◆ _validateIPv6()

_validateIPv6 (   $value)
protected

Validates an IPv6 address

Parameters
string$valueValue to check against
Returns
boolean True when $value is a valid ipv6 address False otherwise

Definition at line 162 of file Ip.php.

162  {
163  if (strlen($value) < 3) {
164  return $value == '::';
165  }
166 
167  if (strpos($value, '.')) {
168  $lastcolon = strrpos($value, ':');
169  if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) {
170  return false;
171  }
172 
173  $value = substr($value, 0, $lastcolon) . ':0:0';
174  }
175 
176  if (strpos($value, '::') === false) {
177  return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $value);
178  }
179 
180  $colonCount = substr_count($value, ':');
181  if ($colonCount < 8) {
182  return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $value);
183  }
184 
185  // special case with ending or starting double colon
186  if ($colonCount == 8) {
187  return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $value);
188  }
189 
190  return false;
191  }
$value
Definition: gender.phtml:16
_validateIPv4($value)
Definition: Ip.php:146

◆ getOptions()

getOptions ( )

Returns all set options

Returns
array

Definition at line 84 of file Ip.php.

85  {
86  return $this->_options;
87  }

◆ isValid()

isValid (   $value)

Defined by Zend_Validate_Interface

Returns true if and only if $value is a valid IP address

Parameters
mixed$value
Returns
boolean

Implements Zend_Validate_Interface.

Definition at line 122 of file Ip.php.

123  {
124  if (!is_string($value)) {
125  $this->_error(self::INVALID);
126  return false;
127  }
128 
129  $this->_setValue($value);
130  if (($this->_options['allowipv4'] && !$this->_options['allowipv6'] && !$this->_validateIPv4($value)) ||
131  (!$this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv6($value)) ||
132  ($this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv4($value) && !$this->_validateIPv6($value))) {
133  $this->_error(self::NOT_IP_ADDRESS);
134  return false;
135  }
136 
137  return true;
138  }
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16
_validateIPv6($value)
Definition: Ip.php:162
_validateIPv4($value)
Definition: Ip.php:146

◆ setOptions()

setOptions (   $options)

Sets the options for this validator

Parameters
array$options
Exceptions
Zend_Validate_Exception
Returns
Zend_Validate_Ip

Definition at line 96 of file Ip.php.

97  {
98  if (array_key_exists('allowipv6', $options)) {
99  $this->_options['allowipv6'] = (boolean) $options['allowipv6'];
100  }
101 
102  if (array_key_exists('allowipv4', $options)) {
103  $this->_options['allowipv4'] = (boolean) $options['allowipv4'];
104  }
105 
106  if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) {
107  #require_once 'Zend/Validate/Exception.php';
108  throw new Zend_Validate_Exception('Nothing to validate. Check your options');
109  }
110 
111  return $this;
112  }

Field Documentation

◆ $_messageTemplates

$_messageTemplates
protected
Initial value:
= array(
self::INVALID => "Invalid type given. String expected",
self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address",
)

Definition at line 41 of file Ip.php.

◆ $_options

$_options
protected
Initial value:
= array(
'allowipv6' => true,
'allowipv4' => true
)

Definition at line 51 of file Ip.php.

◆ INVALID

const INVALID = 'ipInvalid'

Definition at line 35 of file Ip.php.

◆ NOT_IP_ADDRESS

const NOT_IP_ADDRESS = 'notIpAddress'

Definition at line 36 of file Ip.php.


The documentation for this class was generated from the following file: