-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
430 lines (271 loc) · 11.5 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
// make DB connection
$username = "be3ab62935a0b7";
$password = "ad817098";
$server = "us-cdbr-east-05.cleardb.net";
$db = "heroku_2e0461842e9755c";
try {
$db = new PDO("mysql:host=" . $server . ";dbname=" . $db, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
//test commint
$app->get('/', function() use ($app, $db){
$sql = "SELECT * FROM tags";
$query = $db->prepare($sql);
$query->execute();
$results = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
array_push($results, $row);
}
echo print_r($results);
});
//creates a new note
$app->post('/createNote', function() use ($app, $db){
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$type = $_POST['type'];
$note = $_POST['note'];
$output = file_get_contents('http://nominatim.openstreetmap.org/reverse?format=json&lat='.$latitude.'&lon='.$longitude.'&zoom=18&addressdetails=1');
$res = json_decode($output);
var_dump($res, true);
$road = $res->address->road;
$city = $res->address->city;
$address = $road.", ".$city;
$sql = "INSERT INTO tags (latitude, longitude, type, note, address) VALUES (:latitude, :longitude, :type, :note, :address)";
$insert = $db->prepare($sql);
$insert->execute(array(":latitude"=>$latitude, ":longitude"=>$longitude, ":type"=>$type, ":note"=>$note, ":address"=>$address));
echo "Successfully create tag";
});
//gets notes close to long and lat
$app->get('/getAll', function() use ($app, $db){
$response = array();
$sql = "SELECT * FROM tags";
$query = $db->prepare($sql);
$query->execute();
$results = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
array_push($results, $row);
}
$response['tags'] = $results;
//$sql = "SELECT DISTINCT on user * FROM users ORDER BY time DESC";
$sql = " SELECT * FROM users WHERE id IN (SELECT MAX(id) FROM users GROUP BY user)";
$query = $db->prepare($sql);
$query->execute();
$results = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$checkInTime = $row['time'];
$row['time'] = time() - $checkInTime;
array_push($results, $row);
}
$response['users'] = $results;
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($response);
});
//creates a new user watch request
$app->post('/watchupdate', function() use ($app, $db){
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$user = $_POST['user'];
if($_POST['force'] == "true"){
$manual = True;
}
$unixTime = Time();
//pull last entry records for users
$sql = "SELECT * FROM users WHERE user=:user ORDER BY time DESC LIMIT 1";
$query = $db->prepare($sql);
$query->execute(array(":user"=>$user));
$results = $query->fetch(PDO::FETCH_ASSOC);
// create new entry into users table
$sql = "INSERT INTO users (user, latitude, longitude, time) VALUES (:user, :latitude, :longitude, :time)";
$insert = $db->prepare($sql);
$insert->execute(array(":user"=>$user, ":latitude"=>$latitude, ":longitude"=>$longitude, ":time"=>$unixTime));
// Checks if the user's location has changed
//
// Needs to be a smaller range potentially
if (abs($results['latitude']-$latitude) > 0.01 || abs($results['longitude'] - $longitude) > 0.01 || $manual){
//needs to be about 0.001 for a building
$highLat = $latitude+1.01;
$lowLat = $latitude-1.01;
$highLong = $longitude+1.01;
$lowLong = $longitude-1.01;
//return tags within a radius
$sql = "SELECT * FROM tags WHERE latitude BETWEEN ".$lowLat." AND ".$highLat
. " AND longitude BETWEEN ".$lowLong." AND ".$highLong
. " AND type!='Fire Hydrant' LIMIT 2";
$query = $db->prepare($sql);
$query->execute();
$noteString = "";
$noteTitle = "";
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$noteTitle = $row['address'];
$noteString.= "- [".$row['type']."] ".$row['note']."\n ";
}
// send watch response to indicate new custom tags
$response = json_encode(array("update"=>1, "title"=> $noteTitle, "note"=>$noteString));
echo $response;
} else {
// no significant location change
$response = json_encode(array("update"=>0));
echo $response;
};
});
$app->get('/login', function() use ($app){
echo file_get_contents("login.html");
});
$app->post('/login', function() use ($app){
$email = $_POST['email'];
$fields = array(
'username' => $_POST['email'],
'password' => $_POST['password'],
'returnProfile'=>true,
'responseFilters'=>'PROFILE,SETTINGS,APPS,CONNECTIONS'
);
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://www.sirqul.com/api/3.05/account/get");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Application-Key: 66750de6c373ccea7f0a009239e6df32',
'Application-Rest-Key: 4ed060fd987153ca88db1419c103bfc9'
));
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
$response = json_decode($output);
if($response->valid = true){
echo file_get_contents("admin.html");
}
// close curl resource to free up system resources
curl_close($ch);
});
$app->post('/status', function () use ($app, $db) {
$beacon = $_POST['beacon'];
$patient = $_POST['patient'];
$time = $_POST['time'];
$sql = "INSERT INTO status (patient_id, beacon_id, post_time) VALUES (:patient, :beacon, :time)";
$insert = $db->prepare($sql);
$insert->execute(array(":patient"=>$patient, ":beacon"=>$beacon, ":time"=>$time));
// send a push notification
// get device token
$sql = "SELECT deviceToken FROM sessions WHERE patient = :user LIMIT 1";
$query = $db->prepare($sql);
$query->execute(array(":user"=>$user));
$results = $query->fetch(PDO::FETCH_ASSOC);
// Put your device token here (without spaces):
$deviceToken = 'ff12c28e30e013641b26847ae81dae500fbda61633f59fc3dba5b85029c37b87';
// Put your private key's passphrase here:
$passphrase = "DeathPanelP@ss";
// Put your alert message here:
$message = 'Patient status has changed.';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'hhpush.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'update-type' => 'update'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
// Close the connection to the server
fclose($fp);
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode(array("status"=>"done"));
});
$app->get('/recent/:user', function($user) use ($app, $db) {
$sql = "SELECT b.name, b.description, b.progressStep, s.post_time FROM status s INNER JOIN beacon b ON s.beacon_id = b.beacon_id WHERE s.patient_id = :user ORDER BY s.post_time DESC LIMIT 1";
$query = $db->prepare($sql);
$query->execute(array(":user"=>$user));
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($query->fetch(PDO::FETCH_ASSOC));
});
$app->get('/statuses', function () use ($app, $db) {
$sql = "SELECT DISTINCT patient_id FROM status";
$statement = $db->prepare($sql);
$statement->execute();
$response = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$sql = "SELECT s.patient_id, b.beacon_id, b.description, b.name FROM status s INNER JOIN beacon b ON s.beacon_id = b.beacon_id WHERE s.patient_id = :patient ORDER BY s.id DESC LIMIT 1";
$query = $db->prepare($sql);
$query->execute(array(":patient"=>$row['patient_id']));
$result = $query->fetch(PDO::FETCH_ASSOC);
array_push($response,$result);
}
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($response);
});
$app->get('/sessions', function () use ($app, $db) {
$sql = "SELECT * FROM sessions";
$statement = $db->prepare($sql);
$statement->execute();
$response = $statement->fetchAll(PDO::FETCH_ASSOC);
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($response);
});
$app->get('/emergency/:user', function($user) use ($app, $db) {
// get device token
$sql = "SELECT deviceToken FROM sessions WHERE patient = :user LIMIT 1";
$query = $db->prepare($sql);
$query->execute(array(":user"=>$user));
$results = $query->fetch(PDO::FETCH_ASSOC);
$sql = "UPDATE sessions SET emergency = :emergency WHERE patient = :patient";
$update = $db->prepare($sql);
$update->execute(array(":patient"=>$user, ":emergency"=>time()));
// Put your device token here (without spaces):
$deviceToken = 'ff12c28e30e013641b26847ae81dae500fbda61633f59fc3dba5b85029c37b87';
// Put your private key's passphrase here:
$passphrase = "DeathPanelP@ss";
// Put your alert message here:
$message = 'URGENT: The care provider needs your immediate attention.';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'hhpush.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'emergency.aif',
'update-type' => 'emergency'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
echo 'Message not delivered' . PHP_EOL;
}
else {
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode(array("status"=>"success"));
}
// Close the connection to the server
fclose($fp);
});
$app->run();
?>