Move lat/long validation to seperate file, add terrain data from gis
This commit is contained in:
parent
31881d5e69
commit
3cc5cd9374
20
getterrain.php
Normal file
20
getterrain.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
require 'required.php';
|
||||
require 'onlyloggedin.php';
|
||||
require 'latlong_validate.php';
|
||||
|
||||
$terrain = json_decode(file_get_contents("http://gis.terranquest.net/terrain.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long']));
|
||||
|
||||
if (!is_empty($terrain['error'])) {
|
||||
sendError($terrain['error'], true);
|
||||
} else {
|
||||
$out = [
|
||||
"status" => "OK",
|
||||
"typeid" => $terrain['type'],
|
||||
"latitude" => $terrain['latitude'],
|
||||
"longitude" => $terrain['longitude'],
|
||||
"typename" => $terrain['name']
|
||||
];
|
||||
die(json_encode($out));
|
||||
}
|
12
latlong_validate.php
Normal file
12
latlong_validate.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
// Validate input
|
||||
if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
|
||||
sendError("Missing information.", true);
|
||||
}
|
||||
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
|
||||
sendError("Latitude (lat) is in the wrong format.", true);
|
||||
}
|
||||
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
|
||||
sendError("Longitude (long) is in the wrong format.", true);
|
||||
}
|
10
response.php
10
response.php
@ -1,5 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Send an OK message.
|
||||
* @param type $message the message
|
||||
* @param type $die to die or not to die?
|
||||
*/
|
||||
function sendOK($message = "", $die = false) {
|
||||
if (!is_empty($message) && JSON) {
|
||||
echo '{ "status": "OK", "message": "'.$message.'" }';
|
||||
@ -15,6 +20,11 @@ function sendOK($message = "", $die = false) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an error message.
|
||||
* @param type $message the message
|
||||
* @param type $die to die or not to die?
|
||||
*/
|
||||
function sendError($error, $die = false) {
|
||||
if (JSON) {
|
||||
echo '{ "status": "ERROR", "message": "' . $error . '" }';
|
||||
|
@ -23,6 +23,11 @@ define("GEOCACHE_KEY", "");
|
||||
define("MUNZEE_KEY", "");
|
||||
define("MUNZEE_SECRET", "");
|
||||
|
||||
// API key for the TerranQuest GIS server.
|
||||
// Has global terrain data and other large sets.
|
||||
// Get instructions for requesting a key at gis.terranquest.net.
|
||||
define("GIS_API_KEY", "");
|
||||
|
||||
define("GOOGLEPLAY_PUBLICKEY", "");
|
||||
define("APP_STORE_SANDBOX", true);
|
||||
|
||||
|
@ -5,24 +5,14 @@
|
||||
* Get the contents of the current weather with $currently
|
||||
*/
|
||||
|
||||
|
||||
// Validate input
|
||||
if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
|
||||
sendError("Missing information.", true);
|
||||
}
|
||||
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
|
||||
sendError("Latitude (lat) is in the wrong format.", true);
|
||||
}
|
||||
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
|
||||
sendError("Longitude (long) is in the wrong format.", true);
|
||||
}
|
||||
require 'latlong_validate.php';
|
||||
|
||||
// Round to 2 digits (approx. 1.1km)
|
||||
$lat = number_format((float) $VARS['lat'], 2, '.', '');
|
||||
$long = number_format((float) $VARS['long'], 2, '.', '');
|
||||
|
||||
// Delete old records
|
||||
$database->delete('weathercache', ["date[<]" => date('Y-m-d H:i:s', strtotime('-1 hour'))]);
|
||||
$database->delete('weathercache', ["date[<]" => date('Y-m-d H:i:s', strtotime('-30 minutes'))]);
|
||||
|
||||
// If we don't get a cache hit, request from the API
|
||||
if (!$database->has('weathercache', ["AND" => ["latitude" => $lat, "longitude" => $long]])) {
|
||||
|
Reference in New Issue
Block a user