|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
|
4
|
|
|
|
|
5
|
|
|
class IpModel |
|
6
|
|
|
{ |
|
7
|
|
|
public function ipValidate($ipAddress) : array |
|
8
|
|
|
{ |
|
9
|
|
|
$version = null; |
|
10
|
|
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
11
|
|
|
$check = $ipAddress . " is OK"; |
|
12
|
|
|
$version = strpos($ipAddress, ".") === false ? 6 : 4; |
|
13
|
|
|
$hostname = gethostbyaddr($ipAddress); |
|
14
|
|
|
if ($hostname == $ipAddress) { |
|
15
|
|
|
$hostname = "Not found"; |
|
16
|
|
|
} |
|
17
|
|
|
} else { |
|
18
|
|
|
$check = $ipAddress . " is NOT OK ip address"; |
|
19
|
|
|
$hostname = "Not found"; |
|
20
|
|
|
} |
|
21
|
|
|
return [$check, $hostname, $version]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
2 |
|
public function local() : string |
|
25
|
|
|
{ |
|
26
|
2 |
|
return file_get_contents("http://ipecho.net/plain"); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function ipValidateJson($ipAddress) : array |
|
30
|
|
|
{ |
|
31
|
|
|
$version = null; |
|
32
|
|
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
33
|
|
|
$check = $ipAddress . " is OK"; |
|
34
|
|
|
$version = strpos($ipAddress, ".") === false ? "ipv6" : "ipv4"; |
|
35
|
|
|
$hostname = gethostbyaddr($ipAddress); |
|
36
|
|
|
if ($hostname == $ipAddress) { |
|
37
|
|
|
$hostname = "Not found"; |
|
38
|
|
|
} |
|
39
|
|
|
} else { |
|
40
|
|
|
$check = $ipAddress . " is NOT OK ip address"; |
|
41
|
|
|
$hostname = "Not found"; |
|
42
|
|
|
} |
|
43
|
|
|
return ["ip" => $check, "domain" => $hostname, "type" => $version]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function ipStack($ipAddress) |
|
47
|
|
|
{ |
|
48
|
|
|
// $path = ANAX_INSTALL_PATH . "/config/api.txt"; |
|
49
|
|
|
$keys = require(ANAX_INSTALL_PATH . "/config/api.php"); |
|
50
|
|
|
// $accessKey = file_get_contents($path[0]); |
|
51
|
|
|
|
|
52
|
|
|
$accessKey = $keys[0]; |
|
53
|
|
|
// $handle = fopen(ANAX_INSTALL_PATH . "/config/api.txt", "r"); |
|
54
|
|
|
// if ($handle) { |
|
55
|
|
|
// $accessKey = fgets($handle); |
|
56
|
|
|
// // echo $line; |
|
57
|
|
|
// } |
|
58
|
|
|
// fclose($handle); |
|
59
|
|
|
// Initialize CURL: |
|
60
|
|
|
$chr = curl_init('http://api.ipstack.com/'.$ipAddress.'?access_key='.trim($accessKey).''); |
|
61
|
|
|
curl_setopt($chr, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
// Store the data: |
|
64
|
|
|
$json = curl_exec($chr); |
|
|
|
|
|
|
65
|
|
|
curl_close($chr); |
|
|
|
|
|
|
66
|
|
|
$controll = $this->ipValidateJson($ipAddress); |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// Decode JSON response: |
|
70
|
|
|
$apiResult = json_decode($json, true, JSON_UNESCAPED_UNICODE); |
|
71
|
|
|
$apiResult["ip check"] = $controll["ip"]; |
|
72
|
|
|
$apiResult["domain"] = $controll["domain"]; |
|
73
|
|
|
// if (trim($accessKey) === $k) { |
|
74
|
|
|
// return "lika"; |
|
75
|
|
|
// } else { |
|
76
|
|
|
// return var_dump(trim()); |
|
77
|
|
|
// } |
|
78
|
|
|
return $apiResult; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|