PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Validate/Ip.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 192 lines | 91 code | 24 blank | 77 comment | 28 complexity | 7abc354eed666e6b93c14c9187d2d0a3 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Ip extends Zend_Validate_Abstract
  32. {
  33. const INVALID = 'ipInvalid';
  34. const NOT_IP_ADDRESS = 'notIpAddress';
  35. /**
  36. * @var array
  37. */
  38. protected $_messageTemplates = array(
  39. self::INVALID => "Invalid type given. String expected",
  40. self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address",
  41. );
  42. /**
  43. * internal options
  44. *
  45. * @var array
  46. */
  47. protected $_options = array(
  48. 'allowipv6' => true,
  49. 'allowipv4' => true
  50. );
  51. /**
  52. * Sets validator options
  53. *
  54. * @param array $options OPTIONAL Options to set, see the manual for all available options
  55. */
  56. public function __construct($options = array())
  57. {
  58. if ($options instanceof Zend_Config) {
  59. $options = $options->toArray();
  60. } else if (!is_array($options)) {
  61. $options = func_get_args();
  62. $temp['allowipv6'] = array_shift($options);
  63. if (!empty($options)) {
  64. $temp['allowipv4'] = array_shift($options);
  65. }
  66. $options = $temp;
  67. }
  68. $options += $this->_options;
  69. $this->setOptions($options);
  70. }
  71. /**
  72. * Returns all set options
  73. *
  74. * @return array
  75. */
  76. public function getOptions()
  77. {
  78. return $this->_options;
  79. }
  80. /**
  81. * Sets the options for this validator
  82. *
  83. * @param array $options
  84. * @throws Zend_Validate_Exception
  85. * @return Zend_Validate_Ip
  86. */
  87. public function setOptions($options)
  88. {
  89. if (array_key_exists('allowipv6', $options)) {
  90. $this->_options['allowipv6'] = (boolean) $options['allowipv6'];
  91. }
  92. if (array_key_exists('allowipv4', $options)) {
  93. $this->_options['allowipv4'] = (boolean) $options['allowipv4'];
  94. }
  95. if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) {
  96. #require_once 'Zend/Validate/Exception.php';
  97. throw new Zend_Validate_Exception('Nothing to validate. Check your options');
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Defined by Zend_Validate_Interface
  103. *
  104. * Returns true if and only if $value is a valid IP address
  105. *
  106. * @param mixed $value
  107. * @return boolean
  108. */
  109. public function isValid($value)
  110. {
  111. if (!is_string($value)) {
  112. $this->_error(self::INVALID);
  113. return false;
  114. }
  115. $this->_setValue($value);
  116. if (($this->_options['allowipv4'] && !$this->_options['allowipv6'] && !$this->_validateIPv4($value)) ||
  117. (!$this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv6($value)) ||
  118. ($this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv4($value) && !$this->_validateIPv6($value))) {
  119. $this->_error(self::NOT_IP_ADDRESS);
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Validates an IPv4 address
  126. *
  127. * @param string $value
  128. * @return bool
  129. */
  130. protected function _validateIPv4($value) {
  131. $ip2long = ip2long($value);
  132. if($ip2long === false) {
  133. return false;
  134. }
  135. return $value == long2ip($ip2long);
  136. }
  137. /**
  138. * Validates an IPv6 address
  139. *
  140. * @param string $value Value to check against
  141. * @return boolean True when $value is a valid ipv6 address
  142. * False otherwise
  143. */
  144. protected function _validateIPv6($value) {
  145. if (strlen($value) < 3) {
  146. return $value == '::';
  147. }
  148. if (strpos($value, '.')) {
  149. $lastcolon = strrpos($value, ':');
  150. if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) {
  151. return false;
  152. }
  153. $value = substr($value, 0, $lastcolon) . ':0:0';
  154. }
  155. if (strpos($value, '::') === false) {
  156. return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $value);
  157. }
  158. $colonCount = substr_count($value, ':');
  159. if ($colonCount < 8) {
  160. return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $value);
  161. }
  162. // special case with ending or starting double colon
  163. if ($colonCount == 8) {
  164. return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $value);
  165. }
  166. return false;
  167. }
  168. }