1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Anax\Models\IpValidator; |
8
|
|
|
use Anax\Models\GeoApi; |
9
|
|
|
use Anax\Models\CurrentIp; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Controllerclass for IP validation |
13
|
|
|
*/ |
14
|
|
|
class IpController implements ContainerInjectableInterface |
15
|
|
|
{ |
16
|
|
|
use ContainerInjectableTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* rendering index page for user to type ip address |
20
|
|
|
* with current ip as default value |
21
|
|
|
*/ |
22
|
1 |
|
public function indexAction() |
23
|
|
|
{ |
24
|
|
|
// var_dump($this->di); |
25
|
1 |
|
$page = $this->di->get("page"); |
26
|
1 |
|
$title = "Validera IP-adress"; |
27
|
|
|
|
28
|
1 |
|
$userIP = $this->di->get("currentip"); |
29
|
|
|
|
30
|
1 |
|
$page->add("ip/index", $userIP); |
31
|
1 |
|
return $page->render([ |
32
|
1 |
|
"title" => $title, |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* ip validation function - using the model ipValidator and geoApi |
38
|
|
|
* for better testing move page rendering out of function |
39
|
|
|
*/ |
40
|
|
|
public function validateIpAction() |
41
|
|
|
{ |
42
|
|
|
$page = $this->di->get("page"); |
43
|
|
|
$ipAdress = $_GET["ipAdress"]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create an instance of class GeoApi which inherits from IpValidator |
47
|
|
|
* Validates IP address in parent class and finds location in child class |
48
|
|
|
*/ |
49
|
|
|
$ipAndLocation = $this->di->get("geoapi"); |
50
|
|
|
|
51
|
|
|
$data = [ |
52
|
|
|
"valid" => $ipAndLocation->validateIp($ipAdress)["res"], |
53
|
|
|
"domain" => $ipAndLocation->validateIp($ipAdress)["domain"] ?? null, |
54
|
|
|
"location" => $ipAndLocation->findGeoLocation($ipAdress) ?? null |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$title = "Resultat"; |
58
|
|
|
$page->add("ip/result", $data); |
59
|
|
|
|
60
|
|
|
return $page->render([ |
61
|
|
|
"title" => $title, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|