Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ip.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const INVALID = 'ipInvalid';
36  const NOT_IP_ADDRESS = 'notIpAddress';
37 
41  protected $_messageTemplates = array(
42  self::INVALID => "Invalid type given. String expected",
43  self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address",
44  );
45 
51  protected $_options = array(
52  'allowipv6' => true,
53  'allowipv4' => true
54  );
55 
61  public function __construct($options = array())
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  }
78 
84  public function getOptions()
85  {
86  return $this->_options;
87  }
88 
96  public function setOptions($options)
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  }
113 
122  public function isValid($value)
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  }
139 
146  protected function _validateIPv4($value) {
147  $ip2long = ip2long($value);
148  if($ip2long === false) {
149  return false;
150  }
151 
152  return $value == long2ip($ip2long);
153  }
154 
162  protected function _validateIPv6($value) {
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  }
192 }
const NOT_IP_ADDRESS
Definition: Ip.php:36
const INVALID
Definition: Ip.php:35
setOptions($options)
Definition: Ip.php:96
_error($messageKey, $value=null)
Definition: Abstract.php:284
__construct($options=array())
Definition: Ip.php:61
$value
Definition: gender.phtml:16
_validateIPv6($value)
Definition: Ip.php:162
_validateIPv4($value)
Definition: Ip.php:146
isValid($value)
Definition: Ip.php:122
getOptions()
Definition: Ip.php:84
$_messageTemplates
Definition: Ip.php:41