1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hab\MeModule; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Hab\MeModule; |
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 GeoController implements ContainerInjectableInterface |
23
|
|
|
{ |
24
|
|
|
use ContainerInjectableTrait; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string $res is the response of filters on selected IP |
30
|
|
|
*/ |
31
|
|
|
private $res = []; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The initialize method is optional and will always be called before the |
36
|
|
|
* target method/action. This is a convienient method where you could |
37
|
|
|
* setup internal properties that are commonly used by several methods. |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
3 |
|
public function initialize() : void |
42
|
|
|
{ |
43
|
|
|
// Use to initialise member variables. |
44
|
3 |
|
$this->res = []; |
|
|
|
|
45
|
3 |
|
$session = $this->di->get("session"); |
46
|
3 |
|
if ($session->get("res")) { |
47
|
1 |
|
$this->res = $session->get("res"); |
48
|
|
|
} |
49
|
3 |
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates page and form for GEO. |
55
|
|
|
* @return page |
|
|
|
|
56
|
|
|
*/ |
57
|
1 |
|
public function indexActionGet() : object |
58
|
|
|
{ |
59
|
1 |
|
$request = $this->di->get("request"); |
60
|
1 |
|
$page = $this->di->get("page"); |
61
|
|
|
|
62
|
1 |
|
$defaultIP = $request->getServer("HTTP_X_FORWARDED_FOR") ? $request->getServer("HTTP_X_FORWARDED_FOR") : "45.144.116.1"; |
63
|
|
|
$data = [ |
64
|
1 |
|
"res" => $this->res, |
65
|
1 |
|
"defaultIP" => $defaultIP, |
66
|
|
|
]; |
67
|
1 |
|
$title = "Validate IP"; |
68
|
1 |
|
$page->add("MeModule/geo-ip", $data); |
69
|
|
|
|
70
|
1 |
|
return $page->render(["title" => $title]); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function searchActionPost() |
74
|
|
|
{ |
75
|
1 |
|
$response = $this->di->get("response"); |
76
|
1 |
|
$request = $this->di->get("request"); |
77
|
1 |
|
$session = $this->di->get("session"); |
78
|
1 |
|
$ip = $request->getPost("ip"); |
79
|
1 |
|
$validator = new \Hab\MeModule\ValidateIP($ip); |
80
|
1 |
|
$data = $validator->sendRes(); |
81
|
1 |
|
if ($data["isValid"]) { |
82
|
1 |
|
$api = require(ANAX_INSTALL_PATH . "/config.php"); |
83
|
1 |
|
$key = $api["key"]; |
84
|
1 |
|
$fetch = new \Hab\MeModule\Fetch("GET", "http://api.ipstack.com/$ip?access_key=$key"); |
85
|
1 |
|
$this->res["fetch"] = json_decode($fetch->fetch()); |
86
|
|
|
} |
87
|
1 |
|
$this->res["data"] = $data; |
88
|
1 |
|
$session->set("res", $this->res); |
89
|
|
|
|
90
|
1 |
|
return $response->redirect("geo"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Resets session variable's response |
95
|
|
|
* @return redirect |
|
|
|
|
96
|
|
|
*/ |
97
|
1 |
|
public function resetActionPost() : object |
98
|
|
|
{ |
99
|
1 |
|
$response = $this->di->get("response"); |
100
|
1 |
|
$session = $this->di->get("session"); |
101
|
1 |
|
$this->res = []; |
|
|
|
|
102
|
1 |
|
$session->set("res", $this->res); |
103
|
|
|
|
104
|
1 |
|
return $response->redirect("geo"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..