|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_Ip extends Zend_Validate_Abstract 00034 { 00035 const INVALID = 'ipInvalid'; 00036 const NOT_IP_ADDRESS = 'notIpAddress'; 00037 00041 protected $_messageTemplates = array( 00042 self::INVALID => "Invalid type given, value should be a string", 00043 self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address", 00044 ); 00045 00051 protected $_options = array( 00052 'allowipv6' => true, 00053 'allowipv4' => true 00054 ); 00055 00062 public function __construct($options = array()) 00063 { 00064 if ($options instanceof Zend_Config) { 00065 $options = $options->toArray(); 00066 } else if (!is_array($options)) { 00067 $options = func_get_args(); 00068 $temp['allowipv6'] = array_shift($options); 00069 if (!empty($options)) { 00070 $temp['allowipv4'] = array_shift($options); 00071 } 00072 00073 $options = $temp; 00074 } 00075 00076 $options += $this->_options; 00077 $this->setOptions($options); 00078 } 00079 00085 public function getOptions() 00086 { 00087 return $this->_options; 00088 } 00089 00096 public function setOptions($options) 00097 { 00098 if (array_key_exists('allowipv6', $options)) { 00099 $this->_options['allowipv6'] = (boolean) $options['allowipv6']; 00100 } 00101 00102 if (array_key_exists('allowipv4', $options)) { 00103 $this->_options['allowipv4'] = (boolean) $options['allowipv4']; 00104 } 00105 00106 if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) { 00107 require_once 'Zend/Validate/Exception.php'; 00108 throw new Zend_Validate_Exception('Nothing to validate. Check your options'); 00109 } 00110 00111 return $this; 00112 } 00113 00122 public function isValid($value) 00123 { 00124 if (!is_string($value)) { 00125 $this->_error(self::INVALID); 00126 return false; 00127 } 00128 00129 $this->_setValue($value); 00130 if (($this->_options['allowipv4'] && !$this->_options['allowipv6'] && !$this->_validateIPv4($value)) || 00131 (!$this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv6($value)) || 00132 ($this->_options['allowipv4'] && $this->_options['allowipv6'] && !$this->_validateIPv4($value) && !$this->_validateIPv6($value))) { 00133 $this->_error(self::NOT_IP_ADDRESS); 00134 return false; 00135 } 00136 00137 return true; 00138 } 00139 00145 protected function _validateIPv4($value) { 00146 $ip2long = ip2long($value); 00147 if($ip2long === false) { 00148 return false; 00149 } 00150 00151 return $value == long2ip($ip2long); 00152 } 00153 00161 protected function _validateIPv6($value) { 00162 if (strlen($value) < 3) { 00163 return $value == '::'; 00164 } 00165 00166 if (strpos($value, '.')) { 00167 $lastcolon = strrpos($value, ':'); 00168 if (!($lastcolon && $this->_validateIPv4(substr($value, $lastcolon + 1)))) { 00169 return false; 00170 } 00171 00172 $value = substr($value, 0, $lastcolon) . ':0:0'; 00173 } 00174 00175 if (strpos($value, '::') === false) { 00176 return preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $value); 00177 } 00178 00179 $colonCount = substr_count($value, ':'); 00180 if ($colonCount < 8) { 00181 return preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $value); 00182 } 00183 00184 // special case with ending or starting double colon 00185 if ($colonCount == 8) { 00186 return preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $value); 00187 } 00188 00189 return false; 00190 } 00191 }