|
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 IPValidatorAPIController implements ContainerInjectableInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use ContainerInjectableTrait; |
|
15
|
|
|
|
|
16
|
|
|
private $ipvalidator; |
|
17
|
|
|
private $request; |
|
18
|
|
|
private $page; |
|
19
|
|
|
private $geotag; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize method to inject from di |
|
23
|
|
|
* |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function initialize(): void |
|
27
|
|
|
{ |
|
28
|
5 |
|
$this->ipvalidator = $this->di->get("ipvalidator"); |
|
29
|
5 |
|
$this->request = $this->di->get("request"); |
|
30
|
5 |
|
$this->page = $this->di->get("page"); |
|
31
|
5 |
|
$this->geotag = $this->di->get("geotag"); |
|
32
|
5 |
|
} |
|
33
|
|
|
/** |
|
34
|
|
|
* This is the index method action, it handles: |
|
35
|
|
|
* GET METHOD mountpoint |
|
36
|
|
|
* GET METHOD mountpoint/ |
|
37
|
|
|
* GET METHOD mountpoint/index |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function indexActionGet() |
|
42
|
|
|
{ |
|
43
|
1 |
|
$title = "IP-Validator REST API"; |
|
44
|
|
|
|
|
45
|
|
|
// Get users IP-address |
|
46
|
1 |
|
$userIP = $this->ipvalidator->getUserIP($this->request->getServer()); |
|
47
|
|
|
|
|
48
|
1 |
|
$this->page->add("nihl/ip-validator/api/index", [ |
|
49
|
1 |
|
"userIP" => $userIP |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
1 |
|
return $this->page->render([ |
|
53
|
1 |
|
"title" => $title |
|
54
|
|
|
]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* This is the index post method action, it handles: |
|
59
|
|
|
* POST mountpoint |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function indexActionPost() |
|
64
|
|
|
{ |
|
65
|
3 |
|
$ipToValidate = $this->request->getPost("ip", null); |
|
66
|
|
|
|
|
67
|
3 |
|
$validation = $this->ipvalidator->validateIP($ipToValidate); |
|
68
|
3 |
|
$geotagData = $this->geotag->getIPData($ipToValidate); |
|
69
|
|
|
|
|
70
|
|
|
$json = [ |
|
71
|
3 |
|
"validation" => $validation |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
3 |
|
if ($validation["match"] && $validation["type"] == "ip4") { |
|
75
|
1 |
|
$json["geotag"] = $this->geotag->getIPData($ipToValidate); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
if (array_key_exists("geotag", $json) && $json["geotag"]["latitude"]) { |
|
79
|
1 |
|
$json["map"] = $this->geotag->getMap($geotagData["latitude"], $geotagData["longitude"]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return [$json]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Adding an optional catchAll() method will catch all actions sent to the |
|
89
|
|
|
* router. You can then reply with an actual response or return void to |
|
90
|
|
|
* allow for the router to move on to next handler. |
|
91
|
|
|
* A catchAll() handles the following, if a specific action method is not |
|
92
|
|
|
* created: |
|
93
|
|
|
* ANY METHOD mountpoint/** |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $args as a variadic parameter. |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
* |
|
99
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function catchAll(...$args) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$data = [ |
|
104
|
1 |
|
"error" => "Bad request. Invalid url." |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
1 |
|
return [$data, 400]; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.