1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Teca\IpValidator; |
4
|
|
|
|
5
|
|
|
use Anax\DI\DIFactoryConfig; |
6
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
7
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
11
|
|
|
*/ |
12
|
|
|
class IpValidatorController implements ContainerInjectableInterface |
13
|
|
|
{ |
14
|
|
|
use ContainerInjectableTrait; |
15
|
|
|
|
16
|
|
|
protected $validator = null; |
17
|
|
|
|
18
|
|
|
public function initialize() |
19
|
|
|
{ |
20
|
|
|
$this->validator = $this->di->get("ipValidator"); |
21
|
|
|
} |
22
|
|
|
|
23
|
5 |
|
public function toc() : array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
5 |
|
"toc" => [ |
27
|
|
|
"ipvalidator" => [ |
28
|
|
|
"sectionHeader" => true, |
29
|
|
|
"title" => "info", |
30
|
|
|
"linkable" => true, |
31
|
|
|
"level" => 1 |
32
|
|
|
], |
33
|
|
|
"ipvalidator/forms" => [ |
34
|
|
|
"sectionHeader" => true, |
35
|
|
|
"title" => "Formulär", |
36
|
|
|
"linkable" => false, |
37
|
|
|
"level" => 1 |
38
|
|
|
], |
39
|
|
|
"ipvalidator/form" => [ |
40
|
|
|
"sectionHeader" => false, |
41
|
|
|
"title" => "ip validator", |
42
|
|
|
"linkable" => true, |
43
|
|
|
"level" => 1 |
44
|
|
|
], |
45
|
|
|
"ipvalidator/json" => [ |
46
|
|
|
"sectionHeader" => false, |
47
|
|
|
"title" => "ip validator API", |
48
|
|
|
"linkable" => true, |
49
|
|
|
"level" => 1 |
50
|
|
|
], |
51
|
|
|
"ipvalidator/test" => [ |
52
|
|
|
"sectionHeader" => true, |
53
|
|
|
"title" => "Tester av API", |
54
|
|
|
"linkable" => false, |
55
|
|
|
"level" => 1 |
56
|
|
|
], |
57
|
|
|
"ipvalidator/test0" => [ |
58
|
|
|
"sectionHeader" => false, |
59
|
|
|
"title" => "test0: IPv4 - localhost", |
60
|
|
|
"linkable" => true, |
61
|
|
|
"level" => 1 |
62
|
|
|
], |
63
|
|
|
"ipvalidator/test1" => [ |
64
|
|
|
"sectionHeader" => false, |
65
|
|
|
"title" => "test1: IPv4 - dbwebb.se", |
66
|
|
|
"linkable" => true, |
67
|
|
|
"level" => 1 |
68
|
|
|
], |
69
|
|
|
"ipvalidator/test2" => [ |
70
|
|
|
"sectionHeader" => false, |
71
|
|
|
"title" => "test2: IPv6 - google.se", |
72
|
|
|
"linkable" => true, |
73
|
|
|
"level" => 1 |
74
|
|
|
], |
75
|
|
|
"ipvalidator/test3" => [ |
76
|
|
|
"sectionHeader" => false, |
77
|
|
|
"title" => "test3: Ogiltig IP adress", |
78
|
|
|
"linkable" => true, |
79
|
|
|
"level" => 1 |
80
|
|
|
] |
81
|
|
|
], |
82
|
|
|
"title" => "IP Validering" |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function indexAction() : object |
87
|
|
|
{ |
88
|
1 |
|
$page = $this->di->get("page"); |
89
|
|
|
|
90
|
1 |
|
$page->add("anax/v2/toc/default", $this->toc()); |
91
|
1 |
|
$page->add("IpValidator/info", []); |
92
|
|
|
|
93
|
1 |
|
return $page->render([ |
94
|
1 |
|
"title" => "IP Validator Info", |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function formAction() : object |
99
|
|
|
{ |
100
|
1 |
|
$page = $this->di->get("page"); |
101
|
|
|
|
102
|
1 |
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
103
|
|
|
$ipAddress = $_SERVER['HTTP_CLIENT_IP']; |
104
|
1 |
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
105
|
|
|
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
106
|
1 |
|
} else if (!empty($_SERVER['REMOTE_ADDR'])) { |
107
|
|
|
$ipAddress = $_SERVER['REMOTE_ADDR']; |
108
|
|
|
} else { |
109
|
1 |
|
$ipAddress = ""; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
$page->add("anax/v2/toc/default", $this->toc()); |
113
|
1 |
|
$page->add("IpValidator/form", [ |
114
|
1 |
|
"action" => "../ipvalidator/verify", |
115
|
1 |
|
"method" => "GET", |
116
|
1 |
|
"ipAddress" => $ipAddress |
117
|
|
|
]); |
118
|
|
|
|
119
|
1 |
|
return $page->render([ |
120
|
1 |
|
"title" => "Validera IP", |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function jsonActionGet() : object |
125
|
|
|
{ |
126
|
1 |
|
$page = $this->di->get("page"); |
127
|
|
|
|
128
|
1 |
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
129
|
|
|
$ipAddress = $_SERVER['HTTP_CLIENT_IP']; |
130
|
1 |
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
131
|
|
|
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
132
|
1 |
|
} else if (!empty($_SERVER['REMOTE_ADDR'])) { |
133
|
|
|
$ipAddress = $_SERVER['REMOTE_ADDR']; |
134
|
|
|
} else { |
135
|
1 |
|
$ipAddress = ""; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
$page->add("anax/v2/toc/default", $this->toc()); |
139
|
1 |
|
$page->add("IpValidator/form", [ |
140
|
1 |
|
"title" => "IP Validator JSON", |
141
|
1 |
|
"text" => "Formuläret skickar en POST request som sedan visas i webbläsaren som rå JSON. Mer <a href=\"./\">info</a>.", |
142
|
1 |
|
"action" => "../ipvalidatorapi", |
143
|
1 |
|
"method" => "POST", |
144
|
1 |
|
"ipAddress" => $ipAddress |
145
|
|
|
]); |
146
|
|
|
|
147
|
1 |
|
return $page->render([ |
148
|
1 |
|
"title" => "(JSON) Validera IP", |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function verifyAction() : object |
153
|
|
|
{ |
154
|
1 |
|
$page = $this->di->get("page"); |
155
|
1 |
|
$request = $this->di->get("request"); |
156
|
|
|
|
157
|
1 |
|
$ipAddress = $request->getGet("ip"); |
158
|
|
|
|
159
|
1 |
|
$geo = $this->validator->getGeo($ipAddress); |
160
|
|
|
|
161
|
1 |
|
$page->add("anax/v2/toc/default", $this->toc()); |
162
|
1 |
|
$page->add("IpValidator/result", [ |
163
|
1 |
|
"ip" => $ipAddress, |
164
|
1 |
|
"ipv4" => $this->validator->verifyIpv4($ipAddress), |
165
|
1 |
|
"ipv6" => $this->validator->verifyIpv6($ipAddress), |
166
|
1 |
|
"domain" => $this->validator->getDomain($ipAddress), |
167
|
1 |
|
"latitude" => $geo["latitude"], |
168
|
1 |
|
"longitude" => $geo["longitude"], |
169
|
1 |
|
"country_name" => $geo["country_name"], |
170
|
1 |
|
"region_name" => $geo["region_name"], |
171
|
1 |
|
"city" => $geo["city"] |
172
|
|
|
]); |
173
|
|
|
|
174
|
1 |
|
return $page->render([ |
175
|
1 |
|
"title" => "Resultat av IP validering", |
176
|
|
|
]); |
177
|
|
|
} |
178
|
|
|
|
179
|
4 |
|
public function test(string $ipAddress) : array |
180
|
|
|
{ |
181
|
4 |
|
$geo = $this->validator->getGeo($ipAddress); |
182
|
|
|
|
183
|
|
|
$json = [ |
184
|
4 |
|
"ip" => $ipAddress, |
185
|
4 |
|
"ipv4" => $this->validator->verifyIpv4($ipAddress), |
186
|
4 |
|
"ipv6" => $this->validator->verifyIpv6($ipAddress), |
187
|
4 |
|
"domain" => $this->validator->getDomain($ipAddress), |
188
|
4 |
|
"latitude" => $geo["latitude"], |
189
|
4 |
|
"longitude" => $geo["longitude"], |
190
|
4 |
|
"country_name" => $geo["country_name"], |
191
|
4 |
|
"region_name" => $geo["region_name"], |
192
|
4 |
|
"city" => $geo["city"] |
193
|
|
|
]; |
194
|
|
|
|
195
|
4 |
|
return [$json]; |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
public function test0Action() : array |
199
|
|
|
{ |
200
|
1 |
|
$ipAddress = "127.0.0.1"; |
201
|
1 |
|
return $this->test($ipAddress); |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
public function test1Action() : array |
205
|
|
|
{ |
206
|
1 |
|
$ipAddress = "194.47.150.9"; |
207
|
1 |
|
return $this->test($ipAddress); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
public function test2Action() : array |
211
|
|
|
{ |
212
|
1 |
|
$ipAddress = "2a00:1450:400f:808::2003"; |
213
|
1 |
|
return $this->test($ipAddress); |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
public function test3Action() : array |
217
|
|
|
{ |
218
|
1 |
|
$ipAddress = "256.256.256.256"; |
219
|
1 |
|
return $this->test($ipAddress); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|