This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclass.transfer.php
executable file
·247 lines (229 loc) · 10.1 KB
/
class.transfer.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See [root]/license.md for more information. This information must remain intact.
*/
class transfer_controller {
static public function startConnection($host, $user, $pass, $port, $ftps) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
$ftp->startConnection($host, $user, $pass, $port, $ftps);
} else {
$ssh2 = new scp_client();
$ssh2->startConnection($host, $user, $pass, $port);
}
//Create mt_rand for session
if (!isset($_SESSION['ct_mt_rand'])) {
$_SESSION['ct_mt_rand'] = ".ct_" . mt_rand();
}
}
static public function stopConnection() {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
$ftp->stopConnection();
} else {
$ssh2 = new scp_client();
$ssh2->stopConnection();
}
}
/////////////////////////////////////////////////////////////////////////
// Remote server index (Returns a list of files and directorys on the
// remote server as json) For more information: see parseRawList();
/////////////////////////////////////////////////////////////////////////
static public function getServerFiles($path) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->getServerFiles($path);
} else {
$ssh2 = new scp_client();
return $ssh2->getServerFiles($path);
}
}
/////////////////////////////////////////////////////////////////////////
// Transfer a file to remote server
/////////////////////////////////////////////////////////////////////////
static public function transferFileToServer($cPath, $sPath, $fName, $mode) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->transferFileToServer($cPath, $sPath, $fName, $mode);
} else {
$ssh2 = new scp_client();
return $ssh2->transferFileToServer($cPath, $sPath, $fName);
}
}
/////////////////////////////////////////////////////////////////////////
// Transfer a file to Codiad Server
/////////////////////////////////////////////////////////////////////////
static public function transferFileToClient($cPath, $sPath, $fName, $mode) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->transferFileToClient($cPath, $sPath, $fName, $mode);
} else {
$ssh2 = new scp_client();
return $ssh2->transferFileToClient($cPath, $sPath, $fName);
}
}
/////////////////////////////////////////////////////////////////////////
// Create directory on remote server
/////////////////////////////////////////////////////////////////////////
static public function createServerDirectory($path) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->createServerDirectory($path);
} else {
$ssh2 = new scp_client();
return $ssh2->createServerDirectory($path);
}
}
/////////////////////////////////////////////////////////////////////////
// Get current directory name
/////////////////////////////////////////////////////////////////////////
static public function getSeverDirectory() {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->getSeverDirectory();
} else {
$ssh2 = new scp_client();
return $ssh2->getSeverDirectory();
}
}
/////////////////////////////////////////////////////////////////////////
// Remove file on remote server
/////////////////////////////////////////////////////////////////////////
static public function removeServerFile($path) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->removeServerFile($path);
} else {
$ssh2 = new scp_client();
return $ssh2->removeServerFile($path);
}
}
/////////////////////////////////////////////////////////////////////////
// Remove directory on remote server
/////////////////////////////////////////////////////////////////////////
static public function removeServerDirectory($path) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->removeServerDirectory($path);
} else {
$ssh2 = new scp_client();
return $ssh2->removeServerDirectory($path);
}
}
/////////////////////////////////////////////////////////////////////////
// Change permissions of file on remote server
/////////////////////////////////////////////////////////////////////////
static public function changeServerFileMode($path, $mode) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->changeServerFileMode($path, $mode);
} else {
$ssh2 = new scp_client();
return $ssh2->changeServerFileMode($path, $mode);
}
}
/////////////////////////////////////////////////////////////////////////
// Rename directory or file
/////////////////////////////////////////////////////////////////////////
static public function rename($path, $old, $new) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->rename($path, $old, $new);
} else {
$ssh2 = new scp_client();
return $ssh2->rename($path, $old, $new);
}
}
/////////////////////////////////////////////////////////////////////////
// Rename directory or file
/////////////////////////////////////////////////////////////////////////
static public function changeServerGroup($path, $name) {
if ($_SESSION['transfer_type'] == "ftp") {
return '{"status":"error","message":"FTP: Impossible To Change Group"}';
} else {
$ssh2 = new scp_client();
return $ssh2->changeServerGroup($path, $name);
}
}
/////////////////////////////////////////////////////////////////////////
// Edit file locally
/////////////////////////////////////////////////////////////////////////
static public function editFileLocally($cName, $cBase, $sPath, $fName, $mode) {
if ($_SESSION['transfer_type'] == "ftp") {
$ftp = new ftp_client();
return $ftp->editFileLocally($cName, $cBase, $sPath, $fName, $mode);
} else {
$ssh2 = new scp_client();
return $ssh2->editFileLocally($cName, $cBase, $sPath, $fName);
}
}
/////////////////////////////////////////////////////////////////////////
// Delete locally edited file
/////////////////////////////////////////////////////////////////////////
static public function editFileLocallyClose($cPath) {
$msg = array('status' => 'success', 'message' => '');
if (is_file($cPath)) {
if (strstr($cPath,$_SESSION['ct_mt_rand']) === false) {
$msg = array('status' => 'error', 'message' => 'Security stop!');
return json_encode($msg);
}
if (!unlink($cPath)) {
$msg = array('status' => 'error', 'message' => 'Failed to delete file');
}
$dir = dirname($cPath);
$scan = scandir($dir);
//Ignore . and ..
if (count($scan) <= 2) {
rmdir($dir);
}
} else {
$msg = array('status' => 'error', 'message' => 'Failed to delete file');
}
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Sort an array of a server index
/////////////////////////////////////////////////////////////////////////
static public function mySort($a, $b) {
$d = self::editString($a['fName']);
$e = self::editString($b['fName']);
$c = array($d, $e);
sort($c);
if ($d == $e) {
return 0;
}
if ($c[0] == $d) {
return -1;
} else {
return 1;
}
}
static public function editString($str) {
if (substr($str, 0, 1) == ".") {
$str = substr($str, 1);
}
return strtolower($str);
}
static public function getWorkspacePath($path) {
//Security check
if (!Common::checkPath($path)) {
die('{"status":"error","message":"Invalid path"}');
}
if (strpos($path, "/") === 0) {
//Unix absolute path
return $path;
}
if (strpos($path, ":/") !== false) {
//Windows absolute path
return $path;
}
if (strpos($path, ":\\") !== false) {
//Windows absolute path
return $path;
}
return WORKSPACE . "/" . $path;
}
}
?>