1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hab\MeModule; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
14
|
|
|
* The controller will be injected with $di if implementing the interface |
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
17
|
|
|
* requests for that mount point. |
18
|
|
|
* |
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
20
|
|
|
*/ |
21
|
|
|
class LocationJsonController implements ContainerInjectableInterface |
22
|
|
|
{ |
23
|
|
|
use ContainerInjectableTrait; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string $res is the response of filters on selected IP |
29
|
|
|
*/ |
30
|
|
|
private $res = []; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The initialize method is optional and will always be called before the |
35
|
|
|
* target method/action. This is a convienient method where you could |
36
|
|
|
* setup internal properties that are commonly used by several methods. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
4 |
|
public function initialize() : void |
41
|
|
|
{ |
42
|
|
|
// Use to initialise member variables. |
43
|
4 |
|
$this->res = []; |
|
|
|
|
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Takes string from GET query. |
50
|
|
|
* Calculates if string is valid ipv4/ipv6 address |
51
|
|
|
* and if there is a host attached to it. |
52
|
|
|
* Returns response as JSON. |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
4 |
|
public function indexActionGet() : array |
56
|
|
|
{ |
57
|
4 |
|
$request = $this->di->get("request"); |
58
|
4 |
|
$fetch = $this->di->get("fetch"); |
59
|
4 |
|
$validator = $this->di->get("ip"); |
60
|
|
|
|
61
|
4 |
|
$locations = explode(",", $request->getGet("loc", "")); |
62
|
|
|
|
63
|
4 |
|
$keys = require(ANAX_INSTALL_PATH . "/config.php"); |
64
|
4 |
|
$darkKey = $keys["darkSky"]; |
65
|
4 |
|
$boxKey = $keys["mapbox"]; |
66
|
4 |
|
$ipKey = $keys["key"]; |
67
|
|
|
|
68
|
4 |
|
$temp = []; |
69
|
4 |
|
foreach ($locations as $loc) { |
70
|
4 |
|
$validator->setIP(ltrim($loc)); |
71
|
4 |
|
array_push($temp, $validator->sendRes()); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
$ress = []; |
75
|
4 |
|
foreach ($temp as $tmp) { |
76
|
4 |
|
$error = ""; |
77
|
4 |
|
if ($tmp["isValid"]) { |
78
|
2 |
|
$ip = $tmp["ip"]; |
79
|
2 |
|
$res = json_decode($fetch->fetch("GET", "http://api.ipstack.com/$ip?access_key=$ipKey")); |
80
|
2 |
|
if (!$res->city) { |
81
|
1 |
|
$error = "IP: $ip did not provide a location"; |
82
|
|
|
} else { |
83
|
2 |
|
$res = [$res->latitude, $res->longitude]; |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
2 |
|
$ip = $tmp["ip"]; |
87
|
2 |
|
$res = json_decode($fetch->fetch("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/$ip.json?access_token=$boxKey")); |
88
|
|
|
// var_dump(strlen($res->message)); |
89
|
|
|
// die(); |
90
|
2 |
|
if (property_exists($res, "message") || count($res->features) === 0) { |
91
|
1 |
|
$error = "Term: $ip didnt give a result"; |
92
|
|
|
} else { |
93
|
1 |
|
$res = [$res->features[0]->geometry->coordinates[1], $res->features[0]->geometry->coordinates[0]]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
4 |
|
array_push($ress, [$res, $error, $tmp["ip"]]); |
97
|
|
|
} |
98
|
4 |
|
$data = []; |
99
|
4 |
|
foreach ($ress as $locc) { |
100
|
4 |
|
if ($locc[1] == "") { |
101
|
2 |
|
$lat = $locc[0][0]; |
102
|
2 |
|
$lng = $locc[0][1]; |
103
|
2 |
|
$res = json_decode($fetch->fetch("GET", "https://api.darksky.net/forecast/$darkKey/$lat,$lng")); |
104
|
2 |
|
// $res = json_decode($fetch->fetch("GET", "https://api.darksky.net/forecast/$darkKey/$lat,$lng")); |
105
|
|
|
array_push($data, [$res, $locc]); |
106
|
|
|
} else { |
107
|
2 |
|
// var_dump($locc); |
108
|
|
|
array_push($data, [null, $locc]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
4 |
|
|
112
|
|
|
return [ $data ]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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..