1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bjos\GeoLocation; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
use Bjos\Validate\ValidateIp; |
9
|
|
|
|
10
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
11
|
|
|
// use Anax\Route\Exception\NotFoundException; |
12
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Controller to get location of an IP address. |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class GeoApiController implements ContainerInjectableInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerInjectableTrait; |
21
|
|
|
|
22
|
|
|
protected $geo; |
23
|
|
|
private $option; |
24
|
|
|
private $url; |
25
|
|
|
private $validateIp; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The initialize method is optional and will always be called before the |
29
|
|
|
* target method/action. This is a convienient method where you could |
30
|
|
|
* setup internal properties that are commonly used by several methods. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
5 |
|
public function initialize() : void |
35
|
|
|
{ |
36
|
|
|
// Use to initialise member variables. |
37
|
5 |
|
$this->geo = $this->di->get("geolocation"); |
38
|
5 |
|
$this->validateIp = new ValidateIp(); |
39
|
5 |
|
$this->url = "http://api.ipstack.com/"; |
40
|
5 |
|
$this->option = "?access_key="; |
41
|
5 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Shows basic instruction how to use api. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
1 |
|
public function indexActionGet() : array |
49
|
|
|
{ |
50
|
|
|
$json = [ |
51
|
|
|
"data" => [ |
52
|
1 |
|
"message" => "Use POST with IP-address in body to validate and locate ip", |
53
|
|
|
"example" => "POST /geoapi/ {'ip': '8.8.8.8'}", |
54
|
|
|
], |
55
|
|
|
]; |
56
|
1 |
|
return [$json]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* This is the index method action, it handles: |
61
|
|
|
* IP validation and localization of the queryparameter ip. |
62
|
|
|
* |
63
|
|
|
* @return object |
64
|
|
|
*/ |
65
|
4 |
|
public function indexActionPost() : array |
66
|
|
|
{ |
67
|
4 |
|
$ipAdr = $this->di->request->getPost("ip"); |
|
|
|
|
68
|
4 |
|
$mapUrl = "https://www.openstreetmap.org/search?query="; |
69
|
4 |
|
$valid = $this->validateIp->validate($ipAdr); |
70
|
|
|
|
71
|
4 |
|
if ($valid) { |
72
|
2 |
|
$location = $this->geo->getLocation($ipAdr, $this->url, $this->option); |
73
|
2 |
|
$type = $this->validateIp->getIpType($ipAdr); |
74
|
2 |
|
$host = $this->validateIp->getHostName($ipAdr); |
75
|
2 |
|
$mapLink = $mapUrl . $location['latitude'] . "," . $location['longitude']; |
76
|
|
|
$json = [ |
77
|
2 |
|
"ip" => $ipAdr, |
78
|
2 |
|
"valid" => $valid, |
79
|
2 |
|
"type" => $type, |
80
|
2 |
|
"host" => $host, |
81
|
2 |
|
"continent_name" => $location["continent_name"], |
82
|
2 |
|
"country" => $location["country_name"], |
83
|
2 |
|
"city" => $location["city"], |
84
|
2 |
|
"zip" => $location["zip"], |
85
|
2 |
|
"latitude" => $location["latitude"], |
86
|
2 |
|
"longitude" => $location["longitude"], |
87
|
2 |
|
"country_flag" => $location["location"]["country_flag"], |
88
|
2 |
|
"map_link" => $location['latitude'] !== null ? $mapLink : null |
89
|
|
|
]; |
90
|
2 |
|
} else if (isset($ipAdr) && !$valid) { |
91
|
1 |
|
http_response_code(400); |
92
|
|
|
$json = [ |
93
|
|
|
"error" => [ |
94
|
1 |
|
"status" => http_response_code(), |
95
|
1 |
|
"error" => "Bad Request", |
96
|
1 |
|
"ip" => $ipAdr, |
97
|
1 |
|
"message" => "Not a valid IP-Address" |
98
|
|
|
] |
99
|
|
|
]; |
100
|
|
|
} else { |
101
|
1 |
|
http_response_code(400); |
102
|
|
|
$json = [ |
103
|
|
|
"error" => [ |
104
|
1 |
|
"status" => http_response_code(), |
105
|
1 |
|
"error" => "Bad Request", |
106
|
1 |
|
"message" => "ip missing in body", |
107
|
|
|
] |
108
|
|
|
]; |
109
|
|
|
} |
110
|
4 |
|
return [$json]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|