-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
36 lines (33 loc) · 1.08 KB
/
Copy pathapi.php
File metadata and controls
36 lines (33 loc) · 1.08 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
<?php
require_once 'auth.php';
check_auth();
// API pour sauver le JSON global
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if ($data) {
// Backup avant écriture
if (file_exists(JSON_DATA_PATH)) {
copy(JSON_DATA_PATH, JSON_DATA_PATH . '.bak');
}
// Écriture avec file locking
$fp = fopen(JSON_DATA_PATH, 'c');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
fflush($fp);
flock($fp, LOCK_UN);
fclose($fp);
echo json_encode(['status' => 'success']);
} else {
fclose($fp);
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Impossible de verrouiller le fichier']);
}
} else {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Données invalides']);
}
exit;
}
?>