-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
78 lines (68 loc) · 2.52 KB
/
index.php
File metadata and controls
78 lines (68 loc) · 2.52 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
<?php
require_once 'config.dist.php';
require_once 'Redis.php';
function isJamInternal() {
return isset($_SERVER['HTTP_X_ACCESS_TOKEN']) && $_SERVER['HTTP_X_ACCESS_TOKEN'] === JAM_INTERNAL_API_KEY;
}
header('Content-Type: application/json');
$http_status = '400 Bad Request';
$response = ['status' => 'error', 'message' => 'Error processing the request'];
$redis = new \PHPeter\Redis();
$cache_key = ALERT_CACHE_PREFIX . 'en';
switch ($_SERVER['REQUEST_METHOD']) {
case 'POST':
if (isJamInternal()) {
if (isset($_POST['alert_type'], $_POST['alert_text'])) {
if (in_array($_POST['alert_type'], ALERT_TYPES)) {
if (!isset($_POST['alert_ttl']) || is_numeric($_POST['alert_ttl'])) {
$to_cache = json_encode([
'id' => time(),
'type' => $_POST['alert_type'],
'text' => $_POST['alert_text']
]);
$ttl = isset($_POST['alert_ttl']) ? (int) $_POST['alert_ttl'] : ALERT_DEFAULT_TTL;
$redis->set($cache_key, $to_cache, $ttl);
$response = ['status' => 'success'];
} else {
$response['message'] = 'Wrong TTL';
}
} else {
$response['message'] = 'Wrong alert type';
}
} else {
$response['message'] = 'Alert infos are missing';
}
} else {
$http_status = '401 Unauthorized';
$response['message'] = 'Authentication failed';
}
break;
case 'DELETE':
if (isJamInternal()) {
$redis->del($cache_key);
$response = ['status' => 'success'];
} else {
$http_status = '401 Unauthorized';
$response['message'] = 'Authentication failed';
}
break;
default:
$cached = $redis->get($cache_key);
if ($cached !== false) {
$response = [
'status' => 'success',
'alert' => $cached
];
if (isJamInternal()) {
$response['alert']->ttl = $redis->ttl($cache_key);
}
} else {
$http_status = '404 Not Found';
$response['message'] = 'There is currently no ongoing alert';
}
}
if ($response['status'] == 'success') {
$http_status = '200 OK';
}
header('HTTP/1.1 ' . $http_status);
echo json_encode($response);