-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
70 lines (57 loc) · 1.71 KB
/
Copy pathindex.php
File metadata and controls
70 lines (57 loc) · 1.71 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
<?php
/**
* This file is the endpoint for the GeoFency webhook.
* It writes the submitted JSON to a data file
*
* @author Gabriel Birke <gb@birke-software.de>
* @file
*/
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
require_once __DIR__."/vendor/autoload.php";
require_once __DIR__."/settings.php";
$request = Request::createFromGlobals();
function sendErrorResponse($msg, $status) {
$response = new Response($msg, $status, array('Content-Type' => 'text/plain'));
$response->send();
exit;
}
// Check if data dir is writable
if (!is_writable(dirname(DATA_FILE))) {
error_log(dirname(DATA_FILE)." is not writable");
sendErrorResponse("Filesystem error, check logs for more info.", Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Check if this is a POST request
if (!$request->isMethod('POST')) {
sendErrorResponse("Not allowed.", Response::HTTP_METHOD_NOT_ALLOWED);
}
// get valid JSON from request
$content = $request->getContent();
$json = json_decode($content);
if (!$json) {
sendErrorResponse("Request did not contain valid JSON.", Response::HTTP_BAD_REQUEST);
}
// Load old data
if (file_exists(DATA_FILE)) {
$data = json_decode(file_get_contents(DATA_FILE));
if (!$data) {
$data = new stdClass;
}
}
else {
$data = new stdClass;
}
// Store data as enter or exit
if ($json->entry) {
$data->enter = $json;
}
else {
$data->exit = $json;
}
// Save data
if (!file_put_contents(DATA_FILE, json_encode($data))) {
error_log(DATA_FILE." could not be written");
sendErrorResponse("Filesystem error, check logs for more info.", Response::HTTP_INTERNAL_SERVER_ERROR);
}
$response = new Response("OK", Response::HTTP_OK, array('Content-Type' => 'text/plain'));
$response->send();