|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
use Anax\Weather\Weather; |
|
8
|
|
|
use Anax\IpGeo\IpGeo; |
|
9
|
|
|
|
|
10
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
11
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
12
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A sample JSON controller to show how a controller class can be implemented. |
|
16
|
|
|
* The controller will be injected with $di if implementing the interface |
|
17
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
18
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
19
|
|
|
* requests for that mount point. |
|
20
|
|
|
*/ |
|
21
|
|
|
class WeatherJsonController implements ContainerInjectableInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use ContainerInjectableTrait; |
|
24
|
|
|
protected $weather; |
|
25
|
|
|
protected $ipGeo; |
|
26
|
|
|
protected $request; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string $db a sample member variable that gets initialised |
|
31
|
|
|
*/ |
|
32
|
|
|
/** |
|
33
|
|
|
* The initialize method is optional and will always be called before the |
|
34
|
|
|
* target method/action. This is a convienient method where you could |
|
35
|
|
|
* setup internal properties that are commonly used by several methods. |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
7 |
|
public function initialize(): void |
|
40
|
|
|
{ |
|
41
|
|
|
// Use to initialise member variables. |
|
42
|
7 |
|
$this->weather = new Weather($this->di); |
|
43
|
7 |
|
$this->ipGeo = new IpGeo(); |
|
44
|
7 |
|
$this->request = $this->di->get("request"); |
|
45
|
7 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* This is the index method action, it handles: |
|
51
|
|
|
* ANY METHOD mountpoint |
|
52
|
|
|
* ANY METHOD mountpoint/ |
|
53
|
|
|
* ANY METHOD mountpoint/index |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
7 |
|
public function indexAction(): array |
|
58
|
|
|
{ |
|
59
|
7 |
|
$title = "Väderprognos"; |
|
60
|
7 |
|
$ipAddress = $this->request->getGet("ip"); |
|
61
|
7 |
|
$city = $this->request->getGet("city"); |
|
62
|
7 |
|
$searchType = $this->request->getGet("search_type"); |
|
63
|
|
|
// $lat = $this->request->getGet("lat"); |
|
64
|
|
|
// $long = $this->request->getGet("long"); |
|
65
|
|
|
|
|
66
|
7 |
|
if ($city) { |
|
67
|
2 |
|
$res = $this->weather->getCoords($city); |
|
68
|
2 |
|
$lat = $res["lat"]; |
|
69
|
2 |
|
$long = $res["long"]; |
|
70
|
5 |
|
} elseif ($ipAddress) { |
|
71
|
2 |
|
$ipInfo = $this->ipGeo->getLocation($ipAddress); |
|
72
|
2 |
|
$lat = $ipInfo["lat"]; |
|
73
|
2 |
|
$long = $ipInfo["long"]; |
|
74
|
|
|
} else { |
|
75
|
3 |
|
$req = $this->di->get("request"); |
|
76
|
3 |
|
if (!empty($req->getServer("HTTP_CLIENT_IP"))) { |
|
77
|
1 |
|
$ipAddress = $req->getServer("HTTP_CLIENT_IP"); |
|
78
|
2 |
|
} elseif (!empty($req->getServer("HTTP_X_FORWARDED_FOR"))) { |
|
79
|
1 |
|
$ipAddress = $req->getServer("HTTP_X_FORWARDED_FOR"); |
|
80
|
|
|
} else { |
|
81
|
1 |
|
$ipAddress = $req->getServer("REMOTE_ADDR"); |
|
82
|
|
|
} |
|
83
|
3 |
|
$ipInfo = $this->ipGeo->getLocation($ipAddress); |
|
84
|
3 |
|
$lat = $ipInfo["lat"]; |
|
85
|
3 |
|
$long = $ipInfo["long"]; |
|
86
|
|
|
} |
|
87
|
7 |
|
if ($lat == "Missing") { |
|
88
|
|
|
$json = [ |
|
89
|
4 |
|
"err" => "Oops, platsinformation saknas", |
|
90
|
4 |
|
"title" => $title, |
|
91
|
4 |
|
"ip" => $ipAddress, |
|
92
|
4 |
|
"weather" => "", |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
4 |
|
return [$json]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
$weatherInfo = $this->weather->getWeather($lat, $long, $searchType); |
|
99
|
|
|
|
|
100
|
|
|
$json = [ |
|
101
|
3 |
|
"weather" => $weatherInfo, |
|
102
|
3 |
|
"ip" => $ipAddress, |
|
103
|
3 |
|
"title" => $title, |
|
104
|
|
|
]; |
|
105
|
3 |
|
if ($searchType == "history") { |
|
106
|
1 |
|
$json["weather"] = $weatherInfo["history"]; |
|
107
|
1 |
|
$json["city"] = $weatherInfo["city"]; |
|
108
|
2 |
|
} elseif ($searchType == "forecast") { |
|
109
|
1 |
|
$json["weather"] = $weatherInfo["daily"]["data"]; |
|
110
|
1 |
|
$json["city"] = $weatherInfo["city"]; |
|
111
|
1 |
|
} elseif ($searchType == "currently") { |
|
112
|
1 |
|
$json["weather"] = $weatherInfo; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
return [$json]; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|