1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lii\Model; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Validate an IP-address in different ways. |
10
|
|
|
* |
11
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
12
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateField) |
13
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
14
|
|
|
*/ |
15
|
|
|
class IPValidator |
16
|
|
|
{ |
17
|
|
|
// Global variables |
18
|
|
|
public $accessKey = ''; |
19
|
|
|
|
20
|
21 |
|
public function __construct($apiKey) |
21
|
|
|
{ |
22
|
21 |
|
$this->accessKey = $apiKey; |
23
|
21 |
|
} |
24
|
|
|
|
25
|
|
|
// /** |
26
|
|
|
// * Load API access key from private file. |
27
|
|
|
// */ |
28
|
|
|
// private function fetchApiKey() |
29
|
|
|
// { |
30
|
|
|
// if (file_exists(ANAX_INSTALL_PATH."/PRIVATE_TOKEN")) { |
31
|
|
|
// $myfile = fopen(ANAX_INSTALL_PATH."/PRIVATE_TOKEN", "r"); |
32
|
|
|
// $this->accessKey = fread($myfile, filesize(ANAX_INSTALL_PATH."/PRIVATE_TOKEN")); |
33
|
|
|
// fclose($myfile); |
34
|
|
|
// } else { |
35
|
|
|
// $this->accessKey = getenv('API_KEY'); |
36
|
|
|
// } |
37
|
|
|
// } |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validate an IPv4 IP-address. |
42
|
|
|
* |
43
|
|
|
* @param string $inputIP The IP-address to validate. |
44
|
|
|
* |
45
|
|
|
* @return boolean based on if IP-address is valid or not. |
46
|
|
|
*/ |
47
|
16 |
|
public function validateIPv4($inputIP) |
48
|
|
|
{ |
49
|
16 |
|
$valid = false; |
50
|
|
|
|
51
|
16 |
|
if (filter_var($inputIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
52
|
11 |
|
$valid = true; |
53
|
|
|
} |
54
|
|
|
|
55
|
16 |
|
return $valid; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Validate an IPv6 IP-address. |
60
|
|
|
* |
61
|
|
|
* @param string $inputIP The IP-address to validate. |
62
|
|
|
* |
63
|
|
|
* @return boolean based on if IP-address is valid or not. |
64
|
|
|
*/ |
65
|
|
|
|
66
|
13 |
|
public function validateIPv6($inputIP) |
67
|
|
|
{ |
68
|
13 |
|
$valid = false; |
69
|
|
|
|
70
|
13 |
|
if (filter_var($inputIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
71
|
3 |
|
$valid = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
13 |
|
return $valid; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Validate an IP-address and return location. |
79
|
|
|
* |
80
|
|
|
* @param string $inputIP The IP-address to validate. |
81
|
|
|
* |
82
|
|
|
* @return string with information about the IP-address. |
83
|
|
|
*/ |
84
|
|
|
|
85
|
|
|
public function locateIP($inputIP) |
86
|
|
|
{ |
87
|
|
|
$result = "Not valid IP-address."; |
88
|
|
|
|
89
|
|
|
if ($this->validateIPv4($inputIP) || $this->validateIPv6($inputIP)) { |
90
|
|
|
// $this->fetchApiKey(); |
91
|
|
|
|
92
|
|
|
// Initialize CURL: |
93
|
|
|
$cRes = curl_init('http://api.ipstack.com/'.$inputIP.'?access_key='.$this->accessKey.''); |
94
|
|
|
curl_setopt($cRes, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// Store the data: |
97
|
|
|
$json = curl_exec($cRes); |
|
|
|
|
98
|
|
|
curl_close($cRes); |
|
|
|
|
99
|
|
|
|
100
|
|
|
// Decode JSON response: |
101
|
|
|
$result = json_decode($json, true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check if an IP-address has a registered domain name in DNS. |
109
|
|
|
* |
110
|
|
|
* @param string $inputIP The IP-address to check for a domain name. |
111
|
|
|
* |
112
|
|
|
* @return boolean based on if IP-address has an domain name or not. |
113
|
|
|
*/ |
114
|
10 |
|
public function checkDomain($inputIP) |
115
|
|
|
{ |
116
|
10 |
|
$result = false; |
117
|
10 |
|
$domain = null; |
118
|
|
|
|
119
|
10 |
|
$validIP = $this->validateIPv4($inputIP); |
120
|
10 |
|
if ($validIP) { |
121
|
8 |
|
$domain = gethostbyaddr($inputIP); |
122
|
|
|
} |
123
|
10 |
|
if ($domain != $inputIP) { |
124
|
3 |
|
$result = $domain; |
125
|
|
|
} |
126
|
|
|
|
127
|
10 |
|
return $result; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Validate an IP-address and return a JSON object with information about the address |
132
|
|
|
* |
133
|
|
|
* @return json object |
|
|
|
|
134
|
|
|
*/ |
135
|
4 |
|
public function validateIPJSON($inputIP) |
136
|
|
|
{ |
137
|
4 |
|
$validationResultIPv4 = $this->validateIPv4($inputIP); |
138
|
4 |
|
$validationResultIPv6 = $this->validateIPv6($inputIP); |
139
|
4 |
|
$domain = $this->checkDomain($inputIP); |
140
|
|
|
|
141
|
4 |
|
if ($validationResultIPv4) { |
142
|
3 |
|
$ipv4msg = "Valid IPv4 address."; |
143
|
|
|
} else { |
144
|
1 |
|
$ipv4msg = "Not valid IPv4 address."; |
145
|
|
|
} |
146
|
|
|
|
147
|
4 |
|
if ($validationResultIPv6) { |
148
|
1 |
|
$ipv6msg = "Valid IPv6 address."; |
149
|
|
|
} else { |
150
|
3 |
|
$ipv6msg = "Not valid IPv6 address."; |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
if (!$domain) { |
154
|
4 |
|
$domain = "No domain name found."; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$json = [ |
158
|
4 |
|
"ip" => "{$inputIP}", |
159
|
4 |
|
"ipv4" => "{$ipv4msg}", |
160
|
4 |
|
"ipv6" => "{$ipv6msg}", |
161
|
4 |
|
"domain" => "{$domain}", |
162
|
|
|
]; |
163
|
4 |
|
return [$json]; |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Validate an IP-address and return a JSON object with information about the address |
168
|
|
|
* |
169
|
|
|
* @return json object |
170
|
|
|
*/ |
171
|
6 |
|
public function locateIPJSON($inputIP) |
172
|
|
|
{ |
173
|
|
|
$result = [ |
174
|
6 |
|
"error" => "Not valid IP-address.", |
175
|
|
|
]; |
176
|
|
|
|
177
|
6 |
|
if ($this->validateIPv4($inputIP) || $this->validateIPv6($inputIP)) { |
178
|
|
|
// $this->fetchApiKey(); |
179
|
4 |
|
$domain = $this->checkDomain($inputIP); |
180
|
4 |
|
$location = $this->locateIP($inputIP); |
181
|
|
|
// var_dump($location); |
182
|
4 |
|
$maplink = "https://www.google.com/maps/search/?api=1&query=".$location['latitude'].",".$location['longitude']; |
183
|
|
|
|
184
|
4 |
|
if ($this->validateIPv4($inputIP)) { |
185
|
3 |
|
$ipv4msg = "Valid IPv4 address."; |
186
|
|
|
} else { |
187
|
1 |
|
$ipv4msg = "Not valid IPv4 address."; |
188
|
|
|
} |
189
|
|
|
|
190
|
4 |
|
if ($this->validateIPv6($inputIP)) { |
191
|
1 |
|
$ipv6msg = "Valid IPv6 address."; |
192
|
|
|
} else { |
193
|
3 |
|
$ipv6msg = "Not valid IPv6 address."; |
194
|
|
|
} |
195
|
|
|
|
196
|
4 |
|
if (!$domain) { |
197
|
4 |
|
$domain = "No domain name found."; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$result = [ |
201
|
4 |
|
"ip" => "{$inputIP}", |
202
|
4 |
|
"ipv4" => "{$ipv4msg}", |
203
|
4 |
|
"ipv6" => "{$ipv6msg}", |
204
|
4 |
|
"domain" => "{$domain}", |
205
|
4 |
|
"country" => "{$location['country_name']}", |
206
|
4 |
|
"city" => "{$location['city']}", |
207
|
4 |
|
"latitude" => "{$location['latitude']}", |
208
|
4 |
|
"longitude" => "{$location['longitude']}", |
209
|
4 |
|
"maplink" => "{$maplink}", |
210
|
|
|
]; |
211
|
|
|
} |
212
|
|
|
|
213
|
6 |
|
return [$result]; |
|
|
|
|
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|