-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_location.php
184 lines (147 loc) · 5.43 KB
/
submit_location.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// NB: This requires the following variables to be set in config.php:
// $host
// $username
// $password
// $db_name
require 'config.php';
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = '';
$action=$_GET["action"];
// Here we respond to a plain old GET request, supplying the API description as a JSON response.
if(!isset($action))
{
$options = [
"create" => "Create a new search. Requires: name | coordinator | description - Returns: search_id or ERROR",
"register" => "Register a new searcher. Requires: forename | surname | mobile | search_id - Returns: id or ERROR",
"locate" => "Record a searcher's location. Requires: lat | long | id (from register) - Returns: JSON array of other searchers or ERROR.",
"chat" => "Submit a chat message back to the coordinator. Requires: chat | lat | long | id (from register) - Returns: OK or ERROR",
"locations" => "List all searcher locations. Requires: coordinator - Returns: JSON array of all searchers or ERROR.",
"reply" => "Reply to a chat from a searcher. Requires: chat | id (from register) - Returns: OK or ERROR",
"broadcast" => "Broadcasts to all searchers in the search this co-ordinator is the co-ordinator of. - Returns: OK or ERROR",
];
die(json_encode($options));
}
// They're creating a new search - add it into the database, and return it's id
if($action=='create') {
$name = $_GET['name'];
$coordinator = $_GET['coordinator'];
$description = $_GET['description'];
$sql="INSERT INTO blc_db.searchcase(name, coordinator, description) VALUES ('" . $name . "', " . $coordinator . ", '" . $description . "');";
$result=mysql_query($sql);
$response = [
"id" => mysql_insert_id(),
];
mysql_close();
die(json_encode($response));
}
// They're registering as a new searcher - add them into the database, and return an id
if($action=='register') {
$forename = $_GET["forename"];
$surname = $_GET["surname"];
$mobile = $_GET["mobile"];
$searchid = $_GET['search_id'];
$sql="INSERT INTO blc_db.searchprofile(searchcase_id, forename, surname, mobile) VALUES (" . $searchid . ", '" . $forename . "', '" . $surname . "', '" . $mobile . "');";
$result=mysql_query($sql);
$response = [
"id" => mysql_insert_id(),
];
mysql_close();
die(json_encode($response));
}
// They've just sent their latest location - save theirs, then return a collection of other people
if($action=='locate') {
$id = $_GET['id'];
$lat = $_GET['lat'];
$long = $_GET['long'];
// To simplify the INSERT or UPDATE complexity, we'll simply do a delete, immediately followed by an insert.
$sql="DELETE FROM blc_db.locations WHERE userid = $id;";
$result=mysql_query($sql);
$sql="INSERT INTO blc_db.locations(searchprofile_id, lat, lon) VALUES(" . $id . ", " . $lat . ", " . $long . ");";
//die($sql);
$result=mysql_query($sql);
// If successfully insert data into database, we now get all the locations (except our own)
if($result){
$sth = mysql_query("SELECT lat, lon FROM locations WHERE searchprofile_id <> $id;");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
mysql_close();
die(json_encode($rows));
}
}
// They've just sent in a 'chat' message. Store it into the database - we'll worry about what to do with it from there.
if($action=='chat'){
$chat = $_GET['chat']; // Text of the chat
$id = $_GET['id']; // User's ID number
$lat = $_GET['lat']; // Current location is recorded too
$long = $_GET['long'];
$sql="INSERT INTO chat(chat, searchprofile_id, lat, lon) VALUES ('" . $chat . "', " . $id . ", " . $lat . ", " . $long . ");";
$result=mysql_query($sql);
mysql_close();
if($result){
$response = [
"result" => "OK",
];
} else {
$response = [
"result" => "ERROR",
];
}
die(json_encode($response));
}
// This to get all searchers' locations...
if($action=='locations') {
// First we should clear out anyone who hasn't updated their location in the last 5 ?? minutes...
$sql = "DELETE FROM locations WHERE TIMESTAMPDIFF(MINUTE, recorded, CURRENT_TIMESTAMP) > 5;";
$sth = mysql_query($sql);
$coordinator = $_GET['coordinator'];
$sql = "SELECT locations.lat, locations.lon, sp1.id FROM searchprofile sp1 JOIN searchcase ON sp1.searchcase_id = searchcase.id JOIN locations ON sp1.id = locations.searchprofile_id WHERE searchcase.coordinator = " . $coordinator. ";";
$sth = mysql_query($sql);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
mysql_close();
die(json_encode($rows));
}
// This to send a chat message from co-ordinator to a searcher...
if($action=='reply') {
$chat=$_GET['chat']; // Text of the chat
$id=$_GET['id']; // User's ID number
$coord=$_GET['coord']; // User ID of the co-ordinator
$sql = "INSERT INTO blc_db.chat(chat, userid, fromuser) VALUES($chat, $id, -1);";
$sth = mysql_query($sql);
mysql_close();
if($sth){
$response = [
"result" => "OK",
];
} else {
$response = [
"result" => "ERROR",
];
}
die(json_encode($response));
}
// This allows the co-ordinator to send the same message to all searchers.
if($action=='broadcast'){
$chat=$_GET['chat']; // Text of the chat
$sql = "INSERT INTO blc_db.chat(chat, fromuser) VALUES($chat, -1);";
$sth = mysql_query($sql);
mysql_close();
if($sth){
$response = [
"result" => "OK",
];
} else {
$response = [
"result" => "ERROR",
];
}
die(json_encode($response));
}
?>