1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lii\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Lii\Model\IPValidator; |
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 IPController implements ContainerInjectableInterface |
23
|
|
|
{ |
24
|
|
|
use ContainerInjectableTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $db a sample member variable that gets initialised |
28
|
|
|
*/ |
29
|
|
|
private $db = "not active"; |
30
|
|
|
public $validator = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The initialize method is optional and will always be called before the |
34
|
|
|
* target method/action. This is a convienient method where you could |
35
|
|
|
* setup internal properties that are commonly used by several methods. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function initialize() : void |
40
|
|
|
{ |
41
|
|
|
// Use to initialise member variables. |
42
|
|
|
$this->db = "active"; |
43
|
|
|
$service = $this->di->get("apikeys"); |
44
|
|
|
$this->validator = new IPValidator($service->getIpKey()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This is how a general helper method can be created in the controller. |
50
|
|
|
* |
51
|
|
|
* @param string $method as the method that handled the controller |
52
|
|
|
* action. |
53
|
|
|
* @param array $args as an array of arguments. |
54
|
|
|
* |
55
|
|
|
* @return string as a message to output to help understand how the |
56
|
|
|
* controller method works. |
57
|
|
|
*/ |
58
|
|
|
private function getDetailsOnRequest(string $method, array $args = []) : string |
59
|
|
|
{ |
60
|
|
|
$request = $this->di->get("request"); |
61
|
|
|
$router = $this->di->get("router"); |
62
|
|
|
$path = $request->getRoute(); |
63
|
|
|
$httpMethod = $request->getMethod(); |
64
|
|
|
$mount = rtrim($router->getLastRoute(), "/"); |
65
|
|
|
$numArgs = count($args); |
66
|
|
|
$strArgs = implode(", ", $args); |
67
|
|
|
$queryString = http_build_query($request->getGet(), '', ', '); |
68
|
|
|
|
69
|
|
|
return <<<EOD |
70
|
|
|
<h1>$method</h1> |
71
|
|
|
|
72
|
|
|
<p>The controller mountpoint is '$mount'. |
73
|
|
|
<p>The request was '$path' ($httpMethod). |
74
|
|
|
<p>Got '$numArgs' arguments: '$strArgs'. |
75
|
|
|
<p>Query string contains: '$queryString'. |
76
|
|
|
<p>\$db is '{$this->db}'. |
77
|
|
|
EOD; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* This is the index method action, it handles: |
82
|
|
|
* ANY METHOD mountpoint |
83
|
|
|
* ANY METHOD mountpoint/ |
84
|
|
|
* ANY METHOD mountpoint/index |
85
|
|
|
* |
86
|
|
|
* @return object |
87
|
|
|
*/ |
88
|
1 |
|
public function indexAction() : object |
89
|
|
|
{ |
90
|
1 |
|
$request = $this->di->get("request"); |
91
|
1 |
|
$userIP = $request->getServer("HTTP_X_FORWARDED_FOR", "x.x.x.x"); |
92
|
|
|
|
93
|
1 |
|
$page = $this->di->get("page"); |
94
|
|
|
$data = [ |
95
|
1 |
|
"userIP" => $userIP, |
96
|
|
|
]; |
97
|
|
|
|
98
|
1 |
|
$page->add("Lii/iptest", $data); |
99
|
|
|
|
100
|
1 |
|
return $page->render([ |
101
|
1 |
|
"title" => __METHOD__, |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This is the validate method action, it handles: |
107
|
|
|
* POST METHOD mountpoint/validate |
108
|
|
|
* |
109
|
|
|
* @return object |
110
|
|
|
*/ |
111
|
1 |
|
public function validateActionPost() : object |
112
|
|
|
{ |
113
|
1 |
|
$request = $this->di->get("request"); |
114
|
1 |
|
$inputIP = $request->getPost("ip"); |
115
|
|
|
|
116
|
|
|
// $validator = new IPValidator(); |
117
|
1 |
|
$validationResultIPv4 = $this->validator->validateIPv4($inputIP); |
118
|
1 |
|
$validationResultIPv6 = $this->validator->validateIPv6($inputIP); |
119
|
1 |
|
$domain = $this->validator->checkDomain($inputIP); |
120
|
1 |
|
$location = $this->validator->locateIP($inputIP); |
121
|
|
|
|
122
|
1 |
|
$page = $this->di->get("page"); |
123
|
|
|
$data = [ |
124
|
1 |
|
"ip" => $inputIP, |
125
|
1 |
|
"resultIPv4" => $validationResultIPv4, |
126
|
1 |
|
"resultIPv6" => $validationResultIPv6, |
127
|
1 |
|
"domain" => $domain, |
128
|
1 |
|
"location" => $location, |
129
|
|
|
]; |
130
|
1 |
|
$page->add("Lii/ipresult", $data); |
131
|
|
|
|
132
|
1 |
|
return $page->render([ |
133
|
1 |
|
"title" => __METHOD__, |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Adding an optional catchAll() method will catch all actions sent to the |
140
|
|
|
* router. You can then reply with an actual response or return void to |
141
|
|
|
* allow for the router to move on to next handler. |
142
|
|
|
* A catchAll() handles the following, if a specific action method is not |
143
|
|
|
* created: |
144
|
|
|
* ANY METHOD mountpoint/** |
145
|
|
|
* |
146
|
|
|
* @param array $args as a variadic parameter. |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
* |
150
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
151
|
|
|
*/ |
152
|
|
|
public function catchAll(...$args) |
153
|
|
|
{ |
154
|
|
|
$page = $this->di->get("page"); |
155
|
|
|
$data = [ |
156
|
|
|
"content" => $this->getDetailsOnRequest(__METHOD__, $args), |
157
|
|
|
]; |
158
|
|
|
$page->add("Lii/default", $data); |
159
|
|
|
|
160
|
|
|
return $page->render([ |
161
|
|
|
"title" => __METHOD__, |
162
|
|
|
]); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|