/vendor/zend/Zend/Validator/Ip.php

https://github.com/sdh100shaun/Linktuesday.com · PHP · 189 lines · 92 code · 24 blank · 73 comment · 28 complexity · 1b687eefb7affe5ae28c6a5556ff0f23 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 $options
  86. * @return \Zend\Validator\Ip
  87. */
  88. public function setOptions($options)
  89. {
  90. if (array_key_exists('allowipv6', $options)) {
  91. $this->_options['allowipv6'] = (boolean) $options['allowipv6'];
  92. }
  93. if (array_key_exists('allowipv4', $options)) {
  94. $this->_options['allowipv4'] = (boolean) $options['allowipv4'];
  95. }
  96. if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) {
  97. throw new Exception\InvalidArgumentException('Nothing to validate. Check your options');
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Returns true if and only if $value is a valid IP address
  103. *
  104. * @param mixed $value
  105. * @return boolean
  106. */
  107. public function isValid($value)
  108. {
  109. if (!is_string($value)) {
  110. $this->_error(self::INVALID);
  111. return false;
  112. }
  113. $this->_setValue($value);
  114. if (($this->_options['allowipv4'] && !$this->_options['allowipv6'] && !$this->_validateIPv4($value)) ||
  115. (!$this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv6($value)) ||
  116. ($this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv4($value) && !$this->_validateIPv6($value))) {
  117. $this->_error(self::NOT_IP_ADDRESS);
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Validates an IPv4 address
  124. *
  125. * @param string $value
  126. */
  127. protected function _validateIPv4($value) {
  128. $ip2long = ip2long($value);
  129. if($ip2long === false) {
  130. return false;
  131. }
  132. return $value == long2ip($ip2long);
  133. }
  134. /**
  135. * Validates an IPv6 address
  136. *
  137. * @param string $value Value to check against
  138. * @return boolean True when $value is a valid ipv6 address
  139. * False otherwise
  140. */
  141. protected function _validateIPv6($value) {
  142. if (strlen($value) < 3) {
  143. return $value == '::';
  144. }
  145. if (strpos($value, '.')) {
  146. $lastcolon = strrpos($value, ':');
  147. if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) {
  148. return false;
  149. }
  150. $value = substr($value, 0, $lastcolon) . ':0:0';
  151. }
  152. if (strpos($value, '::') === false) {
  153. return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $value);
  154. }
  155. $colonCount = substr_count($value, ':');
  156. if ($colonCount < 8) {
  157. return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $value);
  158. }
  159. // special case with ending or starting double colon
  160. if ($colonCount == 8) {
  161. return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $value);
  162. }
  163. return false;
  164. }
  165. }