Added Other AP

This commit is contained in:
dev-chiefworks
2022-04-26 11:30:34 -04:00
parent 5e006a6a21
commit 47f4fad75c
251 changed files with 29298 additions and 4 deletions
@@ -0,0 +1,27 @@
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /SAVVY/geofenceareacity/
#RewriteBase /
#Checks to
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
#Header add Access-Control-Allow-Origin "*"
#Header add Access-Control-Expose-Headers "Access-Control-Allow-Origin"
#Header add Access-Control-Allow-Headers "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"
#Header add Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
#Header add Content-type "application/json"
@@ -0,0 +1,75 @@
<?php
class GeofenceAreaCityApi extends Api {
public $apiName = 'geofenceareacity';
public function __construct($requestUri, $encryption=true) {
$encryption = false; // We do not need encryption for this class
parent::__construct($requestUri, $encryption);
//$this->encryption = $encryption;
}
/**
* @OA\Get(
* path="/SAVVY/travelguide/api/geofenceareacity?city={city}&country={country}",
* security={{"token": {}}},
* summary="Get geofenceareacity by city and country code",
* @OA\Response(
* response="200",
* description="Found geofence area city",
* @OA\JsonContent(
* type="object"
* )
* )
* )
*/
public function indexAction() {
$message = 'No city found';
$result = [];
$res_status = 404;
try {
$params = [
'city' => $this->requestParams['city'] ?? '',
'country' => $this->requestParams['country'] ?? '',
'lat' => $this->requestParams['lat'] ?? '',
'lng' => $this->requestParams['lng'] ?? '',
'radius' => $this->requestParams['radius'] ?? '',
'status' => $this->requestParams['status'] ?? '',
];
$db = new Db();
list($city, $err) = GeofenceAreaCityModel::get( $db->getConnect(), $params );
if (!empty($err)) {
$result = [
"city" => NULL,
"error" => $err
];
} else {
$result = [
"city" => $city,
];
$res_status = 200;
}
} catch (Exception $e) {
$message = $e->getMessage();
$result = [
"city" => NULL,
"error" => $message
];
}
return $this->response($result, $res_status);
}
public function viewAction() {}
public function createAction() {}
public function updateAction() {}
public function deleteAction() {}
}
@@ -0,0 +1,108 @@
<?php
class GeofenceAreaCityModel {
public static function get($db, $params) {
$query = "SELECT geofence_city.latitude,
geofence_city.longitude,
geofence_city.id,
geofence_city.location,
geofence_city.city,
geofence_city.country,
geofence_city.radius,
settings.status,
settings.geofence_area_city,
settings.image_url
FROM geofence_area_city as geofence_city LEFT JOIN geofence_area_city_settings as settings ON settings.geofence_area_city = geofence_city.id";
if ($params && count($params) > 0) {
$lat = $params['lat'] ?? '';
$lng = $params['lng'] ?? '';
$radius = $params['radius'] ?? '';
$whereQuery = [];
$city = $params['city'] ?? '';
$country = $params['country'] ?? '';
$status = $params['status'] ?? '';
if (!empty($status)) {
$whereQuery[] = "settings.status=$status";
}
if (!empty($city)) {
$whereQuery[] = "LOWER(geofence_city.city)='" . pg_escape_string($city) ."'";
}
if (!empty($country)) {
$whereQuery[] = "LOWER(geofence_city.country)='" . pg_escape_string($country) . "'";
}
$whereQuery = implode(" AND ", $whereQuery);
if (!empty($lat) && !empty($lng) && !empty($radius)) {
$earth_radius = 6371;
$km_per_degree_lat = 111.2;
$pi = 3.14/180;
$latitude = $params["lat"];
$longitude = $params["lng"];
$distance_in_km = $params["radius"];
$query = " SELECT geofence_city.latitude, geofence_city.longitude, geofence_city.id, geofence_city.location, geofence_city.city, geofence_city.country, geofence_city.radius, settings.status, settings.geofence_area_city, settings.image_url FROM (
SELECT (
$earth_radius * acos(
cos( radians( {$latitude}) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians( {$longitude} ) )
+ sin( radians( {$latitude}) )
* sin( radians( latitude ) ))
) AS distance,
id,
city,
country,
latitude,
longitude,
location,
radius
FROM geofence_area_city
WHERE latitude BETWEEN "
. ($latitude - ($distance_in_km / $km_per_degree_lat))
. " AND " . ($latitude + ($distance_in_km / $km_per_degree_lat));
$delta = round($distance_in_km / ($km_per_degree_lat * COS(deg2rad($longitude))), 10);
// Bounding box for speed - latitude within range (longtitude to km not linear)
if(cos($longitude * $pi) > 0) {
$from = $longitude - $delta;
$to = $longitude + $delta;
$query .=
" AND longitude" .
" BETWEEN " . $from .
" AND " . $to;
} else {
$from = $longitude + $delta;
$to = $longitude - $delta;
$query .=
" AND longitude" .
" BETWEEN " . $from .
" AND " . $to;
}
// distance limit for circle
$query .= " ) geofence_city JOIN geofence_area_city_settings AS settings ON geofence_city.id = settings.geofence_area_city WHERE distance < " . $distance_in_km . "AND $whereQuery " . "ORDER BY distance ASC";
} else {
$query .= " WHERE $whereQuery";
}
}
$r = pg_query($db, $query);
if ($r && pg_num_rows($r)) {
$result = pg_fetch_all($r);
} else {
return [NULL, "City not found"];
}
return [$result, NULL];
}
}
@@ -0,0 +1,65 @@
<?php
require_once '../../core/backend.php';
require_once '../common/Api.php';
require_once '../common/Db.php';
require_once 'GeofenceAreaCityModel.php';
require_once 'GeofenceAreaCityApi.php';
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, client_id");
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
header('Content-type: application/json');
if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) {
exit();
}
$headers = getallheaders();
if ((!isset($headers["Authorization"]) || substr($headers["Authorization"], -strlen($httpAuthToken)) != $httpAuthToken) &&
(!isset($headers["authorization"]) || substr($headers["authorization"], -strlen($httpAuthToken)) != $httpAuthToken)) {
header('HTTP/1.1 401 Unauthorized');
header('Status: 401 Unauthorized');
echo "{\"status\":\"Missing authorization\"}";
exit();
}
try {
if (strpos($_SERVER['REQUEST_URI'], '/api/') === false) {
throw new Exception("Invalid API request");
}
$requestUri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
while (array_shift($requestUri) !== 'api') {
};
if ($requestUri[0] == 'geofenceareacity') {
$api = new GeofenceAreaCityApi($requestUri, false);
}
echo $api->run();
} catch (Exception $e) {
echo json_encode(array('error' => $e->getMessage()));
}
/**
* @OA\Info(
* title="Geofence Area City Endpoint API",
* version="1.0",
* @OA\Contact(
* email="support@float.sg"
* )
* )
*/
/**
* @OA\SecurityScheme(
* securityScheme="token",
* type="apiKey",
* name="Authorization",
* in="header"
* )
*/