This is the source code for the website of Serenum. https://serenum.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

227 lines
7.8 KiB

<?php
require_once 'site-settings.php';
header('Content-Type: application/json;charset=utf-8');
$latitude_pure = (isset($_GET['lat']) ? strip_tags(htmlspecialchars($_GET['lat'])) : null);
$latitude_shorten = (float)number_format($latitude_pure, 3);
$longitude_pure = (isset($_GET['lng']) ? strip_tags(htmlspecialchars($_GET['lng'])) : null);
$longitude_shorten = (float)number_format($longitude_pure, 3);
$unit = (isset($_GET['uni']) ? strip_tags(htmlspecialchars($_GET['uni'])) : null);
$language = (isset($_GET['lan']) ? strip_tags(htmlspecialchars($_GET['lan'])) : null);
$apikey = (isset($_GET['api']) ? strip_tags(htmlspecialchars($_GET['api'])) : $config_apikey);
$apikey = (isset($_GET['api']) ? $_GET['api'] : $config_apikey);
$exclude = [];
$url_weather = @file_get_contents('https://api.openweathermap.org/data/2.5/onecall?lat='.$latitude_shorten.'&lon='.$longitude_shorten . (empty($exclude) ? '' : '&exclude='.implode(',', $exclude)).'&units='.$unit.'&lang='.$language.'&appid='.$apikey);
$data_weather = json_decode($url_weather);
$url_air = @file_get_contents('http://api.openweathermap.org/data/2.5/air_pollution?lat='.$latitude_shorten.'&lon='.$longitude_shorten.'&appid='.$apikey);
$data_air = json_decode($url_air);
$url_addr = file_get_contents('http://api.openweathermap.org/geo/1.0/reverse?lat='.$latitude_shorten.'&lon='.$longitude_shorten.'&limit=1&appid='.$apikey);
$data_addr = json_decode($url_addr);
# echo '<pre>'; var_dump($data_air); echo '</pre>';
if($url_weather === false) {
$error = error_get_last();
if(strpos($error['message'], '401') !== false) {
$error_code = 401;
$error_message = 'Unauthorized';
} elseif(strpos($error['message'], '404') !== false) {
$error_code = 404;
$error_message = 'Wrong request';
} elseif(strpos($error['message'], '429') !== false) {
$error_code = 429;
$error_message = '60 API calls per minute exceeded';
}
$json = [
'error' => $error_code,
'message' => $error_message
];
} else {
foreach($data_weather->daily AS $daily) {
$timestamp = new DateTime(date('Y-m-d H:i', $daily->dt));
$timestamp->setTimeZone(new DateTimeZone($data_weather->timezone));
$array_daily[] = [
'ts' => $daily->dt,
'ts_pl' => strtotime($timestamp->format('Y-m-d H:i:s')),
'ts_str' => [
'dy' => dayname(date('D', $daily->dt)),
'dt' => $timestamp->format('Y-m-d'),
'tm' => $timestamp->format('H:i'),
],
'wt_id' => $daily->weather[0]->id,
'wt_ic' => $daily->weather[0]->icon,
'wt_ic_icon' => weathericon($daily->weather[0]->icon . $daily->weather[0]->id),
'wt_dc' => $daily->weather[0]->description,
'tp' => [
'mr' => $daily->temp->morn,
'dy' => $daily->temp->day,
'ev' => $daily->temp->eve,
'ng' => $daily->temp->night,
'mi' => $daily->temp->min,
'ma' => $daily->temp->max
],
'tp_fl' => [
'mr' => $daily->feels_like->morn,
'dy' => $daily->feels_like->day,
'ev' => $daily->feels_like->eve,
'ng' => $daily->feels_like->night
],
'tp_dp' => $daily->dew_point,
'cl' => $daily->clouds,
'ws' => $daily->wind_speed,
'wd' => $daily->wind_deg,
'pr' => $daily->pressure,
'hu' => $daily->humidity,
'uv' => $daily->uvi,
'po_pr' => $daily->pop,
'po_pr_ra' => (isset($daily->rain->{'1h'}) ? $daily->rain->{'1h'} : 0),
'po_pr_sn' => (isset($daily->snow->{'1h'}) ? $daily->snow->{'1h'} : 0)
];
}
foreach($data_weather->hourly AS $hourly) {
$timestamp = new DateTime(date('Y-m-d H:i', $hourly->dt));
$timestamp->setTimeZone(new DateTimeZone($data_weather->timezone));
$array_hourly[] = [
'ts' => $hourly->dt,
'ts_pl' => strtotime($timestamp->format('Y-m-d H:i:s')),
'ts_str' => [
'dy' => dayname(date('D', $hourly->dt)),
'dt' => $timestamp->format('Y-m-d'),
'tm' => $timestamp->format('H:i'),
],
'wt_id' => $hourly->weather[0]->id,
'wt_ic' => $hourly->weather[0]->icon,
'wt_ic_icon' => weathericon($hourly->weather[0]->icon . $hourly->weather[0]->id),
'wt_dc' => $hourly->weather[0]->description,
'tp' => $hourly->temp,
'tp_fl' => $hourly->feels_like,
'tp_dp' => $hourly->dew_point,
'cl' => $hourly->clouds,
'ws' => $hourly->wind_speed,
'wd' => $hourly->wind_deg,
'pr' => $hourly->pressure,
'hu' => $hourly->humidity,
'uv' => $hourly->uvi,
'po_pr' => $hourly->pop,
'po_pr_ra' => (isset($hourly->rain->{'1h'}) ? $hourly->rain->{'1h'} : 0),
'po_pr_sn' => (isset($hourly->snow->{'1h'}) ? $hourly->snow->{'1h'} : 0)
];
}
if(!empty($data_weather->minutely)) {
foreach($data_weather->minutely AS $minutely) {
$timestamp = new DateTime(date('Y-m-d H:i', $minutely->dt));
$timestamp->setTimeZone(new DateTimeZone($data_weather->timezone));
$array_minutely[] = [
'ts' => $minutely->dt,
'ts_pl' => strtotime($timestamp->format('Y-m-d H:i:s')),
'ts_str' => [
'dy' => dayname(date('D', $minutely->dt)),
'dt' => $timestamp->format('Y-m-d'),
'tm' => $timestamp->format('H:i'),
],
'po_pr' => $minutely->precipitation
];
}
}
if(!empty($array_minutely)) {
foreach($array_minutely AS $precipitation) {
$array_precip[] = $precipitation['po_pr'];
}
$precip = array_filter($array_precip);
$average = (count($precip) ? array_sum($precip) / count($precip) : null);
}
$timestamp = new DateTime(date('Y-m-d H:i', $data_weather->current->dt));
$timestamp->setTimeZone(new DateTimeZone($data_weather->timezone));
$json = [
'website_version' => (float)file_get_contents('VERSION'),
'co' => [
'lat' => (float)$latitude_shorten,
'lng' => (float)$longitude_shorten
],
'pl' => [
'na' => (empty($data_addr[0]->name) ? null : $data_addr[0]->name),
'co' => (empty($data_addr[0]->country) ? null : $data_addr[0]->country)
],
'tz' => $data_weather->timezone,
'tz_os' => $data_weather->timezone_offset,
'ts' => $data_weather->current->dt,
'ts_pl' => strtotime($timestamp->format('Y-m-d H:i:s')),
'ts_str' => [
'dt' => $timestamp->format('Y-m-d'),
'tm' => $timestamp->format('H:i'),
],
'wt_id' => $data_weather->current->weather[0]->id,
'wt_ic' => $data_weather->current->weather[0]->icon,
'wt_ic_icon' => weathericon($data_weather->current->weather[0]->icon . $data_weather->current->weather[0]->id),
'wt_dc' => $data_weather->current->weather[0]->description,
'tp' => $data_weather->current->temp,
'tp_fl' => $data_weather->current->feels_like,
'tp_dp' => $data_weather->current->dew_point,
'cl' => $data_weather->current->clouds,
'ws' => $data_weather->current->wind_speed,
'wd' => $data_weather->current->wind_deg,
'pr' => $data_weather->current->pressure,
'hu' => $data_weather->current->humidity,
'uv' => $data_weather->current->uvi,
'vi' => ($unit == 'metric' ? ($data_weather->current->visibility * 0.001) : ($data_weather->current->visibility * 0.0006213711922)),
'po_pr_1h' => (empty($array_minutely) ? null : (float)number_format($average, 2)),
'po_pr_ra' => (isset($data_weather->current->rain->{'1h'}) ? $data_weather->current->rain->{'1h'} : 0),
'po_pr_sn' => (isset($data_weather->current->snow->{'1h'}) ? $data_weather->current->snow->{'1h'} : 0),
'ai' => [
'qu' => $data_air->list[0]->main->aqi,
'co' => [
'co' => $data_air->list[0]->components->co,
'no' => $data_air->list[0]->components->no,
'no2' => $data_air->list[0]->components->no2,
'o3' => $data_air->list[0]->components->o3,
'so2' => $data_air->list[0]->components->so2,
'pm2_5' => $data_air->list[0]->components->pm2_5,
'pm10' => $data_air->list[0]->components->pm10,
'nh3' => $data_air->list[0]->components->nh3
]
],
'mi' => [
'da' => (empty($array_minutely) ? null : $array_minutely)
],
'hr' => [
'da' => $array_hourly
],
'dy' => [
'da' => $array_daily
]
];
}
echo json_encode($json);
?>