1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288:
<?php
namespace MvcCore\Ext\Forms\Validators;
class Ip extends \MvcCore\Ext\Forms\Validator {
const ERROR_IP = 0;
protected static $errorMessages = [
self::ERROR_IP => "Field '{0}' requires a valid IP address. Allowed types: {1}.",
];
protected $allowIPv4OctetFormat = TRUE;
protected $allowIPv4HexFormat = FALSE;
protected $allowIPv4BinaryFormat = FALSE;
protected $allowIPv6 = TRUE;
protected $allowIPv6Literals = FALSE;
public function GetAllowIPv4OctetFormat () {
return $this->allowIPv4OctetFormat;
}
public function SetAllowIPv4OctetFormat ($allowIPv4OctetFormat = TRUE) {
$this->allowIPv4OctetFormat = $allowIPv4OctetFormat;
return $this;
}
public function GetAllowIPv4HexFormat () {
return $this->allowIPv4HexFormat;
}
public function SetAllowIPv4HexFormat ($allowIPv4HexFormat = TRUE) {
$this->allowIPv4HexFormat = $allowIPv4HexFormat;
return $this;
}
public function GetAllowIPv4BinaryFormat () {
return $this->allowIPv4BinaryFormat;
}
public function SetAllowIPv4BinaryFormat ($allowIPv4BinaryFormat = TRUE) {
$this->allowIPv4BinaryFormat = $allowIPv4BinaryFormat;
return $this;
}
public function GetAllowIPv6 () {
return $this->allowIPv6;
}
public function SetAllowIPv6 ($allowIPv6 = TRUE) {
$this->allowIPv6 = $allowIPv6;
return $this;
}
public function GetAllowIPv6Literals () {
return $this->allowIPv6Literals;
}
public function SetAllowIPv6Literals ($allowIPv6Literals = TRUE) {
$this->allowIPv6Literals = $allowIPv6Literals;
return $this;
}
public function Validate ($rawSubmittedValue) {
$result = NULL;
$rawSubmittedValue = (string) $rawSubmittedValue;
$ipv4Allowed = $this->allowIPv4HexFormat || $this->allowIPv4OctetFormat || $this->allowIPv4BinaryFormat;
if ($ipv4Allowed) {
$result = $this->validateIPv4($rawSubmittedValue);
if ($result !== NULL) return $result;
}
if ($this->allowIPv6) {
if ($this->allowIPv6Literals) {
if (preg_match('/^\[(.*)\]$/', $rawSubmittedValue, $matches))
$rawSubmittedValue = $matches[1];
}
$result = $this->validateIPv6($rawSubmittedValue);
if ($result !== NULL) return $result;
}
if ($result === NULL) {
$errorMsgAllowedTypes = [];
if ($ipv4Allowed) {
$ipv4AllowedFormats = [];
if ($this->allowIPv4OctetFormat) $ipv4AllowedFormats[] = 'octet';
if ($this->allowIPv4HexFormat) $ipv4AllowedFormats[] = 'hexadecimal';
if ($this->allowIPv4BinaryFormat) $ipv4AllowedFormats[] = 'binary';
$errorMsgAllowedTypes[] = 'IPv4 (formats: ' . implode(', ', $ipv4AllowedFormats) . ')';
if ($this->allowIPv6)
$errorMsgAllowedTypes[] = 'IPv6' . ($this->allowIPv6Literals ? ' (with literals)' : '');
}
$this->field->AddValidationError(
static::GetErrorMessage(static::ERROR_IP),
[implode(', ', $errorMsgAllowedTypes)]
);
}
return $result;
}
protected function validateIPv4 ($rawSubmittedValue) {
if ($this->allowIPv4BinaryFormat && preg_match('/^([01]{8}\.){3}[01]{8}\z/i', $rawSubmittedValue)) {
$rawSubmittedValue = bindec(substr($rawSubmittedValue, 0, 8))
. '.' . bindec(substr($rawSubmittedValue, 9, 8))
. '.' . bindec(substr($rawSubmittedValue, 18, 8))
. '.' . bindec(substr($rawSubmittedValue, 27, 8));
} elseif ($this->allowIPv4OctetFormat && preg_match('/^([0-9]{3}\.){3}[0-9]{3}\z/i', $rawSubmittedValue)) {
$rawSubmittedValue = (int) substr($rawSubmittedValue, 0, 3)
. '.' . (int) substr($rawSubmittedValue, 4, 3)
. '.' . (int) substr($rawSubmittedValue, 8, 3)
. '.' . (int) substr($rawSubmittedValue, 12, 3);
} elseif ($this->allowIPv4HexFormat && preg_match('/^([0-9a-f]{2}\.){3}[0-9a-f]{2}\z/i', $rawSubmittedValue)) {
$rawSubmittedValue = hexdec(substr($rawSubmittedValue, 0, 2))
. '.' . hexdec(substr($rawSubmittedValue, 3, 2))
. '.' . hexdec(substr($rawSubmittedValue, 6, 2))
. '.' . hexdec(substr($rawSubmittedValue, 9, 2));
}
$ip2long = ip2long($rawSubmittedValue);
if ($ip2long === FALSE)
return NULL;
if ($rawSubmittedValue == long2ip($ip2long)) {
return $rawSubmittedValue;
}
return NULL;
}
protected function validateIPv6 ($rawSubmittedValue) {
if (strlen($rawSubmittedValue) < 3) {
if ($rawSubmittedValue == '::') {
return '::';
} else {
return NULL;
}
}
if (strpos($rawSubmittedValue, '.')) {
$lastColon = strrpos($rawSubmittedValue, ':');
$validatedIPv4 = $this->validateIPv4(substr($rawSubmittedValue, $lastColon + 1));
if (!($lastColon && $validatedIPv4))
return NULL;
$rawSubmittedValue = substr($rawSubmittedValue, 0, $lastColon) . ':0:0';
}
if (strpos($rawSubmittedValue, '::') === FALSE) {
if (preg_match('/\A(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}\z/i', $rawSubmittedValue))
return $rawSubmittedValue;
}
$colonCount = substr_count($rawSubmittedValue, ':');
if ($colonCount < 8) {
if (preg_match('/\A(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?\z/i', $rawSubmittedValue))
return $rawSubmittedValue;
}
if ($colonCount == 8) {
if (preg_match('/\A(?:::)?(?:[a-f0-9]{1,4}:){6}[a-f0-9]{1,4}(?:::)?\z/i', $rawSubmittedValue))
return $rawSubmittedValue;
}
return NULL;
}
}