-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhook-handler.php
47 lines (43 loc) · 1.41 KB
/
webhook-handler.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
<?php
validateAuth('webhook_user', 'password');
$trackings = extractTrackingsFromRequest();
saveTrackingsToFile($trackings, __DIR__ . '/trackings-log.csv');
print count($trackings) . ' items processed';
function validateAuth($allowedLogin, $allowedPassword)
{
if (!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
|| $_SERVER['PHP_AUTH_USER'] !== $allowedLogin
|| $_SERVER['PHP_AUTH_PW'] !== $allowedPassword
) {
header('WWW-Authenticate: Basic realm="Restricted area"');
http_response_code(401);
print 'Unauthorized ' . $_SERVER['REQUEST_URI'] . ' from ' . $_SERVER['REMOTE_ADDR'];
exit;
}
}
function extractTrackingsFromRequest() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(400);
print 'Request method must be POST';
exit;
}
$json = file_get_contents('php://input');
$data = @json_decode($json, true);
if ($data === null || !isset($data['data'])) {
http_response_code(400);
print 'Bad json format';
exit;
}
return $data['data'];
}
function saveTrackingsToFile(array $trackings, $filename) {
$file = new SplFileObject($filename, 'a');
$timestamp = (new DateTime())->format('c');
foreach ($trackings as $tracking) {
$file->fputcsv([
$timestamp,
$tracking['trackingNumber'],
$tracking['status'],
]);
}
}