-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_note.php
More file actions
executable file
·102 lines (87 loc) · 3.67 KB
/
move_note.php
File metadata and controls
executable file
·102 lines (87 loc) · 3.67 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
include 'config.php';
$notesDir = NOTES_DIR;
$notesLimit = NOTES_LIMIT;
// Limit dirs
// List of dirs
$dirs = array_filter(glob($notesDir . '*'), 'is_dir');
$dirNames = array_map('basename', $dirs);
// Get the selected dirs for moving and current notes dir
if (isset($_POST['selectedDir']) && isset($_POST['destinationDir'])) {
$selectedDir = substr(preg_replace('/[^a-zA-Z0-9-_]/', '', $_POST['selectedDir']), 0, 16);
$destinationDir = substr(preg_replace('/[^a-zA-Z0-9-_]/', '', $_POST['destinationDir']), 0, 16);
} else {
echo "ERROR: script should not be run directly."; exit(1);
}
$allowedDirs = array(); // Initialize an empty array for allowed dirs
foreach ($dirNames as $dir) {
if ($dir !== $selectedDir) {
$allowedDirs[] = $dir;
}
}
// Exit if destination dir is not in allowed dirs list
if (!in_array($destinationDir, $allowedDirs)) {
http_response_code(500); // Internal Server Error
exit;
}
// Get the list of filenames to move from the POST request
$noteNames = json_decode($_POST['noteNames'], true);
asort($noteNames, SORT_DESC); // sort descending order
$movedFilesCount = count($noteNames);
// Define the source and destination dir paths
$sourceDirPath = $notesDir . $selectedDir . "/";
$destinationDirPath = $notesDir . $destinationDir . "/";
// Get the list of txt files in the destination dir to check the limit
$files = array_diff(scandir($destinationDirPath), array('..', '.'));
$textFiles = array_filter($files, function ($noteName) {
return pathinfo($noteName, PATHINFO_EXTENSION) === 'txt';
});
$notesCount = count($textFiles);
if ($notesCount + $movedFilesCount >= $notesLimit) {
echo "ERROR: Exceed $notesLimit notes.";
http_response_code(500);
exit(1);
}
foreach ($noteNames as $noteName) {
// Move the file
$sourceFilePath = $sourceDirPath . $noteName;
$destinationFilePath = $destinationDirPath . $noteName;
if (file_exists($sourceFilePath)) {
if (file_exists($destinationFilePath)) {
// Find the highest numeric filename in the destination dir
$maxNumber = 0;
foreach ($textFiles as $note) {
if (preg_match('/(\d+)(?!.*\d)/', pathinfo($note, PATHINFO_FILENAME), $matches)) {
$numericPart = $matches[1];
if ($numericPart > $maxNumber) {
$maxNumber = intval($numericPart);
}
} else {
$maxNumber = 1;
}
}
$newNumber = ($maxNumber + 1);
while (true) {
$pattern = $destinationDirPath . '/*' . $newNumber . '.txt';
$numFiles = glob($pattern);
if (count($numFiles) > 0) {
$newNumber++;
} else {
break;
}
}
if (is_numeric(pathinfo($noteName, PATHINFO_FILENAME))) {
$newFileName = $newNumber . ".txt";
} else {
$newFileName = preg_replace('/\d+$/', '', pathinfo($noteName, PATHINFO_FILENAME)) . $newNumber . ".txt";
}
rename($sourceFilePath, $destinationDirPath . '/' . $newFileName);
} else {
rename($sourceFilePath, $destinationFilePath);
}
} else {
echo "Error: File '$noteName' does not exist in the source dir.";
exit;
}
}
?>