-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_dir.php
More file actions
executable file
·44 lines (40 loc) · 1.45 KB
/
delete_dir.php
File metadata and controls
executable file
·44 lines (40 loc) · 1.45 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
<?php
include 'config.php';
$notesDir = NOTES_DIR;
// Validate and sanitize the directory name to be deleted from the POST request
if (isset($_POST['deldir']) && preg_match('/^[a-zA-Z0-9_-]+$/', $_POST['deldir'])) {
$delDir = $_POST['deldir'];
$sanitizedDelDir = preg_replace('/[^a-zA-Z0-9-_]/', '', $delDir);
$delDirPath = realpath($notesDir) . DIRECTORY_SEPARATOR . $sanitizedDelDir;
// Ensure the delete directory path is within the allowed notes directory
if (strpos($delDirPath, realpath($notesDir)) !== 0) {
echo "ERROR: Invalid directory path.";
http_response_code(400);
exit(1);
}
} else {
echo "ERROR: script should not be run directly.";
header("Location: .");
exit(1);
}
// Check if the directory exists and is empty
if (is_dir($delDirPath)) {
if (!glob("$delDirPath/*")) {
if (rmdir($delDirPath)) {
echo "Directory deleted successfully.";
} else {
echo "ERROR: Could not delete directory.";
http_response_code(500);
exit(1);
}
} else {
echo "ERROR: Directory contains files and cannot be deleted.";
http_response_code(400);
exit(1);
}
} else {
echo "ERROR: Directory does not exist.";
http_response_code(400);
exit(1);
}
?>