1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//adopted from poweradmin (https://github.com/poweradmin) |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class IpCheck |
7
|
|
|
*/ |
8
|
|
|
class IpCheck |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
public $ipin; |
11
|
|
|
public $ipout; |
12
|
|
|
public $ipver; |
13
|
|
|
|
14
|
|
|
// Return IP type. 4 for IPv4, 6 for IPv6, 0 for bad IP. |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param $ipValue |
18
|
|
|
*/ |
19
|
|
|
public function addressType($ipValue) |
20
|
|
|
{ |
21
|
|
|
$this->ipin = $ipValue; |
22
|
|
|
$this->ipver = 0; |
23
|
|
|
|
24
|
|
|
// IPv4 addresses are easy-peasy |
25
|
|
|
if (filter_var($this->ipin, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
26
|
|
|
$this->ipver = 4; |
27
|
|
|
$this->ipout = $this->ipin; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// IPv6 is at least a little more complex. |
31
|
|
|
if (filter_var($this->ipin, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
32
|
|
|
|
33
|
|
|
// Look for embedded IPv4 in an embedded IPv6 address, where FFFF is appended. |
34
|
|
|
if (strpos($this->ipin, '::FFFF:') === 0) { |
35
|
|
|
$ipv4addr = substr($this->ipin, 7); |
36
|
|
View Code Duplication |
if (filter_var($ipv4addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
|
|
|
37
|
|
|
$this->ipver = 4; |
38
|
|
|
$this->ipout = $ipv4addr; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Look for an IPv4 address embedded as ::x.x.x.x |
42
|
|
|
} elseif (strpos($this->ipin, '::') === 0) { |
43
|
|
|
$ipv4addr = substr($this->ipin, 2); |
44
|
|
View Code Duplication |
if (filter_var($ipv4addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
|
|
|
45
|
|
|
$this->ipver = 4; |
46
|
|
|
$this->ipout = $ipv4addr; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Otherwise, assume this an IPv6 address. |
50
|
|
|
} else { |
51
|
|
|
$this->ipver = 6; |
52
|
|
|
$this->ipout = $this->ipin; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** Check whether the given address is an IP address |
58
|
|
|
* |
59
|
|
|
* @param string $ip Given IP address |
60
|
|
|
* |
61
|
|
|
* @return string A if IPv4, AAAA if IPv6 or 0 if invalid |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function isValidIpAddress($ip) |
64
|
|
|
{ |
65
|
|
|
$value = 0; |
66
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
67
|
|
|
$value = 'A'; |
68
|
|
|
} elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
69
|
|
|
$value = 'AAAA'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.