PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Validate/Ip.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 191 lines | 93 code | 24 blank | 74 comment | 28 complexity | 8b1772301a364f4a474e76cc4568216c 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. * @version $Id: Ip.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @return void
  56. */
  57. public function __construct($options = array())
  58. {
  59. if ($options instanceof Zend_Config) {
  60. $options = $options->toArray();
  61. } else if (!is_array($options)) {
  62. $options = func_get_args();
  63. $temp['allowipv6'] = array_shift($options);
  64. if (!empty($options)) {
  65. $temp['allowipv4'] = array_shift($options);
  66. }
  67. $options = $temp;
  68. }
  69. $options += $this->_options;
  70. $this->setOptions($options);
  71. }
  72. /**
  73. * Returns all set options
  74. *
  75. * @return array
  76. */
  77. public function getOptions()
  78. {
  79. return $this->_options;
  80. }
  81. /**
  82. * Sets the options for this validator
  83. *
  84. * @param array $options
  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. */
  129. protected function _validateIPv4($value) {
  130. $ip2long = ip2long($value);
  131. if($ip2long === false) {
  132. return false;
  133. }
  134. return $value == long2ip($ip2long);
  135. }
  136. /**
  137. * Validates an IPv6 address
  138. *
  139. * @param string $value Value to check against
  140. * @return boolean True when $value is a valid ipv6 address
  141. * False otherwise
  142. */
  143. protected function _validateIPv6($value) {
  144. if (strlen($value) < 3) {
  145. return $value == '::';
  146. }
  147. if (strpos($value, '.')) {
  148. $lastcolon = strrpos($value, ':');
  149. if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) {
  150. return false;
  151. }
  152. $value = substr($value, 0, $lastcolon) . ':0:0';
  153. }
  154. if (strpos($value, '::') === false) {
  155. return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $value);
  156. }
  157. $colonCount = substr_count($value, ':');
  158. if ($colonCount < 8) {
  159. return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $value);
  160. }
  161. // special case with ending or starting double colon
  162. if ($colonCount == 8) {
  163. return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $value);
  164. }
  165. return false;
  166. }
  167. }