-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverftpprocess.php
More file actions
225 lines (185 loc) · 6.92 KB
/
Copy pathserverftpprocess.php
File metadata and controls
225 lines (185 loc) · 6.92 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
$return = true;
require __DIR__ . '/configuration.php';
require __DIR__ . '/include.php';
requireSameOrigin('index.php');
require __DIR__ . '/includes/ftp.php';
$task = sanitizeInput($_POST["task"] ?? "");
if ($task === "") {
$task = sanitizeInput($_GET["task"] ?? "");
}
$serverid = sanitizeInput($_POST["id"] ?? "");
if ($serverid === "") {
$serverid = sanitizeInput($_GET["id"] ?? "");
}
$path = sanitizeInput($_POST["path"] ?? "");
if ($path === "") {
$path = sanitizeInput($_GET["path"] ?? "");
}
$ftpconnection = null;
if ($serverid !== "") {
$rows = dbRow(
"SELECT ipid, boxid, user, password
FROM server
WHERE serverid='$serverid' AND clientid='{$_SESSION["clientid"]}'
LIMIT 1",
true
);
if (empty($rows) || empty($rows["ipid"])) {
$_SESSION["msg1"] = "FTP Connection Failed!";
$_SESSION["msg2"] = "Could not connect to the FTP.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
$rows1 = dbRow("SELECT ip FROM ip WHERE ipid='{$rows["ipid"]}' LIMIT 1", true);
$rows2 = dbRow("SELECT ftpport, passive FROM box WHERE boxid='{$rows["boxid"]}' LIMIT 1", true);
$ip = $rows1["ip"] ?? "";
$ftpport = $rows2["ftpport"] ?? 21;
$passive = (($rows2["passive"] ?? "") === "On");
if ($ip === "") {
$_SESSION["msg1"] = "FTP Connection Failed!";
$_SESSION["msg2"] = "Could not connect to the FTP.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
$ftpconnection = get_ftp_connection($ip, $ftpport, $rows["user"], $rows["password"], $passive);
if (!$ftpconnection) {
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
}
function failFtp(string $serverid, string $path, string $title, string $msg): void
{
$_SESSION["msg1"] = $title;
$_SESSION["msg2"] = $msg;
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
function joinFtpPath(string $path, string $name): string
{
$path = (string)$path;
if ($path === "" || substr($path, -1) === "/") {
return $path . $name;
}
return $path . "/" . $name;
}
switch ($task) {
case "filesave": {
$file = sanitizeInput($_POST["file"] ?? "");
$filecontents = (string)($_POST["filecontents"] ?? "");
if ($file === "" || $filecontents === "") {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
$filecontents = str_replace("\\r\\n", "\n", $filecontents);
$filecontents = str_replace("\\\"", "\"", $filecontents);
$filecontents = str_replace("\\'", "'", $filecontents);
$filecontents = str_replace("\\\\", "\\", $filecontents);
$tempHandle = fopen("php://temp", "r+");
if ($tempHandle === false) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
if (fwrite($tempHandle, $filecontents) === false) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
rewind($tempHandle);
$remote = joinFtpPath($path, $file);
if (!@ftp_fput($ftpconnection, $remote, $tempHandle, FTP_BINARY)) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
$_SESSION["msg1"] = "File Saved Successfully!";
$_SESSION["msg2"] = "The file has been saved.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
case "fileupload": {
if (!isset($_FILES["file"]) || (int)($_FILES["file"]["error"] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
$tmp = (string)($_FILES["file"]["tmp_name"] ?? "");
$name = sanitizeUploadFilename((string)($_FILES["file"]["name"] ?? ""));
if ($tmp === "" || $name === "") {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
$filecontents = file_get_contents($tmp);
if ($filecontents === false) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
$tempHandle = fopen("php://temp", "r+");
if ($tempHandle === false) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
if (fwrite($tempHandle, $filecontents) === false) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
rewind($tempHandle);
$remote = joinFtpPath($path, $name);
if (!@ftp_fput($ftpconnection, $remote, $tempHandle, FTP_BINARY)) {
failFtp($serverid, $path, "File Save Failed!", "The file has not been saved.");
}
$_SESSION["msg1"] = "File Saved Successfully!";
$_SESSION["msg2"] = "The file has been saved.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
case "filedelete": {
$file = sanitizeInput($_GET["file"] ?? "");
if ($file === "") {
failFtp($serverid, $path, "File Delete Failed!", "The file has not been deleted.");
}
$remote = joinFtpPath($path, $file);
if (!ftpDeleteFile($ftpconnection, $remote)) {
failFtp($serverid, $path, "File Delete Failed!", "The file has not been deleted.");
}
$_SESSION["msg1"] = "File Deleted Successfully!";
$_SESSION["msg2"] = "The file has been deleted.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
case "dirdelete": {
$dir = sanitizeInput($_GET["dir"] ?? "");
if ($dir === "") {
failFtp($serverid, $path, "Directory Delete Failed!", "The directory has not been deleted.");
}
$remote = joinFtpPath($path, $dir);
if (!ftpDeleteDirRecursive($ftpconnection, $remote)) {
failFtp($serverid, $path, "Directory Delete Failed!", "The directory has not been deleted.");
}
$_SESSION["msg1"] = "Directory Deleted Successfully!";
$_SESSION["msg2"] = "The directory has been deleted.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
case "makedir": {
$dir = sanitizeInput($_POST["dir"] ?? "");
if ($dir === "") {
failFtp($serverid, $path, "Directory Creation Failed!", "The directory has not been created.");
}
$remote = joinFtpPath($path, $dir);
if (!ftpEnsurePath($ftpconnection, $remote)) {
failFtp($serverid, $path, "Directory Creation Failed!", "The directory has not been created.");
}
$_SESSION["msg1"] = "Directory Created Successfully!";
$_SESSION["msg2"] = "The directory has been created.";
header("Location: serverftp.php?id=" . urlencode($serverid) . "&path=" . urlencode($path));
exit;
}
default:
header("Location: index.php");
exit;
}
function ftpDeleteDirRecursive($ftpconnection, string $filepath): bool
{
$filepath = preg_replace("/(.+?)\\/*$/", "\\1/", $filepath);
$list = ftp_nlist($ftpconnection, $filepath);
if ($list !== false && count($list) > 0) {
foreach ($list as $item) {
if (!@ftp_delete($ftpconnection, $item)) {
if (!ftpDeleteDirRecursive($ftpconnection, $item)) {
return false;
}
}
}
}
$result = @ftp_rmdir($ftpconnection, $filepath);
return $result !== false;
}