1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nihl\IPValidator; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* IP-Validator analyzes ip-adresses according to ip4 and ip6 |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class IPValidatorController implements ContainerInjectableInterface |
13
|
|
|
{ |
14
|
|
|
use ContainerInjectableTrait; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var object $ipvalidator instance of IPValidator class |
19
|
|
|
*/ |
20
|
|
|
private $ipvalidator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initiate IPValidator |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
5 |
|
public function initialize() : void |
28
|
|
|
{ |
29
|
|
|
// Use to initialise member variables. |
30
|
5 |
|
$this->ipvalidator = $this->di->get("ipvalidator"); |
31
|
5 |
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Index action |
36
|
|
|
* |
37
|
|
|
* @return object |
38
|
|
|
*/ |
39
|
4 |
|
public function indexAction() : object |
40
|
|
|
{ |
41
|
4 |
|
$title = "IP-Validator"; |
42
|
|
|
|
43
|
|
|
// Fetch services from di |
44
|
4 |
|
$request = $this->di->get("request"); |
45
|
4 |
|
$ipvalidator = $this->di->get("ipvalidator"); |
46
|
4 |
|
$page = $this->di->get("page"); |
47
|
4 |
|
$geotag = $this->di->get("geotag"); |
48
|
|
|
|
49
|
|
|
// Get users IP-address |
50
|
4 |
|
$userIP = $ipvalidator->getUserIP($request->getServer()); |
51
|
|
|
|
52
|
|
|
// Set IP to validate, UserIP as default |
53
|
4 |
|
$ipToValidate = $request->getGet("ip", $userIP); |
54
|
|
|
|
55
|
|
|
// Validate ip |
56
|
4 |
|
$data = $ipvalidator->validateIP($ipToValidate); |
57
|
|
|
|
58
|
|
|
// Render index page |
59
|
4 |
|
$page->add("nihl/ip-validator/index", $data); |
60
|
|
|
|
61
|
|
|
// Add location data and map if it is available |
62
|
4 |
|
$geotagdata = $geotag->getIPData($ipToValidate); |
63
|
|
|
|
64
|
4 |
|
if (is_array($geotagdata) && $geotagdata["type"]) { |
65
|
4 |
|
$geotagdata["map"] = $geotag->renderMap($geotagdata["latitude"], $geotagdata["longitude"]); |
66
|
|
|
|
67
|
4 |
|
$page->add("nihl/ip-validator/geotag", $geotagdata); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
return $page->render([ |
71
|
4 |
|
"title" => $title |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Adding an optional catchAll() method will catch all actions sent to the |
79
|
|
|
* router. You can then reply with an actual response or return void to |
80
|
|
|
* allow for the router to move on to next handler. |
81
|
|
|
* A catchAll() handles the following, if a specific action method is not |
82
|
|
|
* created: |
83
|
|
|
* ANY METHOD mountpoint/** |
84
|
|
|
* |
85
|
|
|
* @param array $args as a variadic parameter. |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
* |
89
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
90
|
|
|
*/ |
91
|
1 |
|
public function catchAll(...$args) |
|
|
|
|
92
|
|
|
{ |
93
|
1 |
|
$title = "IP-Validator | Route not found"; |
94
|
1 |
|
$page = $this->di->get("page"); |
95
|
1 |
|
$path = $this->di->get("request")->getRoute(); |
96
|
1 |
|
$page->add("nihl/ip-validator/error", [ |
97
|
1 |
|
"path" => $path |
98
|
|
|
]); |
99
|
|
|
|
100
|
1 |
|
return $page->render([ |
101
|
1 |
|
"title" => $title |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.