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.
 
 
 

183 lines
5.2 KiB

<?php
/**
* RX2 - Main API to process in coming request for closest location.
* 2019-09-29
* Rick Hays
*
*/
include_once 'assets/classes/geocode.php';
// See if we are in test mode //////////////////////////////////////////////////////////////////////////////////////
$TEST = GetParam('T', 0);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Test Key before allowing access - No encryption, simple key test.
$key = strtoupper(GetParam('K'));
if ($key !== 'RLH4321')
{
$result['status'] = 'ERROR';
$result['code'] = '100';
$result['description'] = 'Bad Key Code.';
$jsonOut = json_encode($result);
}
else
{
$request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
$command = strtoupper(GetParam('C'));
$address = rawurldecode(GetParam('address')) . '';
switch ($request_method)
{
case 'GET':
switch ($command)
{
case 'LL':
if ($address === '')
{
$aOutput = array
(
'status' => 'ERROR',
'code' => '101',
'description' => 'No Address present.',
);
$jsonOut = json_encode($aOutput);
break;
} // if ($address === '')
$geo = new geocode();
$latlon = $geo->getLatLong($address);
$jsonOut = json_encode($latlon);
$geo = NULL;
break;
case 'DIST':
if ($address === '')
{
$aOutput = array
(
'status' => 'ERROR',
'code' => '101',
'description' => 'No Address present.',
);
$jsonOut = json_encode($aOutput);
break;
} // if ($address === '')
$geo = new geocode();
$db = new PDO('sqlite:assets/db/RX.db');
$latlon = $geo->getLatLong($address);
if ($latlon['status'] === 'OK')
{
$shortest = 999999.99; // <-- Sets to a very high value to find shortest distance below.
$pharmacies = $db->query('SELECT rowid,* FROM pharmacies');
$SKIP = 0; // Part of test mode to skip all but one record in DB
foreach ($pharmacies as $pharmacy)
{
$destination = $pharmacy['Address'] . ' ' . $pharmacy['City'] . ' ' . $pharmacy['State'] . ' ' . $pharmacy['Zip'];
if ($SKIP === 0) $result = $geo->getDistance($address, $destination);
if ($TEST == 1) $SKIP = 1;
if ($result['status'] === 'OK')
{
$distance = floatval(preg_replace('/[^0-9.]/', '', $result['distance']));
if ($distance < $shortest)
{
$aOutput = array
(
'pharmacy' => $pharmacy['Pharmacy'],
'address' => $pharmacy['Address'],
'city' => $pharmacy['City'],
'state' => $pharmacy['State'],
'zip' => $pharmacy['Zip'],
'latitude' => $pharmacy['Latitude'],
'longitude' => $pharmacy['Longitude'],
'home_lat' => $latlon['lat'],
'home_lon' => $latlon['lon'],
'status' => $result['status'],
'distance' => $result['distance'],
'duration' => $result['duration'],
);
$shortest = $distance;
} // if ($result['distance'] < $shortest)
} // if ($result['status'] === 'OK')
else
{
$aOutput = array
(
'status' => 'ERROR',
'code' => '104',
'description' => "Problem returning distance between address and pharmacy.\nAddress: " . $address . "\nPharmacy( ROWID: " . $pharmacy['rowid'] . " Name: " . $pharmacy['Pharmacy'] . ') ',
);
$jsonOut = json_encode($aOutput);
break;
} //else ($result['status'] === 'OK')
} // foreach ($pharmacies as $pharmacy)
} // if ($latlon['status'] === 'OK')
else
{
$aOutput = array
(
'status' => 'ERROR',
'code' => '103',
'description' => "Problem returning Lat / Lon.\nAddress: " . $address,
);
$jsonOut = json_encode($aOutput);
break;
} // else ($latlon['status'] === 'OK')
$jsonOut = json_encode($aOutput);
$db = NULL;
$geo = NULL;
break;
default:
$aOutput = array
(
'status' => 'ERROR',
'code' => '102',
'description' => "Invalid Command sent - " . $command,
);
$jsonOut = json_encode($aOutput);
break;
} // switch ($command)
break;
// case 'POST':
// break;
//
// case 'PUT':
// break;
//
// case 'DELETE':
// break;
//
default:
// Invalid Request Method
header("HTTP/1.0 405 Method Not Allowed");
exit;
} // switch ($request_method)
} // else ($key !== 'RLH4321')
echo $jsonOut;
// ##### END OF PROGRAM ################################################################################################
/**
* GetParam - Returns the GET or POST values, and allows for defaults if not present.
* @param $param_name
* @param null $default
*
* @return string|null
*/
function GetParam($param_name, $default=NULL)
{
global $_POST;
global $_GET;
$param_value = "";
if(isset($_POST[$param_name]))
$param_value = $_POST[$param_name];
else if(isset($_GET[$param_name]))
$param_value = $_GET[$param_name];
else if($param_value === '')
$param_value = $default;
return $param_value;
}