Key.php (9397B) download
1<?php
2class Key
3{
4
5 protected array $data;
6 function retrieveData($key)
7 {
8 $validKeys = [
9 ':3jvl7yb5sRr80s6lTdeOyxV9VTQZkCPRp7bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk' => 'retrieveZooData',
10 ':ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684' => 'retrieveRBData',
11 ];
12 if (array_key_exists($key['data'], $validKeys)) {
13 $method = $validKeys[$key['data']];
14 $this->$method($key);
15 } else {
16 echo "You have no access here!";
17 }
18 }
19
20 function retrieveZooData($key)
21 {
22 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
23 $weather_data = [];
24 $query = "SELECT distinct temperature, wind_speed, nl.name as city, co.country as country
25 FROM weather_data wd
26 JOIN station s ON wd.station_name = s.name
27 JOIN contract_station cs ON cs.station_name = s.name
28 JOIN nearestlocation nl ON nl.station_name = s.name
29 JOIN country co ON co.country_code = nl.country_code
30 JOIN geolocation geo ON geo.country_code = co.country_code
31 JOIN contract c ON cs.contract_id = c.contract_id
32 WHERE c.token = '3jvl/yb?sRr80s6lTdeOyxV9VTQZkCPRp/bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk'
33 AND date(wd.date_time) = date(curdate())
34 GROUP BY nl.name
35 ";
36
37 $stmt = mysqli_prepare($db, $query);
38 $stmt->execute();
39 $data = $stmt->get_result();
40
41 while ($row = $data->fetch_assoc()) {
42 $wind_chill = windchill($row["temperature"], $row["wind_speed"]);
43 $weather_data[] = [
44 'windchill' => $wind_chill,
45 'location' => $row['city'],
46 'country' => $row['country']
47 ];
48 }
49 header('Content-Type: application/json');
50 echo json_encode($weather_data);
51 }
52
53 function retrieveRBData($key)
54 {
55 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
56 $weather_data = [];
57 $query = "SELECT distinct temperature, dew_point, nl.name as city, date_time
58 FROM weather_data wd
59 JOIN station s ON wd.station_name = s.name
60 JOIN contract_station cs ON cs.station_name = s.name
61 JOIN nearestlocation nl ON nl.station_name = s.name
62 JOIN country co ON co.country_code = nl.country_code
63 JOIN geolocation geo ON geo.country_code = co.country_code
64 JOIN contract c ON cs.contract_id = c.contract_id
65 WHERE c.token = 'ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684'
66 ";
67 $stmt = mysqli_prepare($db, $query);
68 $stmt->execute();
69 $data = $stmt->get_result();
70
71 while ($row = $data->fetch_assoc()) {
72 $humidity = humid($row["temperature"], $row["dew_point"]);
73 $weather_data[] = [
74 'humidity' => $humidity,
75 'location' => $row['city'],
76 'datetime' => $row['date_time']
77 ];
78
79 }
80
81 header('Content-Type: application/json');
82 echo json_encode($weather_data);
83 }
84
85
86 function retrieveHumidityTable($key, $generateXml = false)
87 {
88 $html = '<table>';
89 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
90 $query = "SELECT distinct temperature, dew_point, nl.name as city, date_time
91 FROM weather_data wd
92 JOIN station s ON wd.station_name = s.name
93 JOIN contract_station cs ON cs.station_name = s.name
94 JOIN nearestlocation nl ON nl.station_name = s.name
95 JOIN country co ON co.country_code = nl.country_code
96 JOIN geolocation geo ON geo.country_code = co.country_code
97 JOIN contract c ON cs.contract_id = c.contract_id
98 WHERE c.token = ?
99 ORDER BY date_time DESC";
100 $stmt = mysqli_prepare($db, $query);
101 $stmt->bind_param("s", $key);
102 $stmt->execute();
103 $data = $stmt->get_result();
104
105 $results = array();
106
107 $table1 = '<tr><th id="tabledate" colspan="4">Chengdu Humidity Data</th></tr>';
108 $table1 .= '<tr><th>Date</th><th>Humidity</th></tr>';
109 $table2 = '<tr><th id="tabledate" colspan="4">Kangding Humidity Data</th></tr>';
110 $table2 .= '<tr><th>Date</th><th>Humidity</th></tr>';
111 $xml1 = '<location name="Chengdu">';
112 $xml2 = '<location name="Kangding">';
113 while ($row = $data->fetch_assoc()) {
114 $humidity = humid($row["temperature"], $row["dew_point"]);
115 $current_date = $row['date_time'];
116 $correct_date = date("d M Y H:i:s", strtotime($current_date));
117 $city = $row['city'];
118
119 $results[$city] = array();
120
121 $results[$city][] = array(
122 'date' => $correct_date,
123 'humidity' => $humidity
124 );
125
126 // Table and Generate XML
127 if ($city == 'Chengdu') {
128 $table1 .= '<tr><td>' . $correct_date . '</td><td>' . $humidity . '</td></tr>';
129 $xml1 .= '<data>' . '<date>' . $correct_date . '</date><humidity>' . $humidity . '</humidity>' . '</data>';
130 } else {
131 $table2 .= '<tr><td>' . $correct_date . '</td><td>' . $humidity . '</td></tr>';
132 $xml2 .= '<data>' . '<date>' . $correct_date . '</date><humidity>' . $humidity . '</humidity>' . '</data>';
133 }
134 }
135 $xml1 .= '</location>';
136 $xml2 .= '</location>';
137 $xml = $xml1 . $xml2;
138 $html .= $table1 . $table2;
139 if ($generateXml) {
140 return $xml; // Return SimpleXMLElement object
141 }
142 return $html;
143 }
144
145
146 function retrieveHData($key, $generateXml = false)
147 {
148 $html = '';
149 $citycity = '';
150 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
151 $query = "SELECT distinct temperature, wind_speed, dew_point, DATE(date_time) AS date, nl.name as city
152 FROM weather_data wd
153 JOIN station s ON wd.station_name = s.name
154 JOIN contract_station cs ON cs.station_name = s.name
155 JOIN nearestlocation nl ON nl.station_name = s.name
156 JOIN country co ON co.country_code = nl.country_code
157 JOIN geolocation geo ON geo.country_code = co.country_code
158 JOIN contract c ON cs.contract_id = c.contract_id
159 WHERE c.token = ?
160 ORDER BY date DESC, temperature, wind_speed
161 ";
162
163 $stmt = mysqli_prepare($db, $query);
164 $stmt->bind_param("s", $key);
165 $stmt->execute();
166 $data = $stmt->get_result();
167 $xml = '<WCTPD name="Windchill corrected temperature per day">';
168
169 $results = array();
170 $locations = array();
171
172 while ($row = $data->fetch_assoc()) {
173 $wind_chill = windchill($row["temperature"], $row["wind_speed"]);
174 $current_date = $row['date'];
175 $city = $row['city'];
176
177 if (!isset($results[$current_date])) {
178 $results[$current_date] = array();
179 $locations[$current_date] = array();
180 }
181
182 if (!in_array($city, $locations[$current_date]) && count($results[$current_date]) < 5) {
183 $results[$current_date][] = array(
184 'city' => $city,
185 'windchill' => $wind_chill,
186 );
187
188 $locations[$current_date][] = $city;
189 }
190 }
191 $html .= $this->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684');
192
193 $html .= '<table id="htmlTable">';
194 $html .= '<div class="space"></div>';
195 $html .= '<div class="content-title"><h2>Windchill corrected temperature per day</h2></div>';
196
197 foreach ($results as $date => $entries) {
198 $html .= '<tr><th id="tabledate" colspan="4">' . date("d M Y", strtotime($date)) . '</th></tr>';
199 $html .= '<tr><th>Location</th><th>Windchill</th></tr>';
200
201 foreach ($entries as $entry) {
202 $xml .= '<data city="' . $entry['city'] . '">' . '<wind_chill>' . $entry['windchill'] . '</wind_chill>' . '</data>';
203 $html .= '<tr>';
204 $html .= '<td>' . $entry['city'] . '</td>';
205 $html .= '<td>' . $entry['windchill'] . '</td>';
206 $html .= '</tr>';
207 }
208 }
209
210 $html .= '</table>';
211 $html .= '<a href="?downloadXml=true" class="download-button">Download XML</a>';
212 $xml .= '</WCTPD>';
213 if ($generateXml) {
214 return $xml; // Return SimpleXMLElement object
215 }
216 return $html;
217 }
218}
219
220function windchill($temp, $wind): float
221{
222 $result = 13.12 + 0.6215 * $temp - 11.37 * pow($wind, 0.16) + 0.3965 * $temp * pow($wind, 0.16);
223 return round($result, 2);
224}
225
226function humid($temp, $dewp): float|int
227{
228 $specific_humidity = exp((17.625 * $dewp) / (243.04 + $dewp));
229 $saturation_point = exp((17.625 * $temp) / (243.04 + $temp));
230
231 return round(($specific_humidity / $saturation_point) * 100, 2);
232}
233
234function checkHumid($temp, $dewp): string
235{
236 $humidity = humid($temp, $dewp);
237 if ($humidity < 50) {
238 return "warning";
239 } else {
240 return "dontchu worry";
241 }
242}
243?>