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 GeoLocationController implements ContainerInjectableInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerInjectableTrait; |
21
|
|
|
|
22
|
|
|
protected $geo; |
23
|
|
|
private $option; |
24
|
|
|
private $url; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The initialize method is optional and will always be called before the |
28
|
|
|
* target method/action. This is a convienient method where you could |
29
|
|
|
* setup internal properties that are commonly used by several methods. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
1 |
|
public function initialize() : void |
34
|
|
|
{ |
35
|
|
|
// Use to initialise member variables. |
36
|
1 |
|
$this->geo = $this->di->get("geolocation"); |
37
|
1 |
|
$this->url = "http://api.ipstack.com/"; |
38
|
1 |
|
$this->option = "?access_key="; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* This is the index method action, it handles: |
43
|
|
|
* IP validation and localization of the queryparameter ip. |
44
|
|
|
* |
45
|
|
|
* @return object |
46
|
|
|
*/ |
47
|
1 |
|
public function indexActionGet() : object |
48
|
|
|
{ |
49
|
1 |
|
$title = "Geolocation"; |
50
|
1 |
|
$page = $this->di->get("page"); |
51
|
1 |
|
$ipAdr = $this->di->request->getGet("ip"); |
|
|
|
|
52
|
|
|
|
53
|
1 |
|
$validateIp = new ValidateIp(); |
54
|
|
|
|
55
|
1 |
|
$host = null; |
56
|
1 |
|
$type = null; |
57
|
1 |
|
$location = null; |
58
|
1 |
|
$valid = $validateIp->validate($ipAdr); |
59
|
|
|
|
60
|
1 |
|
if ($valid) { |
61
|
1 |
|
$location = $this->geo->getLocation($ipAdr, $this->url, $this->option); |
62
|
1 |
|
$type = $validateIp->getIpType($ipAdr); |
63
|
1 |
|
$host = $validateIp->getHostName($ipAdr); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$userIp = $this->geo->getUserIp(); |
67
|
|
|
|
68
|
|
|
$data = [ |
69
|
1 |
|
"valid" => $valid, |
70
|
1 |
|
"ip" => $ipAdr, |
71
|
1 |
|
"type" => $type, |
72
|
1 |
|
"host" => $host, |
73
|
1 |
|
"userIp" => $userIp, |
74
|
1 |
|
"location" => $location |
75
|
|
|
]; |
76
|
|
|
|
77
|
1 |
|
$page->add("bjos/geolocation/index", $data); |
78
|
|
|
|
79
|
1 |
|
$page->add("bjos/geolocation/geomap", [ |
80
|
1 |
|
"location" => $location |
81
|
|
|
]); |
82
|
|
|
|
83
|
1 |
|
$page->add("bjos/geolocation/geoapi", [ |
84
|
1 |
|
"userIp" => $userIp |
85
|
|
|
]); |
86
|
|
|
|
87
|
1 |
|
return $page->render([ |
88
|
1 |
|
"title" => $title |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|