Zend_Validate_Ip

The Gravity Forms Advanced File Uploader Zend Validate Ip class.

Defined (1)

The class is defined in the following location(s).

/inc/VideoUploader/includes/Zend/Validate/Ip.php  
  1. class Zend_Validate_Ip extends Zend_Validate_Abstract 
  2. const INVALID = 'ipInvalid'; 
  3. const NOT_IP_ADDRESS = 'notIpAddress'; 
  4.  
  5. /** 
  6. * @var array 
  7. */ 
  8. protected $_messageTemplates = array( 
  9. self::INVALID => "Invalid type given. String expected",  
  10. self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address",  
  11. ); 
  12.  
  13. /** 
  14. * internal options 
  15. * @var array 
  16. */ 
  17. protected $_options = array( 
  18. 'allowipv6' => true,  
  19. 'allowipv4' => true 
  20. ); 
  21.  
  22. /** 
  23. * Sets validator options 
  24. * @param array $options OPTIONAL Options to set, see the manual for all available options 
  25. * @return void 
  26. */ 
  27. public function __construct($options = array()) 
  28. if ($options instanceof Zend_Config) { 
  29. $options = $options->toArray(); 
  30. } else if (!is_array($options)) { 
  31. $options = func_get_args(); 
  32. $temp['allowipv6'] = array_shift($options); 
  33. if (!empty($options)) { 
  34. $temp['allowipv4'] = array_shift($options); 
  35.  
  36. $options = $temp; 
  37.  
  38. $options += $this->_options; 
  39. $this->setOptions($options); 
  40.  
  41. /** 
  42. * Returns all set options 
  43. * @return array 
  44. */ 
  45. public function getOptions() 
  46. return $this->_options; 
  47.  
  48. /** 
  49. * Sets the options for this validator 
  50. * @param array $options 
  51. * @return Zend_Validate_Ip 
  52. */ 
  53. public function setOptions($options) 
  54. if (array_key_exists('allowipv6', $options)) { 
  55. $this->_options['allowipv6'] = (boolean) $options['allowipv6']; 
  56.  
  57. if (array_key_exists('allowipv4', $options)) { 
  58. $this->_options['allowipv4'] = (boolean) $options['allowipv4']; 
  59.  
  60. if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) { 
  61. require_once 'Zend/Validate/Exception.php'; 
  62. throw new Zend_Validate_Exception('Nothing to validate. Check your options'); 
  63.  
  64. return $this; 
  65.  
  66. /** 
  67. * Defined by Zend_Validate_Interface 
  68. * Returns true if and only if $value is a valid IP address 
  69. * @param mixed $value 
  70. * @return boolean 
  71. */ 
  72. public function isValid($value) 
  73. if (!is_string($value)) { 
  74. $this->_error(self::INVALID); 
  75. return false; 
  76.  
  77. $this->_setValue($value); 
  78. if (($this->_options['allowipv4'] && !$this->_options['allowipv6'] && !$this->_validateIPv4($value)) || 
  79. (!$this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv6($value)) || 
  80. ($this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv4($value) && !$this->_validateIPv6($value))) { 
  81. $this->_error(self::NOT_IP_ADDRESS); 
  82. return false; 
  83.  
  84. return true; 
  85.  
  86. /** 
  87. * Validates an IPv4 address 
  88. * @param string $value 
  89. */ 
  90. protected function _validateIPv4($value) { 
  91. $ip2long = ip2long($value); 
  92. if($ip2long === false) { 
  93. return false; 
  94.  
  95. return $value == long2ip($ip2long); 
  96.  
  97. /** 
  98. * Validates an IPv6 address 
  99. * @param string $value Value to check against 
  100. * @return boolean True when $value is a valid ipv6 address 
  101. * False otherwise 
  102. */ 
  103. protected function _validateIPv6($value) { 
  104. if (strlen($value) < 3) { 
  105. return $value == '::'; 
  106.  
  107. if (strpos($value, '.')) { 
  108. $lastcolon = strrpos($value, ':'); 
  109. if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) { 
  110. return false; 
  111.  
  112. $value = substr($value, 0, $lastcolon) . ':0:0'; 
  113.  
  114. if (strpos($value, '::') === false) { 
  115. return preg_match('/\A(?:[a-f0-9]{1, 4}:) {7}[a-f0-9]{1, 4}\z/i', $value); 
  116.  
  117. $colonCount = substr_count($value, ':'); 
  118. if ($colonCount < 8) { 
  119. return preg_match('/\A(?::|(?:[a-f0-9]{1, 4}:)+):(?:(?:[a-f0-9]{1, 4}:)*[a-f0-9]{1, 4})?\z/i', $value); 
  120.  
  121. // special case with ending or starting double colon 
  122. if ($colonCount == 8) { 
  123. return preg_match('/\A(?:::)?(?:[a-f0-9]{1, 4}:) {6}[a-f0-9]{1, 4}(?:::)?\z/i', $value); 
  124.  
  125. return false;