|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
|
4
|
|
|
|
|
5
|
|
|
namespace artes\Weather; |
|
6
|
|
|
|
|
7
|
|
|
// use Anax\Commons\AppInjectableInterface; |
|
8
|
|
|
// use Anax\Commons\AppInjectableTrait; |
|
9
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
10
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
11
|
|
|
use Anax\Route\Exception\NotFoundException; |
|
12
|
|
|
use artes\IP\IPGeotag; |
|
13
|
|
|
use artes\IP\RealIP; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
17
|
|
|
* The controller will be injected with $app if implementing the interface |
|
18
|
|
|
* AppInjectableInterface, like this sample class does. |
|
19
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
20
|
|
|
* requests for that mount point. |
|
21
|
|
|
* |
|
22
|
|
|
* @SuppressWarnings(PHPMD) |
|
23
|
|
|
*/ |
|
24
|
|
|
class WeatherAPIController implements ContainerInjectableInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ContainerInjectableTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This is the index method action, it handles: |
|
30
|
|
|
* ANY METHOD mountpoint |
|
31
|
|
|
* ANY METHOD mountpoint/ |
|
32
|
|
|
* ANY METHOD mountpoint/index |
|
33
|
|
|
* |
|
34
|
|
|
* @return object |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function indexActionGet() : object |
|
37
|
|
|
{ |
|
38
|
1 |
|
$page = $this->di->get("page"); |
|
39
|
1 |
|
$realip = new RealIP(); |
|
40
|
1 |
|
$ipaddress = $realip->getRealIpAddr(); |
|
41
|
|
|
$data = [ |
|
42
|
1 |
|
"ip" => $ipaddress |
|
43
|
|
|
]; |
|
44
|
1 |
|
$page->add( |
|
45
|
1 |
|
"weather/weatherapi", |
|
46
|
|
|
$data |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
1 |
|
return $page->render([ |
|
50
|
1 |
|
"title" => "My IP", |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* This is the index method action, it handles: |
|
56
|
|
|
* ANY METHOD mountpoint |
|
57
|
|
|
* ANY METHOD mountpoint/ |
|
58
|
|
|
* ANY METHOD mountpoint/index |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function infoActionGet() : array |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->makeJSON(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* This is the index method action, it handles: |
|
69
|
|
|
* ANY METHOD mountpoint |
|
70
|
|
|
* ANY METHOD mountpoint/ |
|
71
|
|
|
* ANY METHOD mountpoint/index |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function infoActionPost() : array |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->makeJSON(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function makeJSON() |
|
81
|
|
|
{ |
|
82
|
2 |
|
$ip = $this->di->get("ip"); |
|
83
|
2 |
|
$request = $this->di->get("request"); |
|
84
|
2 |
|
$userip = $request->getGet("ip", "") ?: $request->getPost("userip", ""); |
|
85
|
2 |
|
$lon = $request->getGet("lon", "") ?: $request->getPost("longitud", ""); |
|
86
|
2 |
|
$lat = $request->getGet("lat", "") ?: $request->getPost("latitud", ""); |
|
87
|
2 |
|
$type = $request->getGet("type", "") ?: $request->getPost("type", ""); |
|
88
|
2 |
|
$validator = new ValidAPIWeather($request, $ip); |
|
89
|
2 |
|
if ($validator->errormsg()) { |
|
90
|
|
|
$myjson = [ |
|
91
|
2 |
|
"msg" => $validator->errormsg() |
|
92
|
|
|
]; |
|
93
|
2 |
|
return [json_encode($myjson, JSON_UNESCAPED_UNICODE)]; |
|
94
|
|
|
} |
|
95
|
2 |
|
$alldata = $this->generateData($userip, $lat, $lon, $type); |
|
96
|
2 |
|
$data = $alldata[0]; |
|
97
|
2 |
|
if (!(($lat|| $alldata[1]) && ($lon || $alldata[2]))) { |
|
98
|
|
|
$msg = [ |
|
99
|
2 |
|
"msg" => "No geodata could be detected." |
|
100
|
|
|
]; |
|
101
|
2 |
|
return [json_encode($msg, JSON_UNESCAPED_UNICODE)]; |
|
102
|
|
|
} |
|
103
|
|
|
return [json_encode($data, JSON_UNESCAPED_UNICODE)]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
private function generateData($userip, $lat, $lon, $type) |
|
107
|
|
|
{ |
|
108
|
2 |
|
$ipkey = ""; |
|
109
|
2 |
|
$weatherkey = ""; |
|
110
|
|
|
// this loads $ipkey and $weatherkey |
|
111
|
2 |
|
include(ANAX_INSTALL_PATH . '/config/api/apikeys.php'); |
|
112
|
2 |
|
$geotag = new IPGeotag($ipkey); |
|
113
|
2 |
|
if ($userip) { |
|
114
|
2 |
|
$geoinfo = $geotag->checkdefaultip($userip); |
|
115
|
2 |
|
$lat = $geoinfo["latitude"] ?? ""; |
|
116
|
2 |
|
$lon = $geoinfo["longitude"] ?? ""; |
|
117
|
|
|
} |
|
118
|
2 |
|
$data = $this->getWeather($weatherkey, $lat, $lon, $type); |
|
119
|
2 |
|
return [$data, $lat, $lon]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* This is the index method action, it handles: |
|
124
|
|
|
* ANY METHOD mountpoint |
|
125
|
|
|
* ANY METHOD mountpoint/ |
|
126
|
|
|
* ANY METHOD mountpoint/index |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public function getWeather($weatherkey, $lat, $lon, $type) : array |
|
131
|
|
|
{ |
|
132
|
3 |
|
$openweather = new OpenWeather($weatherkey, $lat, $lon); |
|
133
|
3 |
|
if ($type === "historical") { |
|
134
|
3 |
|
$data = $openweather->historicweather(); |
|
135
|
3 |
|
} elseif ($type === "forecast") { |
|
136
|
3 |
|
$data = $openweather->forecast(); |
|
137
|
|
|
} else { |
|
138
|
3 |
|
$data = $openweather->currentweather(); |
|
139
|
|
|
} |
|
140
|
3 |
|
return $data; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|