-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathrpc.php
More file actions
executable file
·139 lines (111 loc) · 5.03 KB
/
rpc.php
File metadata and controls
executable file
·139 lines (111 loc) · 5.03 KB
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
<?php
//http://owntracks.org/booklet/tech/http/
# Obtain the JSON payload from an OwnTracks app POSTed via HTTP
# and insert into database table.
header("Content-type: application/javascript");
require_once('config.inc.php');
$response = array();
if ($_config['sql_type'] == 'mysql') {
require_once('lib/db/MySql.php');
/** @var MySql $sql */
$sql = new MySql($_config['sql_db'], $_config['sql_host'], $_config['sql_user'], $_config['sql_pass'], $_config['sql_prefix']);
} elseif ($_config['sql_type'] == 'mysqlpdo') {
require_once('lib/db/MySqlPdo.php');
/** @var MySqlPdo $sql */
$sql = new MySqlPdo($_config['sql_db'], $_config['sql_host'], $_config['sql_user'], $_config['sql_pass'], $_config['sql_prefix']);
} elseif ($_config['sql_type'] == 'sqlite') {
require_once('lib/db/SQLite.php');
/** @var SQLite $sql */
$sql = new SQLite($_config['sql_db']);
} else {
die('Invalid database type: ' . $_config['sql_type']);
}
if (array_key_exists('action', $_REQUEST)) {
if ($_REQUEST['action'] === 'getMarkers') {
if (!array_key_exists('dateFrom', $_GET)) {
$_GET['dateFrom'] = date("Y-m-d");
}
if (!array_key_exists('dateTo', $_GET)) {
$_GET['dateTo'] = date("Y-m-d");
}
if (array_key_exists('accuracy', $_GET) && $_GET['accuracy'] > 0) {
$accuracy = intVal($_GET['accuracy']);
} else {
$accuracy = $_config['default_accuracy'];
}
$time_from = date_parse_from_format('Y-m-d', $_GET['dateFrom']);
$time_from = mktime(0, 0, 0, $time_from['month'], $time_from['day'], $time_from['year']);
$time_to = date_parse_from_format( 'Y-m-d', $_GET['dateTo']);
$time_to = mktime(23, 59, 59, $time_to['month'], $time_to['day'], $time_to['year']);
//$time_to = strtotime('+1 day', $time_to);
$markers = $sql->getMarkers($time_from, $time_to, $accuracy);
if ($markers === false) {
$response['status'] = false;
$response['error'] = 'Database query error';
http_response_code(500);
} else {
$response['status'] = true;
$response['markers'] = json_encode($markers);
}
} elseif ($_REQUEST['action'] === 'deleteMarker') {
if (!array_key_exists('epoch', $_REQUEST)) {
$response['error'] = "No epoch provided for marker removal";
$response['status'] = false;
http_response_code(204);
}else{
$result = $sql->deleteMarker($_REQUEST['epoch']);
if ($result === false) {
$response['error'] = 'Unable to delete marker from database.';
$response['status'] = false;
http_response_code(500);
} else {
$response['msg'] = "Marker deleted from database";
$response['status'] = true;
}
}
} elseif ($_REQUEST['action'] === 'geoDecode') {
if (!array_key_exists('epoch', $_REQUEST)) {
$response['error'] = "No epoch provided for marker removal";
$response['status'] = false;
http_response_code(204);
} else {
// GET MARKER'S LAT & LONG DATA
$marker = $sql->getMarkerLatLon($_REQUEST['epoch']);
if ($marker === false) {
$response['error'] = 'Unable to get marker from database.';
$response['status'] = false;
} else {
$latitude = $marker['latitude'];
$longitude = $marker['longitude'];
// GEO DECODE LAT & LONG
$geo_decode_url = $_config['geo_reverse_lookup_url'] . 'lat=' .$latitude. '&lon='.$longitude;
$geo_decode_json = file_get_contents($geo_decode_url);
$geo_decode = @json_decode($geo_decode_json, true);
$place_id = intval($geo_decode['place_id']);
$osm_id = intval($geo_decode['osm_id']);
$location = strval($geo_decode['display_name']);
if ($location == '') { $location = @json_encode($geo_decode); }
//UPDATE MARKER WITH GEODECODED LOCATION
$result = $sql->updateLocationData((int)$_REQUEST['epoch'], (float)$latitude, (float)$longitude, $location, $place_id, $osm_id);
if ($result === false) {
$response['error'] = 'Unable to update marker in database.';
$response['status'] = false;
http_response_code(500);
} else {
$response['msg'] = 'Marker\'s location fetched and saved to database';
$response['location'] = $location;
$response['status'] = true;
}
}
}
} else {
$response['error'] = "No action to perform";
$response['status'] = false;
http_response_code(404);
}
} else {
$response['error'] = "Invalid request type or no action";
$response['status'] = false;
http_response_code(404);
}
echo json_encode($response);