-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
343 lines (312 loc) · 11.8 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000');
header('X-Robots-Tag: noindex, nofollow', true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#0088cc">
<link rel="shortcut icon" href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABqklEQVQ4jZ2Tv0scURDHP7P7SGWh14mkuXJZEH8cgqUWcklAsLBbCEEJSprkD7hD/4BUISHEkMBBiivs5LhCwRQBuWgQji2vT7NeYeF7GxwLd7nl4knMwMDMfL8z876P94TMLt+8D0U0EggQSsAjwMvga8ChJAqxqjTG3m53AQTg4tXHDRH9ABj+zf6oytbEu5d78nvzcyiivx7QXBwy46XOi5z1jbM+Be+nqVfP8yzuD3FM6rzIs9YE1hqGvDf15cVunmdx7w5eYJw1pcGptC9CD4gBUuef5Ujq/BhAlTLIeFYuyfmTZgeYv+2nPt1a371P+Hm1WUPYydKf0lnePwVmh3hnlcO1uc7yvgJUDtdG8oy98kduK2KjeHI0fzCQINSXOk/vlXBUOaihAwnGWd8V5r1uhe1VIK52V6JW2D4FqHZX5lphuwEE7ooyaN7gjLMmKSwYL+pMnV+MA/6+g8RYa2Lg2RBQbj4+rll7uymLy3coiuXb5PdQVf7rKYvojAB8Lf3YUJUHfSYR3XqeLO5JXvk0dhKqSqQQoCO+s5AIxCLa2Lxc6ALcAPwS26XFskWbAAAAAElFTkSuQmCC" />
<title>Weather Updates</title>
<meta name="description" content="Search for City and Country Names to Retrieve Current Weather Status and Conditions."/>
<?php $current_page = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; echo '<link rel="canonical" href="'.$current_page.'" />'; ?>
<link rel="preconnect" href="https://cdnjs.cloudflare.com">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.3/css/bulma.min.css" integrity="sha512-IgmDkwzs96t4SrChW29No3NXBIBv8baW490zk5aXvhCD8vuZM3yUSkbyTBcXohkySecyzIrUwiF/qV0cuPcL3Q==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Fira Code', monospace;
background-color: #eff1f3;
color: #333;
min-height: 100vh;
font-weight: 600;
}
.container {
display: flex;
justify-content: center;
align-items: center;
}
.card-container {
width: 90%;
max-width: 600px;
border-radius: 10px;
background-color: #ffffff;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.input-container {
margin-bottom: 20px;
}
#inputLocation, #apiKey, .button {
border-radius: 20px;
font-family: 'Fira Code', monospace;
width: 100%;
}
#inputLocation, #apiKey {
background-color: #f5f5f5;
color: #333;
border: 1px solid #dcdcdc;
word-wrap: break-word;
padding: 10px;
margin-bottom: 10px;
}
#inputLocation::placeholder, #apiKey::placeholder {
color: #aaa;
opacity: 1;
word-wrap: break-word;
}
#inputLocation:focus, #apiKey:focus {
border-color: #0088cc;
box-shadow: 0 0 0 0.125em rgba(0, 136, 204, 0.25);
}
.content {
word-wrap: break-word;
}
#chat-box {
border: none;
border-radius: 5px;
overflow-y: auto;
margin-bottom: 20px;
background-color: #ffffff;
padding: 10px;
}
.chat-message {
padding: 10px;
margin-bottom: 10px;
}
.user-message {
background-color: #e7f3fe;
border-radius: 5px;
}
.bot-message {
background-color: #edf2f7;
border-radius: 5px;
}
.message-content {
word-wrap: break-word;
white-space: normal;
}
.success-message {
color: #0088cc;
font-weight: bold;
}
.error-message {
color: #ff3860;
font-weight: bold;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<section class="section">
<div class="container">
<div class="card-container">
<div id="chat-box">
<div class="chat-message bot-message">
<div class="message-content success-message">
<p>Enter a city or country name to get weather data.</p><br>
<p>Your API Key Has been Stored on your Brower LocalStorage We Never Logged or Stored your Sensitive Data in our Server | ZERO LOGS Query.</p>
</div>
</div>
</div>
<div class="field input-container">
<div class="control">
<input class="input" type="text" id="inputLocation" placeholder="city or country">
</div>
</div>
<div class="field input-container">
<div class="control">
<input class="input" type="text" id="apiKey" placeholder="Your API key">
</div>
</div>
<div class="field">
<div class="control">
<button class="button is-primary is-fullwidth" onclick="getWeatherData()">Get Data</button>
</div>
</div>
<hr>
<div class="field">
<div class="control">
<button class="button is-danger is-fullwidth" onclick="clearLocalStorage()">Clear LocalStorage</button>
</div>
</div>
<div class="field">
<div class="control">
<button class="button is-info is-fullwidth" onclick="resetForm()">Reset Form</button>
</div>
</div>
</div>
</div>
</section>
<script>
function storeApiKey() {
const apiKey = document.getElementById("apiKey").value.trim();
if (apiKey) {
localStorage.setItem("apiKey", apiKey);
document.getElementById("apiKey").classList.add("hidden");
}
}
function getApiKey() {
return localStorage.getItem("apiKey");
}
document.addEventListener("DOMContentLoaded", function() {
const apiKey = getApiKey();
if (apiKey) {
document.getElementById("apiKey").classList.add("hidden");
}
});
function verifyApiKey(apiKey) {
return true;
}
function getWeatherData() {
storeApiKey();
const apiKey = getApiKey();
const location = document.getElementById("inputLocation").value.trim();
const chatBox = document.getElementById("chat-box");
if (!verifyApiKey(apiKey)) {
addBotMessage("Failed to verify API key. Please check your API key and try again.", "error-message");
return;
}
chatBox.innerHTML = '';
if (!location) {
addBotMessage("Please enter a city or country name.", "error-message");
return;
}
const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: `{"location":"${location}","apiKey":"${apiKey}"}`
};
fetch('/api/weather.php', options)
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch data from the server. Please try again later.');
}
return response.json();
})
.then(data => {
if (data.cod === '404') {
addBotMessage("Location not found. Please enter a valid city or country name.", "error-message");
} else {
const temperature = data.main.temp;
const humidity = data.main.humidity;
const pressure = data.main.pressure;
const airQuality = data.weather[0].description;
const windSpeed = data.wind.speed;
const windSpeedKmh = (windSpeed * 3.6).toFixed(2);
const temperatureEmoji = getTemperatureEmoji(temperature);
const humidityEmoji = getHumidityEmoji(humidity);
const pressureEmoji = getPressureEmoji(pressure);
const airQualityEmoji = getAirQualityEmoji(airQuality);
const windSpeedEmoji = getWindSpeedEmoji(windSpeedKmh);
const sunriseUTC = new Date(data.sys.sunrise * 1000);
const sunsetUTC = new Date(data.sys.sunset * 1000);
const timezoneOffset = data.timezone;
const sunriseLocal = new Date(sunriseUTC.getTime() + timezoneOffset * 1000).toLocaleTimeString('en-US', { timeZone: 'UTC' });
const sunsetLocal = new Date(sunsetUTC.getTime() + timezoneOffset * 1000).toLocaleTimeString('en-US', { timeZone: 'UTC' });
addBotMessage("Temperature: " + temperature + "°C " + temperatureEmoji, "success-message");
addBotMessage("Humidity: " + humidity + "%" + "\t" + humidityEmoji, "success-message");
addBotMessage("Air Pressure: " + pressure + " hPa " + pressureEmoji, "success-message");
addBotMessage("Weather Condition: " + airQuality + " " + airQualityEmoji, "success-message");
addBotMessage("Wind Speed: " + windSpeedKmh + " km/h " + windSpeedEmoji, "success-message");
addBotMessage("Sunrise: " + sunriseLocal, "success-message");
addBotMessage("Sunset: " + sunsetLocal, "success-message")
const output = chatBox.innerText.trim();
navigator.clipboard.writeText(output)
.then(() => {
addBotMessage("Weather data has been copied to clipboard.", "success-message");
})
.catch(err => {
console.error('Error copying weather data');
addBotMessage("Failed to copy weather data to clipboard. Please try again.", "error-message");
});
}
})
.catch(error => {
console.error('Error:', error);
addBotMessage(error.message || "An error occurred while fetching data. Please try again later.", "error-message");
});
}
function addBotMessage(message, className) {
const chatBox = document.getElementById("chat-box");
const messageElement = document.createElement("div");
messageElement.className = "chat-message bot-message";
messageElement.innerHTML = '<div class="message-content ' + className + '">' + message + '</div>';
chatBox.appendChild(messageElement);
}
function getTemperatureEmoji(temperature) {
if (temperature < 0) {
return '❄️';
} else if (temperature >= 0 && temperature < 15) {
return '🥶';
} else if (temperature >= 15 && temperature < 25) {
return '😊';
} else if (temperature >= 25 && temperature < 35) {
return '😎';
} else {
return '🔥';
}
}
function getHumidityEmoji(humidity) {
if (humidity < 30) {
return '💨';
} else if (humidity >= 30 && humidity < 60) {
return '😊';
} else {
return '💦';
}
}
function getPressureEmoji(pressure) {
if (pressure < 1000) {
return '🌀';
} else if (pressure >= 1000 && pressure < 1020) {
return '🙂';
} else {
return '⛈️';
}
}
function getAirQualityEmoji(airQuality) {
if (airQuality.includes('rain')) {
return '🌧️';
} else if (airQuality.includes('cloud')) {
return '☁️';
} else if (airQuality.includes('clear')) {
return '☀️';
} else {
return '';
}
}
function getWindSpeedEmoji(windSpeed) {
if (windSpeed < 10) {
return '🍃';
} else if (windSpeed >= 10 && windSpeed < 20) {
return '💨';
} else {
return '🌬️';
}
}
function clearLocalStorage() {
localStorage.removeItem("apiKey");
document.getElementById("apiKey").classList.remove("hidden");
window.location.reload();
}
function resetForm() {
document.getElementById("inputLocation").value = "";
document.getElementById("chat-box").innerHTML = '';
window.location.reload();
}
</script>
</body>
</html>