1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Anax\IpGeo\IpGeo; |
8
|
|
|
|
9
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
10
|
|
|
// use Anax\Route\Exception\NotFoundException; |
11
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A sample controller to show how a controller class can be implemented. |
15
|
|
|
* The controller will be injected with $di if implementing the interface |
16
|
|
|
* ContainerInjectableInterface, like this sample class does. |
17
|
|
|
* The controller is mounted on a particular route and can then handle all |
18
|
|
|
* requests for that mount point. |
19
|
|
|
* |
20
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
21
|
|
|
*/ |
22
|
|
|
class IpGeoController implements ContainerInjectableInterface |
23
|
|
|
{ |
24
|
|
|
use ContainerInjectableTrait; |
25
|
|
|
protected $ipGeo; |
26
|
|
|
protected $request; |
27
|
|
|
protected $response; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string $db a sample member variable that gets initialised |
32
|
|
|
*/ |
33
|
|
|
/** |
34
|
|
|
* The initialize method is optional and will always be called before the |
35
|
|
|
* target method/action. This is a convienient method where you could |
36
|
|
|
* setup internal properties that are commonly used by several methods. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
5 |
|
public function initialize(): void |
41
|
|
|
{ |
42
|
|
|
// Use to initialise member variables. |
43
|
5 |
|
$this->ipGeo = new IpGeo(); |
44
|
5 |
|
$this->request = $this->di->get("request"); |
45
|
5 |
|
$this->response = $this->di->get("response"); |
46
|
5 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This is the index method action, it handles: |
52
|
|
|
* ANY METHOD mountpoint |
53
|
|
|
* ANY METHOD mountpoint/ |
54
|
|
|
* ANY METHOD mountpoint/index |
55
|
|
|
* |
56
|
|
|
* @return object |
57
|
|
|
*/ |
58
|
4 |
|
public function indexAction(): object |
59
|
|
|
{ |
60
|
4 |
|
$page = $this->di->get("page"); |
61
|
4 |
|
$title = "Ip Geolokalisering"; |
62
|
4 |
|
$ipAddress = $this->request->getGet("ip"); |
63
|
4 |
|
if ($ipAddress) { |
64
|
1 |
|
$ipGeoInfo = $this->ipGeo->getLocation($ipAddress); |
65
|
|
|
} else { |
66
|
3 |
|
$req = $this->di->get("request"); |
67
|
3 |
|
if (!empty($req->getServer("HTTP_CLIENT_IP"))) { |
68
|
1 |
|
$ipAddress = $req->getServer("HTTP_CLIENT_IP"); |
69
|
2 |
|
} elseif (!empty($req->getServer("HTTP_X_FORWARDED_FOR"))) { |
70
|
1 |
|
$ipAddress = $req->getServer("HTTP_X_FORWARDED_FOR"); |
71
|
|
|
} else { |
72
|
1 |
|
$ipAddress = $req->getServer("REMOTE_ADDR"); |
73
|
|
|
} |
74
|
3 |
|
$ipGeoInfo = $this->ipGeo->getLocation($ipAddress); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
4 |
|
$page->add("ipGeo/index", [ |
79
|
4 |
|
"geoInfo" => $ipGeoInfo, |
80
|
4 |
|
"ip" => $ipAddress, |
81
|
4 |
|
"title" => $title, |
82
|
|
|
]); |
83
|
|
|
|
84
|
4 |
|
return $page->render(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This sample method action it the handler for route: |
90
|
|
|
* POST mountpoint/create |
91
|
|
|
* |
92
|
|
|
* @return object |
93
|
|
|
*/ |
94
|
1 |
|
public function indexActionPost(): object |
95
|
|
|
{ |
96
|
1 |
|
$ipAddress = $this->request->getPost("ipAddress"); |
97
|
|
|
|
98
|
1 |
|
return $this->response->redirect("geo_ip?ip=$ipAddress"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|