-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_notes.php
More file actions
68 lines (55 loc) · 2.16 KB
/
save_notes.php
File metadata and controls
68 lines (55 loc) · 2.16 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
<?php
$notes_file_path = __DIR__ . '/TNbazaDanych/dashboard_notes.json';
$data_directory = dirname($notes_file_path);
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['status' => 'error', 'message' => 'Niedozwolona metoda żądania. Dozwolona tylko metoda POST.']);
http_response_code(405);
exit;
}
$raw_data = file_get_contents('php://input');
if ($raw_data === false) {
echo json_encode(['status' => 'error', 'message' => 'Nie udało się odczytać danych wejściowych.']);
http_response_code(400);
exit;
}
$data = json_decode($raw_data, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo json_encode(['status' => 'error', 'message' => 'Nieprawidłowy format danych JSON. Błąd: ' . json_last_error_msg()]);
http_response_code(400);
exit;
}
if (!isset($data['notes'])) {
echo json_encode(['status' => 'error', 'message' => 'Brakujący klucz "notes" w przesłanych danych.']);
http_response_code(400);
exit;
}
$notes_content = trim($data['notes']);
$notes_to_save = ['notes' => $notes_content];
$json_to_save = json_encode($notes_to_save, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if ($json_to_save === false) {
echo json_encode(['status' => 'error', 'message' => 'Nie udało się zakodować danych do formatu JSON.']);
http_response_code(500);
exit;
}
if (!is_dir($data_directory)) {
if (!mkdir($data_directory, 0775, true)) {
echo json_encode(['status' => 'error', 'message' => 'Nie udało się utworzyć katalogu `data`. Sprawdź uprawnienia.']);
http_response_code(500);
exit;
}
}
if (!is_writable($data_directory)) {
echo json_encode(['status' => 'error', 'message' => 'Katalog `data` nie ma uprawnień do zapisu.']);
http_response_code(500);
exit;
}
if (file_put_contents($notes_file_path, $json_to_save) === false) {
echo json_encode(['status' => 'error', 'message' => 'Podczas zapisu notatek wystąpił błąd. Sprawdź uprawnienia.']);
http_response_code(500);
} else {
echo json_encode(['status' => 'success', 'message' => 'Notatki zapisane pomyślnie.']);
http_response_code(200);
}
exit;
?>