-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
50 lines (40 loc) · 1.37 KB
/
api.php
File metadata and controls
50 lines (40 loc) · 1.37 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
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type');
$file = __DIR__ . '/presets.json';
if (!file_exists($file)) {
file_put_contents($file, json_encode([], JSON_PRETTY_PRINT));
}
$action = $_GET['action'] ?? '';
if ($action === 'save') {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data || !isset($data['name']) || !isset($data['widgets'])) {
http_response_code(400);
echo json_encode(['error' => 'Dados inválidos']);
exit;
}
$current = json_decode(file_get_contents($file), true);
$current[$data['name']] = $data['widgets'];
file_put_contents($file, json_encode($current, JSON_PRETTY_PRINT));
echo json_encode(['success' => true]);
exit;
}
if ($action === 'load') {
$name = $_GET['name'] ?? '';
$current = json_decode(file_get_contents($file), true);
if (isset($current[$name])) {
echo json_encode($current[$name]);
} else {
http_response_code(404);
echo json_encode(['error' => 'Preset não encontrado']);
}
exit;
}
if ($action === 'list') {
$current = json_decode(file_get_contents($file), true);
echo json_encode(array_keys($current));
exit;
}
echo json_encode(['error' => 'Ação desconhecida']);