-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_note.php
More file actions
executable file
·58 lines (51 loc) · 1.86 KB
/
delete_note.php
File metadata and controls
executable file
·58 lines (51 loc) · 1.86 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
<?php
include 'config.php';
$notesDir = NOTES_DIR;
// Validate and sanitize the selected dir from the POST request
if (isset($_POST['dir']) && preg_match('/^[a-zA-Z0-9_-]+$/', $_POST['dir'])) {
$selectedDir = $_POST['dir'];
} else {
echo "ERROR: Invalid directory.";
header("Location: .");
http_response_code(400);
exit(1);
}
// Define the dir path and prevent directory traversal
$dirPath = realpath($notesDir . DIRECTORY_SEPARATOR . $selectedDir) . DIRECTORY_SEPARATOR;
if (strpos($dirPath, realpath($notesDir)) !== 0) {
echo "ERROR: Invalid directory path.";
http_response_code(400);
exit(1);
}
// Get the list of filenames to delete from the POST request
$noteNames = json_decode($_POST['noteNames'], true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "ERROR: Invalid JSON input.";
http_response_code(400);
exit(1);
}
// Validate file names
foreach ($noteNames as $noteName) {
if (!preg_match('/^[a-zA-Z0-9_-]+\.txt$/', $noteName)) {
echo "ERROR: Invalid note name detected.";
http_response_code(400);
exit(1);
}
}
// Iterate through the filenames and delete each note from the selected dir
foreach ($noteNames as $noteName) {
$notePath = $dirPath . $noteName;
if (file_exists($notePath)) {
if (!unlink($notePath)) {
echo "ERROR: Could not delete note " . htmlspecialchars($noteName, ENT_QUOTES, 'UTF-8') . ".";
http_response_code(500);
exit(1);
}
} else {
echo "ERROR: File " . htmlspecialchars($noteName, ENT_QUOTES, 'UTF-8') . " does not exist.";
http_response_code(400);
exit(1);
}
}
echo 'Files deleted successfully.';
?>