1 | <?php |
||
2 | |||
3 | namespace Anax\Model; |
||
4 | |||
5 | class IpValidator |
||
6 | { |
||
7 | private $config; |
||
8 | /** |
||
9 | * Constructor, allow for $di to be injected. |
||
10 | * |
||
11 | * @param str $config api key |
||
12 | */ |
||
13 | 1 | public function __construct($config) |
|
14 | { |
||
15 | 1 | $this->config = $config; |
|
16 | 1 | } |
|
17 | |||
18 | 1 | public function setConfig($config) |
|
19 | { |
||
20 | 1 | $this->config = $config; |
|
21 | 1 | } |
|
22 | |||
23 | /** |
||
24 | * |
||
25 | * Returns Array[valid, ip, ipv, domain], get used at view. |
||
26 | * |
||
27 | */ |
||
28 | 1 | public function getIp($ip) : array |
|
29 | { |
||
30 | 1 | if (filter_var($ip, FILTER_VALIDATE_IP)) { |
|
31 | 1 | $valid = "true"; |
|
32 | } else { |
||
33 | $valid = "false"; |
||
34 | } |
||
35 | |||
36 | 1 | if ($valid == "true") { |
|
37 | 1 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
38 | 1 | $ipv = "Ipv4"; |
|
39 | } else { |
||
40 | $ipv = "Ipv6"; |
||
41 | } |
||
42 | |||
43 | 1 | $key = $this->config->config; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
44 | 1 | $details = json_decode(file_get_contents("http://api.ipstack.com/{$ip}?access_key={$key}")); |
|
45 | |||
46 | 1 | $domain = gethostbyaddr($ip); |
|
47 | 1 | $lat = $details->latitude; |
|
48 | 1 | $long = $details->longitude; |
|
49 | 1 | $country = $details->country_name; |
|
50 | 1 | $region = $details->region_name; |
|
51 | 1 | $city = $details->city; |
|
52 | |||
53 | return [ |
||
54 | 1 | "valid" => $valid, |
|
55 | 1 | "ip" => $ip, |
|
56 | 1 | "ipv" => $ipv, |
|
57 | 1 | "domain" => $domain, |
|
58 | 1 | "lat" => $lat, |
|
59 | 1 | "long" => $long, |
|
60 | 1 | "country" => $country, |
|
61 | 1 | "region" => $region, |
|
62 | 1 | "city" => $city |
|
63 | ]; |
||
64 | |||
65 | } else { |
||
66 | return [ |
||
67 | "valid" => "Not Valid IP", |
||
68 | "ip" => null, |
||
69 | "ipv" => null, |
||
70 | "domain" => null, |
||
71 | "lat" => null, |
||
72 | "long" => null, |
||
73 | "country" => null, |
||
74 | "region" => null, |
||
75 | "city" => null |
||
76 | ]; |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 |