-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
38 lines (38 loc) · 1.33 KB
/
Copy pathapi.php
File metadata and controls
38 lines (38 loc) · 1.33 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
<?php
header("Content-Type: application/json");
$dataFile = __DIR__ . '/counter_data.json';
$tokenFile = __DIR__ . '/tokens.token';
if (!file_exists($dataFile)) {
file_put_contents($dataFile, json_encode([]));
}
if (!file_exists($tokenFile)) {
file_put_contents($tokenFile, json_encode([]));
}
$data = json_decode(file_get_contents($dataFile), true);
$tokens = json_decode(file_get_contents($tokenFile), true);
$token = $_SERVER['HTTP_TOKEN'] ?? '';
$authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
$tokenHash = hash('sha256', $token);
if (!isset($tokens[$tokenHash]) || $tokens[$tokenHash] !== hash('sha256', $authHeader)) {
http_response_code(403);
echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
exit;
}
$action = $_GET['action'] ?? '';
if ($action === 'increment') {
if (!isset($data[$token])) {
$data[$token] = 0;
}
$data[$token]++;
file_put_contents($dataFile, json_encode($data));
echo json_encode(['status' => 'success', 'count' => $data[$token]]);
} elseif ($action === 'get') {
if (!isset($data[$token])) {
echo json_encode(['status' => 'error', 'message' => 'Token not found']);
} else {
echo json_encode(['status' => 'success', 'count' => $data[$token]]);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
}
?>