/library/Zend/Validator/Ip.php

https://github.com/leerbag/zf2 · PHP · 207 lines · 105 code · 26 blank · 76 comment · 35 complexity · 288d43e9160b820e8566fb8582e16cf4 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Validator;
  24. /**
  25. * @uses \Zend\Validator\AbstractValidator
  26. * @uses \Zend\Validator\Exception
  27. * @category Zend
  28. * @package Zend_Validate
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Ip extends AbstractValidator
  33. {
  34. const INVALID = 'ipInvalid';
  35. const NOT_IP_ADDRESS = 'notIpAddress';
  36. /**
  37. * @var array
  38. */
  39. protected $_messageTemplates = array(
  40. self::INVALID => "Invalid type given. String expected",
  41. self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address",
  42. );
  43. /**
  44. * internal options
  45. *
  46. * @var array
  47. */
  48. protected $_options = array(
  49. 'allowipv6' => true,
  50. 'allowipv4' => true
  51. );
  52. /**
  53. * Sets validator options
  54. *
  55. * @param array $options OPTIONAL Options to set, see the manual for all available options
  56. * @return void
  57. */
  58. public function __construct($options = array())
  59. {
  60. if ($options instanceof \Zend\Config\Config) {
  61. $options = $options->toArray();
  62. } else if (!is_array($options)) {
  63. $options = func_get_args();
  64. $temp['allowipv6'] = array_shift($options);
  65. if (!empty($options)) {
  66. $temp['allowipv4'] = array_shift($options);
  67. }
  68. $options = $temp;
  69. }
  70. $options += $this->_options;
  71. $this->setOptions($options);
  72. }
  73. /**
  74. * Returns all set options
  75. *
  76. * @return array
  77. */
  78. public function getOptions()
  79. {
  80. return $this->_options;
  81. }
  82. /**
  83. * Sets the options for this validator
  84. *
  85. * @param array|Traversable $options
  86. * @return \Zend\Validator\Ip
  87. */
  88. public function setOptions($options = array())
  89. {
  90. if (!is_array($options) && !$options instanceof Traversable) {
  91. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable');
  92. }
  93. if (array_key_exists('allowipv6', $options)) {
  94. $this->_options['allowipv6'] = (boolean) $options['allowipv6'];
  95. }
  96. if (array_key_exists('allowipv4', $options)) {
  97. $this->_options['allowipv4'] = (boolean) $options['allowipv4'];
  98. }
  99. if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) {
  100. throw new Exception\InvalidArgumentException('Nothing to validate. Check your options');
  101. }
  102. return parent::setOptions($options);
  103. }
  104. /**
  105. * Returns true if and only if $value is a valid IP address
  106. *
  107. * @param mixed $value
  108. * @return boolean
  109. */
  110. public function isValid($value)
  111. {
  112. if (!is_string($value)) {
  113. $this->error(self::INVALID);
  114. return false;
  115. }
  116. $this->setValue($value);
  117. if (($this->_options['allowipv4'] && !$this->_options['allowipv6'] && !$this->_validateIPv4($value)) ||
  118. (!$this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv6($value)) ||
  119. ($this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv4($value) && !$this->_validateIPv6($value))) {
  120. $this->error(self::NOT_IP_ADDRESS);
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * Validates an IPv4 address
  127. *
  128. * @param string $value
  129. */
  130. protected function _validateIPv4($value) {
  131. if (preg_match('/^([01]{8}.){3}[01]{8}$/i', $value)) {
  132. // binary format 00000000.00000000.00000000.00000000
  133. $value = bindec(substr($value, 0, 8)) . "." . bindec(substr($value, 9, 8)) . "."
  134. . bindec(substr($value, 18, 8)) . "." . bindec(substr($value, 27, 8));
  135. } else if (preg_match('/^([0-9]{3}.){3}[0-9]{3}$/i', $value)) {
  136. // octet format 777.777.777.777
  137. $value = (int) substr($value, 0, 3) . "." . (int) substr($value, 4, 3) . "."
  138. . (int) substr($value, 8, 3) . "." . (int) substr($value, 12, 3);
  139. } else if (preg_match('/^([0-9a-f]{2}.){3}[0-9a-f]{2}$/i', $value)) {
  140. // hex format ff.ff.ff.ff
  141. $value = hexdec(substr($value, 0, 2)) . "." . hexdec(substr($value, 3, 2)) . "."
  142. . hexdec(substr($value, 6, 2)) . "." . hexdec(substr($value, 9, 2));
  143. }
  144. $ip2long = ip2long($value);
  145. if($ip2long === false) {
  146. return false;
  147. }
  148. return $value == long2ip($ip2long);
  149. }
  150. /**
  151. * Validates an IPv6 address
  152. *
  153. * @param string $value Value to check against
  154. * @return boolean True when $value is a valid ipv6 address
  155. * False otherwise
  156. */
  157. protected function _validateIPv6($value) {
  158. if (strlen($value) < 3) {
  159. return $value == '::';
  160. }
  161. if (strpos($value, '.')) {
  162. $lastcolon = strrpos($value, ':');
  163. if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) {
  164. return false;
  165. }
  166. $value = substr($value, 0, $lastcolon) . ':0:0';
  167. }
  168. if (strpos($value, '::') === false) {
  169. return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $value);
  170. }
  171. $colonCount = substr_count($value, ':');
  172. if ($colonCount < 8) {
  173. return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $value);
  174. }
  175. // special case with ending or starting double colon
  176. if ($colonCount == 8) {
  177. return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $value);
  178. }
  179. return false;
  180. }
  181. }