1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* IP Controller |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Hepa19\IPGeo; |
8
|
|
|
|
9
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
10
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controller for IP validator API |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class IPGeoJSONController implements ContainerInjectableInterface |
17
|
|
|
{ |
18
|
|
|
use ContainerInjectableTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Index page |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
3 |
|
public function indexActionGet() : array |
26
|
|
|
{ |
27
|
3 |
|
$request = $this->di->get("request"); |
28
|
3 |
|
$ip = $request->getGet("ip"); |
29
|
|
|
|
30
|
3 |
|
if (empty($ip)) { |
31
|
1 |
|
$ipgetcurrent = $this->di->get("ipgetcurrent"); |
32
|
1 |
|
$ip = $ipgetcurrent->getIP(); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
$validator = $this->di->get("validator"); |
36
|
3 |
|
$valid = $validator->isValid($ip); |
37
|
3 |
|
$protocol = $validator->getProtocol($ip); |
38
|
3 |
|
$host = $validator->getHost($ip); |
39
|
3 |
|
$ipstackRes = $this->getIPData($ip); |
40
|
3 |
|
$map = $this->di->get("map"); |
41
|
3 |
|
$mapLink = $map->getMap($ipstackRes["longitude"], $ipstackRes["latitude"]); |
42
|
|
|
|
43
|
|
|
$data = [ |
44
|
3 |
|
"ip" => $ip, |
45
|
3 |
|
"valid" => $valid ?? null, |
46
|
3 |
|
"protocol" => $protocol ?? null, |
47
|
3 |
|
"host" => $host ?? null, |
48
|
3 |
|
"country_name" => $ipstackRes["country_name"] ?? null, |
49
|
3 |
|
"city" => $ipstackRes["city"] ?? null, |
50
|
3 |
|
"longitude" => $ipstackRes["longitude"] ?? null, |
51
|
3 |
|
"latitude" => $ipstackRes["latitude"] ?? null, |
52
|
3 |
|
"map_link" => $mapLink ?? null |
53
|
|
|
]; |
54
|
|
|
|
55
|
3 |
|
return [$data]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get ip data |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
public function getIPData($ip) |
65
|
|
|
{ |
66
|
|
|
$ipstack = $this->di->get("ipstack"); |
67
|
|
|
$ipstack->setUrl($ip); |
68
|
|
|
$ipstackRes = $ipstack->getData(); |
69
|
|
|
|
70
|
|
|
return $ipstackRes; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|