-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_note.php
More file actions
executable file
·55 lines (47 loc) · 1.77 KB
/
save_note.php
File metadata and controls
executable file
·55 lines (47 loc) · 1.77 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
<?php
include 'config.php';
$notesDir = NOTES_DIR;
$sizeLimit = NOTE_SIZE_LIMIT;
// 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: .");
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 filename and content from the POST request
if (isset($_POST['noteName']) && preg_match('/^[a-zA-Z0-9-_]+\.txt$/', $_POST['noteName'])) {
$noteName = $_POST['noteName'];
} else {
echo "ERROR: Invalid file name.";
exit(1);
}
$content = isset($_POST['content']) ? $_POST['content'] : exit(1);
if (empty($content)) {
$content = ' ';
}
// Calculate the size of the content in bytes
$contentSize = strlen($content);
// Check if the content size exceeds the limit
if ($contentSize <= $sizeLimit) {
// Save the content to the file in the selected dir
if (!file_put_contents($dirPath . $noteName, $content)) {
http_response_code(500); // Set the error status code to 500
echo 'Error writing note. Check permissions or try again.';
exit(1);
}
} else {
// Respond with an error message if content size exceeds the limit
echo "ERROR: Content size exceeds the limit of $sizeLimit bytes.";
http_response_code(400);
exit(1);
}
?>